@@ -2,6 +2,7 @@ package autobatch
2
2
3
3
import (
4
4
"bytes"
5
+ "context"
5
6
"fmt"
6
7
"testing"
7
8
@@ -14,6 +15,8 @@ func TestAutobatch(t *testing.T) {
14
15
}
15
16
16
17
func TestFlushing (t * testing.T ) {
18
+ ctx := context .Background ()
19
+
17
20
child := ds .NewMapDatastore ()
18
21
d := NewAutoBatching (child , 16 )
19
22
@@ -24,15 +27,15 @@ func TestFlushing(t *testing.T) {
24
27
v := []byte ("hello world" )
25
28
26
29
for _ , k := range keys {
27
- err := d .Put (k , v )
30
+ err := d .Put (ctx , k , v )
28
31
if err != nil {
29
32
t .Fatal (err )
30
33
}
31
34
}
32
35
33
36
// Get works normally.
34
37
for _ , k := range keys {
35
- val , err := d .Get (k )
38
+ val , err := d .Get (ctx , k )
36
39
if err != nil {
37
40
t .Fatal (err )
38
41
}
@@ -43,36 +46,36 @@ func TestFlushing(t *testing.T) {
43
46
}
44
47
45
48
// Not flushed
46
- _ , err := child .Get (keys [0 ])
49
+ _ , err := child .Get (ctx , keys [0 ])
47
50
if err != ds .ErrNotFound {
48
51
t .Fatal ("shouldnt have found value" )
49
52
}
50
53
51
54
// Delete works.
52
- err = d .Delete (keys [14 ])
55
+ err = d .Delete (ctx , keys [14 ])
53
56
if err != nil {
54
57
t .Fatal (err )
55
58
}
56
- _ , err = d .Get (keys [14 ])
59
+ _ , err = d .Get (ctx , keys [14 ])
57
60
if err != ds .ErrNotFound {
58
61
t .Fatal (err )
59
62
}
60
63
61
64
// Still not flushed
62
- _ , err = child .Get (keys [0 ])
65
+ _ , err = child .Get (ctx , keys [0 ])
63
66
if err != ds .ErrNotFound {
64
67
t .Fatal ("shouldnt have found value" )
65
68
}
66
69
67
70
// Final put flushes.
68
- err = d .Put (ds .NewKey ("test16" ), v )
71
+ err = d .Put (ctx , ds .NewKey ("test16" ), v )
69
72
if err != nil {
70
73
t .Fatal (err )
71
74
}
72
75
73
76
// should be flushed now, try to get keys from child datastore
74
77
for _ , k := range keys [:14 ] {
75
- val , err := child .Get (k )
78
+ val , err := child .Get (ctx , k )
76
79
if err != nil {
77
80
t .Fatal (err )
78
81
}
@@ -83,18 +86,18 @@ func TestFlushing(t *testing.T) {
83
86
}
84
87
85
88
// Never flushed the deleted key.
86
- _ , err = child .Get (keys [14 ])
89
+ _ , err = child .Get (ctx , keys [14 ])
87
90
if err != ds .ErrNotFound {
88
91
t .Fatal ("shouldnt have found value" )
89
92
}
90
93
91
94
// Delete doesn't flush
92
- err = d .Delete (keys [0 ])
95
+ err = d .Delete (ctx , keys [0 ])
93
96
if err != nil {
94
97
t .Fatal (err )
95
98
}
96
99
97
- val , err := child .Get (keys [0 ])
100
+ val , err := child .Get (ctx , keys [0 ])
98
101
if err != nil {
99
102
t .Fatal (err )
100
103
}
@@ -105,22 +108,24 @@ func TestFlushing(t *testing.T) {
105
108
}
106
109
107
110
func TestSync (t * testing.T ) {
111
+ ctx := context .Background ()
112
+
108
113
child := ds .NewMapDatastore ()
109
114
d := NewAutoBatching (child , 100 )
110
115
111
116
put := func (key ds.Key ) {
112
- if err := d .Put (key , []byte (key .String ())); err != nil {
117
+ if err := d .Put (ctx , key , []byte (key .String ())); err != nil {
113
118
t .Fatal (err )
114
119
}
115
120
}
116
121
del := func (key ds.Key ) {
117
- if err := d .Delete (key ); err != nil {
122
+ if err := d .Delete (ctx , key ); err != nil {
118
123
t .Fatal (err )
119
124
}
120
125
}
121
126
122
127
get := func (d ds.Datastore , key ds.Key ) {
123
- val , err := d .Get (key )
128
+ val , err := d .Get (ctx , key )
124
129
if err != nil {
125
130
t .Fatal (err )
126
131
}
@@ -130,7 +135,7 @@ func TestSync(t *testing.T) {
130
135
}
131
136
}
132
137
invalidGet := func (d ds.Datastore , key ds.Key ) {
133
- if _ , err := d .Get (key ); err != ds .ErrNotFound {
138
+ if _ , err := d .Get (ctx , key ); err != ds .ErrNotFound {
134
139
t .Fatal ("should not have found value" )
135
140
}
136
141
}
@@ -146,6 +151,9 @@ func TestSync(t *testing.T) {
146
151
// For clarity comments are written as if op = Put and undoOp = Delete
147
152
func internalSyncTest (t * testing.T , d , child ds.Datastore , op , undoOp func (ds.Key ),
148
153
checkOp , checkUndoOp func (ds.Datastore , ds.Key )) {
154
+
155
+ ctx := context .Background ()
156
+
149
157
var keys []ds.Key
150
158
keymap := make (map [ds.Key ]int )
151
159
for i := 0 ; i < 4 ; i ++ {
@@ -185,7 +193,7 @@ func internalSyncTest(t *testing.T, d, child ds.Datastore, op, undoOp func(ds.Ke
185
193
checkUndoOp (child , ds .NewKey ("0" ))
186
194
187
195
// Sync the tree "0/*/*"
188
- if err := d .Sync (ds .NewKey ("0" )); err != nil {
196
+ if err := d .Sync (ctx , ds .NewKey ("0" )); err != nil {
189
197
t .Fatal (err )
190
198
}
191
199
@@ -196,7 +204,7 @@ func internalSyncTest(t *testing.T, d, child ds.Datastore, op, undoOp func(ds.Ke
196
204
checkKeyRange (t , keymap , keys , child , [][]string {{"1" , "3/1/1" }}, checkUndoOp )
197
205
198
206
// Sync the tree "1/1/*"
199
- if err := d .Sync (ds .NewKey ("1/1" )); err != nil {
207
+ if err := d .Sync (ctx , ds .NewKey ("1/1" )); err != nil {
200
208
t .Fatal (err )
201
209
}
202
210
@@ -207,7 +215,7 @@ func internalSyncTest(t *testing.T, d, child ds.Datastore, op, undoOp func(ds.Ke
207
215
checkKeyRange (t , keymap , keys , child , [][]string {{"1" , "1/0/1" }, {"2" , "3/1/1" }}, checkUndoOp )
208
216
209
217
// Sync the tree "3/1/1"
210
- if err := d .Sync (ds .NewKey ("3/1/1" )); err != nil {
218
+ if err := d .Sync (ctx , ds .NewKey ("3/1/1" )); err != nil {
211
219
t .Fatal (err )
212
220
}
213
221
@@ -217,7 +225,7 @@ func internalSyncTest(t *testing.T, d, child ds.Datastore, op, undoOp func(ds.Ke
217
225
// Verify no other keys were synchronized
218
226
checkKeyRange (t , keymap , keys , child , [][]string {{"1" , "1/0/1" }, {"2" , "3/1/0" }}, checkUndoOp )
219
227
220
- if err := d .Sync (ds.Key {}); err != nil {
228
+ if err := d .Sync (ctx , ds.Key {}); err != nil {
221
229
t .Fatal (err )
222
230
}
223
231
@@ -231,7 +239,7 @@ func internalSyncTest(t *testing.T, d, child ds.Datastore, op, undoOp func(ds.Ke
231
239
op (deletedKey )
232
240
233
241
// Sync it
234
- if err := d .Sync (deletedKey ); err != nil {
242
+ if err := d .Sync (ctx , deletedKey ); err != nil {
235
243
t .Fatal (err )
236
244
}
237
245
0 commit comments