-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathautobatch_test.go
251 lines (205 loc) · 5.51 KB
/
autobatch_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package autobatch
import (
"bytes"
"fmt"
"testing"
ds "github.com/ipfs/go-datastore"
dstest "github.com/ipfs/go-datastore/test"
)
func TestAutobatch(t *testing.T) {
dstest.SubtestAll(t, NewAutoBatching(ds.NewMapDatastore(), 16))
}
func TestFlushing(t *testing.T) {
child := ds.NewMapDatastore()
d := NewAutoBatching(child, 16)
var keys []ds.Key
for i := 0; i < 16; i++ {
keys = append(keys, ds.NewKey(fmt.Sprintf("test%d", i)))
}
v := []byte("hello world")
for _, k := range keys {
err := d.Put(k, v)
if err != nil {
t.Fatal(err)
}
}
// Get works normally.
for _, k := range keys {
val, err := d.Get(k)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, v) {
t.Fatal("wrong value")
}
}
// Not flushed
_, err := child.Get(keys[0])
if err != ds.ErrNotFound {
t.Fatal("shouldnt have found value")
}
// Delete works.
err = d.Delete(keys[14])
if err != nil {
t.Fatal(err)
}
_, err = d.Get(keys[14])
if err != ds.ErrNotFound {
t.Fatal(err)
}
// Still not flushed
_, err = child.Get(keys[0])
if err != ds.ErrNotFound {
t.Fatal("shouldnt have found value")
}
// Final put flushes.
err = d.Put(ds.NewKey("test16"), v)
if err != nil {
t.Fatal(err)
}
// should be flushed now, try to get keys from child datastore
for _, k := range keys[:14] {
val, err := child.Get(k)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, v) {
t.Fatal("wrong value")
}
}
// Never flushed the deleted key.
_, err = child.Get(keys[14])
if err != ds.ErrNotFound {
t.Fatal("shouldnt have found value")
}
// Delete doesn't flush
err = d.Delete(keys[0])
if err != nil {
t.Fatal(err)
}
val, err := child.Get(keys[0])
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, v) {
t.Fatal("wrong value")
}
}
func TestSync(t *testing.T) {
child := ds.NewMapDatastore()
d := NewAutoBatching(child, 100)
put := func(key ds.Key) {
if err := d.Put(key, []byte(key.String())); err != nil {
t.Fatal(err)
}
}
del := func(key ds.Key) {
if err := d.Delete(key); err != nil {
t.Fatal(err)
}
}
get := func(d ds.Datastore, key ds.Key) {
val, err := d.Get(key)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(val, []byte(key.String())) {
t.Fatal("wrong value")
}
}
invalidGet := func(d ds.Datastore, key ds.Key) {
if _, err := d.Get(key); err != ds.ErrNotFound {
t.Fatal("should not have found value")
}
}
// Test if Syncing Puts works
internalSyncTest(t, d, child, put, del, get, invalidGet)
// Test if Syncing Deletes works
internalSyncTest(t, d, child, del, put, invalidGet, get)
}
// This function can be used to test Sync Puts and Deletes
// For clarity comments are written as if op = Put and undoOp = Delete
func internalSyncTest(t *testing.T, d, child ds.Datastore, op, undoOp func(ds.Key),
checkOp, checkUndoOp func(ds.Datastore, ds.Key)) {
var keys []ds.Key
keymap := make(map[ds.Key]int)
for i := 0; i < 4; i++ {
k := ds.NewKey(fmt.Sprintf("%d", i))
keymap[k] = len(keys)
keys = append(keys, k)
for j := 0; j < 2; j++ {
k := ds.NewKey(fmt.Sprintf("%d/%d", i, j))
keymap[k] = len(keys)
keys = append(keys, k)
for k := 0; k < 2; k++ {
k := ds.NewKey(fmt.Sprintf("%d/%d/%d", i, j, k))
keymap[k] = len(keys)
keys = append(keys, k)
}
}
}
for _, k := range keys {
op(k)
}
// Get works normally.
for _, k := range keys {
checkOp(d, k)
}
// Put not flushed
checkUndoOp(child, ds.NewKey("0"))
// Delete works.
deletedKey := ds.NewKey("2/1/1")
undoOp(deletedKey)
checkUndoOp(d, deletedKey)
// Put still not flushed
checkUndoOp(child, ds.NewKey("0"))
// Sync the tree "0/*/*"
if err := d.Sync(ds.NewKey("0")); err != nil {
t.Fatal(err)
}
// Try to get keys "0/*/*" from the child datastore
checkKeyRange(t, keymap, keys, d, [][]string{{"0", "0/1/1"}}, checkOp)
// Verify no other keys were synchronized
checkKeyRange(t, keymap, keys, child, [][]string{{"1", "3/1/1"}}, checkUndoOp)
// Sync the tree "1/1/*"
if err := d.Sync(ds.NewKey("1/1")); err != nil {
t.Fatal(err)
}
// Try to get keys "0/*/*" and "1/1/*" from the child datastore
checkKeyRange(t, keymap, keys, d, [][]string{{"0", "0/1/1"}, {"1/1", "1/1/1"}}, checkOp)
// Verify no other keys were synchronized
checkKeyRange(t, keymap, keys, child, [][]string{{"1", "1/0/1"}, {"2", "3/1/1"}}, checkUndoOp)
// Sync the tree "3/1/1"
if err := d.Sync(ds.NewKey("3/1/1")); err != nil {
t.Fatal(err)
}
// Try to get keys "0/*/*", "1/1/*", "3/1/1" from the child datastore
checkKeyRange(t, keymap, keys, d, [][]string{{"0", "0/1/1"}, {"1/1", "1/1/1"}, {"3/1/1", "3/1/1"}}, checkOp)
// Verify no other keys were synchronized
checkKeyRange(t, keymap, keys, child, [][]string{{"1", "1/0/1"}, {"2", "3/1/0"}}, checkUndoOp)
if err := d.Sync(ds.Key{}); err != nil {
t.Fatal(err)
}
// Never flushed the deleted key.
checkUndoOp(child, deletedKey)
// Try to get all keys except the deleted key from the child datastore
checkKeyRange(t, keymap, keys, d, [][]string{{"0", "2/1/0"}, {"3", "3/1/1"}}, checkOp)
// Add the deleted key into the datastore
op(deletedKey)
// Sync it
if err := d.Sync(deletedKey); err != nil {
t.Fatal(err)
}
// Check it
checkOp(d, deletedKey)
}
func checkKeyRange(t *testing.T, keymap map[ds.Key]int, keys []ds.Key,
d ds.Datastore, validKeyRanges [][]string, checkFn func(ds.Datastore, ds.Key)) {
t.Helper()
for _, validKeyBoundaries := range validKeyRanges {
start, end := keymap[ds.NewKey(validKeyBoundaries[0])], keymap[ds.NewKey(validKeyBoundaries[1])]
for _, k := range keys[start:end] {
checkFn(d, k)
}
}
}