-
Notifications
You must be signed in to change notification settings - Fork 25
/
watch_test.go
68 lines (55 loc) · 1.38 KB
/
watch_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package CouloyDB
import (
"context"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestDB_Watch(t *testing.T) {
db, err := NewCouloyDB(DefaultOptions())
assert.Nil(t, err)
assert.NotNil(t, db)
defer destroyCouloyDB(db)
key := "CouloyDB"
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
watchCh := db.Watch(ctx, key)
go func() {
_ = db.Put([]byte(key), []byte("value1"))
_ = db.Put([]byte(key), []byte("value2"))
_ = db.Del([]byte(key))
}()
expectedEvents := []*watchEvent{
{key: key, value: []byte("value1"), eventType: PutEvent},
{key: key, value: []byte("value2"), eventType: PutEvent},
{key: key, value: nil, eventType: DelEvent},
}
for _, expectedEvent := range expectedEvents {
select {
case <-ctx.Done():
assert.Fail(t, "Context canceled before receiving all events")
return
case event, ok := <-watchCh:
assert.True(t, ok)
assert.Equal(t, expectedEvent, event)
}
}
}
func TestDB_Watch_Cancel(t *testing.T) {
db, err := NewCouloyDB(DefaultOptions())
assert.Nil(t, err)
assert.NotNil(t, db)
defer destroyCouloyDB(db)
key := "key"
ctx, cancel := context.WithCancel(context.Background())
watchCh := db.wm.watch(ctx, key)
go func() {
cancel()
}()
select {
case <-ctx.Done():
break
case _, _ = <-watchCh:
assert.Fail(t, "Context should be canceled before receiving all events")
}
}