-
Notifications
You must be signed in to change notification settings - Fork 639
/
watch_test.go
147 lines (130 loc) · 3.07 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package rosedb
import (
"math/rand"
"testing"
"github.com/rosedblabs/rosedb/v2/utils"
"github.com/stretchr/testify/assert"
)
func TestWatch_Insert_Scan(t *testing.T) {
capacity := 1000
// There are two spaces to determine whether the queue is full and overwrite the write.
size := capacity - 2
q := make([][2][]byte, 0, size)
w := NewWatcher(uint64(capacity))
for i := 0; i < size; i++ {
key := utils.GetTestKey(rand.Int())
value := utils.RandomValue(128)
q = append(q, [2][]byte{key, value})
w.putEvent(&Event{
Action: WatchActionPut,
Key: key,
Value: value,
BatchId: 0,
})
}
for i := 0; i < size; i++ {
e := w.getEvent()
assert.NotEmpty(t, e)
key := q[i][0]
assert.Equal(t, key, e.Key)
value := q[i][1]
assert.Equal(t, value, e.Value)
}
}
func TestWatch_Rotate_Insert_Scan(t *testing.T) {
capacity := 1000
q := make([][2][]byte, capacity)
w := NewWatcher(uint64(capacity))
for i := 0; i < 2500; i++ {
key := utils.GetTestKey(rand.Int())
value := utils.RandomValue(128)
w.putEvent(&Event{
Action: WatchActionPut,
Key: key,
Value: value,
BatchId: 0,
})
sub := i % capacity
q[sub] = [2][]byte{key, value}
}
sub := int(w.queue.Front)
for {
e := w.getEvent()
if e == nil {
break
}
key := q[sub][0]
assert.Equal(t, key, e.Key)
value := q[sub][1]
assert.Equal(t, value, e.Value)
sub = (sub + 1) % capacity
}
}
func TestWatch_Put_Watch(t *testing.T) {
options := DefaultOptions
options.WatchQueueSize = 10
db, err := Open(options)
assert.Nil(t, err)
defer destroyDB(db)
w, err := db.Watch()
assert.Nil(t, err)
for i := 0; i < 50; i++ {
key := utils.GetTestKey(rand.Int())
value := utils.RandomValue(128)
err = db.Put(key, value)
assert.Nil(t, err)
event := <-w
assert.Equal(t, WatchActionPut, event.Action)
assert.Equal(t, key, event.Key)
assert.Equal(t, value, event.Value)
}
}
func TestWatch_Put_Delete_Watch(t *testing.T) {
options := DefaultOptions
options.WatchQueueSize = 10
db, err := Open(options)
assert.Nil(t, err)
defer destroyDB(db)
w, err := db.Watch()
assert.Nil(t, err)
key := utils.GetTestKey(rand.Int())
value := utils.RandomValue(128)
err = db.Put(key, value)
assert.Nil(t, err)
err = db.Delete(key)
assert.Nil(t, err)
for i := 0; i < 2; i++ {
event := <-w
assert.Equal(t, key, event.Key)
if event.Action == WatchActionPut {
assert.Equal(t, value, event.Value)
} else if event.Action == WatchActionDelete {
assert.Equal(t, 0, len(event.Value))
}
}
}
func TestWatch_Batch_Put_Watch(t *testing.T) {
options := DefaultOptions
options.WatchQueueSize = 1000
db, err := Open(options)
assert.Nil(t, err)
defer destroyDB(db)
w, err := db.Watch()
assert.Nil(t, err)
times := 100
batch := db.NewBatch(DefaultBatchOptions)
for i := 0; i < times; i++ {
err = batch.Put(utils.GetTestKey(rand.Int()), utils.RandomValue(128))
assert.Nil(t, err)
}
err = batch.Commit()
assert.Nil(t, err)
var batchId uint64
for i := 0; i < times; i++ {
event := <-w
if i == 0 {
batchId = event.BatchId
}
assert.Equal(t, batchId, event.BatchId)
}
}