-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathquick_test.go
More file actions
468 lines (419 loc) · 12 KB
/
quick_test.go
File metadata and controls
468 lines (419 loc) · 12 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package statedb
import (
"bytes"
"cmp"
"fmt"
"iter"
"math/rand"
"reflect"
"strings"
"testing"
"testing/quick"
"github.com/cilium/statedb/index"
"github.com/stretchr/testify/require"
)
// Use an object with strings for both primary and secondary
// indexing. With non-unique indexes we'll create a composite
// key out of them by concatenation and thus we need test that
// this does not break iteration order etc.
type quickObj struct {
A, B string
}
// TableHeader implements TableWritable.
func (q quickObj) TableHeader() []string {
return []string{"A", "B"}
}
// TableRow implements TableWritable.
func (q quickObj) TableRow() []string {
return []string{q.A, q.B}
}
func (q quickObj) String() string {
return fmt.Sprintf("%x %x", []byte(q.A), []byte(q.B))
}
var _ TableWritable = quickObj{}
var (
aIndex = Index[quickObj, string]{
Name: "a",
FromObject: func(t quickObj) index.KeySet {
return index.NewKeySet(index.String(t.A))
},
FromKey: index.String,
FromString: index.FromString,
Unique: true,
}
bIndex = Index[quickObj, string]{
Name: "b",
FromObject: func(t quickObj) index.KeySet {
return index.NewKeySet(index.String(t.B))
},
FromKey: index.String,
FromString: index.FromString,
Unique: false,
}
)
func isOrdered(t *testing.T, aFirst bool, it iter.Seq2[quickObj, Revision]) bool {
var prev quickObj
for a := range it {
if aFirst {
if ret := cmp.Compare(a.A, prev.A); ret < 0 {
t.Logf("isOrdered(A): %s < %s!", a, prev)
return false
} else if ret == 0 && cmp.Compare(a.B, prev.B) < 0 {
t.Logf("isOrdered(B): %s < %s!", a, prev)
return false
}
} else {
if ret := cmp.Compare(a.B, prev.B); ret < 0 {
t.Logf("isOrdered(B): %s < %s!", a, prev)
return false
} else if ret == 0 && cmp.Compare(a.A, prev.A) < 0 {
t.Logf("isOrdered(A): %s < %s!", a, prev)
return false
}
}
prev = a
}
return true
}
func seqLen[A, B any](it iter.Seq2[A, B]) int {
n := 0
for range it {
n++
}
return n
}
func TestDB_Quick(t *testing.T) {
t.Parallel()
db := New()
table, err := NewTable(db, "test", aIndex, bIndex)
require.NoError(t, err, "NewTable")
anyTable := AnyTable{table}
values := map[string]string{}
pickRandom := func() string {
if len(values) == 0 || rand.Intn(10) == 0 {
return "nonexisting"
}
n := rand.Intn(len(values))
for a := range values {
if n <= 0 {
return a
}
n--
}
return ""
}
numInserted, numRemoved := 0, 0
check := func(a, b string, remove bool, useModify bool) bool {
txn := db.WriteTxn(table)
if remove {
key := pickRandom()
expected, found := values[key]
actual, hadOld, err := table.Delete(txn, quickObj{A: key})
if err != nil {
t.Logf("delete error: %s", err)
return false
}
if found {
if !hadOld {
t.Logf("object was inserted but not found when removing")
return false
}
if actual.B != expected {
t.Logf("removed object value mismatch %q vs %q", expected, actual.B)
}
delete(values, key)
numRemoved++
} else {
if hadOld {
t.Logf("object was removed when it should have not existed")
return false
}
}
txn.Commit()
return true
}
getObj, _, getWatch, getFound := table.GetWatch(txn, aIndex.Query(a))
expected, found := values[a]
expectedB := b
var (
old quickObj
hadOld bool
err error
)
if useModify {
old, hadOld, err = table.Modify(txn, quickObj{a, b}, func(old, new quickObj) quickObj {
new.B = old.B + new.B
return new
})
require.NoError(t, err, "Modify")
if found {
expectedB = expected + b
}
} else {
old, hadOld, err = table.Insert(txn, quickObj{a, b})
require.NoError(t, err, "Insert")
}
numInserted++
require.Equal(t, getFound, hadOld)
if found {
if !hadOld {
t.Logf("object was updated but old value not returned")
return false
}
if old.B != expected {
t.Logf("insert returned wrong old object, %q vs %q", expected, old.B)
return false
}
} else {
if hadOld {
t.Logf("insert returned old value when it should not have existed")
return false
}
}
values[a] = expectedB
if len(values) != table.NumObjects(txn) {
t.Logf("wrong object count")
return false
}
txn.Commit()
if hadOld {
select {
case <-getWatch:
default:
t.Logf("get watch channel not closed after insert")
return false
}
require.Equal(t, old, getObj)
}
rtxn := db.ReadTxn()
if len(values) != table.NumObjects(rtxn) {
t.Logf("wrong object count")
return false
}
//
// Check queries against the primary index
//
if len(values) != seqLen(table.All(rtxn)) {
t.Logf("All() via aIndex wrong length")
return false
}
if len(values) != seqLen(table.Prefix(rtxn, aIndex.Query(""))) {
t.Logf("Prefix() via aIndex wrong length")
return false
}
if len(values) != seqLen(table.LowerBound(rtxn, aIndex.Query(""))) {
t.Logf("LowerBound() via aIndex wrong length")
return false
}
obj, _, found := table.Get(rtxn, aIndex.Query(a))
if !found || obj.A != a {
t.Logf("Get() via aIndex")
return false
}
for obj := range table.Prefix(rtxn, aIndex.Query(a)) {
if !strings.HasPrefix(obj.A, a) {
t.Logf("Prefix() returned object with wrong prefix via aIndex")
return false
}
}
anyObjs, err := anyTable.Prefix(rtxn, "a", a)
require.NoError(t, err, "AnyTable.Prefix")
for anyObj := range anyObjs {
obj := anyObj.(quickObj)
if !strings.HasPrefix(obj.A, a) {
t.Logf("AnyTable.Prefix() returned object with wrong prefix via aIndex")
return false
}
}
for obj := range table.LowerBound(rtxn, aIndex.Query(a)) {
if cmp.Compare(obj.A, a) < 0 {
t.Logf("LowerBound() order wrong")
return false
}
}
anyObjs, err = anyTable.LowerBound(rtxn, "a", a)
require.NoError(t, err, "AnyTable.LowerBound")
for anyObj := range anyObjs {
obj := anyObj.(quickObj)
if cmp.Compare(obj.A, a) < 0 {
t.Logf("AnyTable.LowerBound(%x) order wrong: %x < %x", []byte(a), []byte(obj.A), []byte(a))
return false
}
}
if !isOrdered(t, true, table.All(rtxn)) {
t.Logf("All() wrong order")
return false
}
if !isOrdered(t, true, table.Prefix(rtxn, aIndex.Query(""))) {
t.Logf("Prefix() via aIndex wrong order")
return false
}
if !isOrdered(t, true, table.LowerBound(rtxn, aIndex.Query(""))) {
t.Logf("LowerBound() via aIndex wrong order")
return false
}
//
// Check against the secondary (non-unique index)
//
queryB := expectedB
// Non-unique indexes return the same number of objects as we've inserted.
if len(values) != seqLen(table.Prefix(rtxn, bIndex.Query(""))) {
t.Logf("Prefix() via bIndex wrong length")
return false
}
if len(values) != seqLen(table.LowerBound(rtxn, bIndex.Query(""))) {
t.Logf("LowerBound() via bIndex wrong length")
return false
}
// Get returns the first match, but since the index is non-unique, this might
// not be the one that we just inserted.
obj, _, found = table.Get(rtxn, bIndex.Query(queryB))
if !found || obj.B != queryB {
t.Logf("Get(%q) via bIndex not found (%v) or wrong B (%q vs %q)", queryB, found, obj.B, queryB)
return false
}
found = false
for obj := range table.List(rtxn, bIndex.Query(queryB)) {
if obj.B != queryB {
t.Logf("List() via bIndex wrong B")
return false
}
if obj.A == a {
found = true
}
}
if !found {
t.Logf("List() via bIndex, object not found")
return false
}
visited := map[string]struct{}{}
for obj := range table.Prefix(rtxn, bIndex.Query(queryB)) {
if !strings.HasPrefix(obj.B, queryB) {
t.Logf("Prefix() via bIndex has wrong prefix")
return false
}
if _, found := visited[obj.A]; found {
t.Logf("Prefix() visited object %q twice", obj.A)
return false
}
visited[obj.A] = struct{}{}
}
anyObjs, err = anyTable.Prefix(rtxn, "b", queryB)
require.NoError(t, err, "AnyTable.Prefix")
for anyObj := range anyObjs {
obj := anyObj.(quickObj)
if !strings.HasPrefix(obj.B, queryB) {
t.Logf("AnyTable.Prefix() via bIndex has wrong prefix: %q vs %q", obj.B, queryB)
return false
}
}
visited = map[string]struct{}{}
for obj := range table.LowerBound(rtxn, bIndex.Query(queryB)) {
if cmp.Compare(obj.B, queryB) < 0 {
t.Logf("LowerBound() via bIndex has wrong objects, expected %v >= %v", []byte(obj.B), []byte(queryB))
return false
}
if _, found := visited[obj.A]; found {
t.Logf("Prefix() visited object %q twice", obj.A)
return false
}
visited[obj.A] = struct{}{}
}
anyObjs, err = anyTable.LowerBound(rtxn, "b", queryB)
require.NoError(t, err, "AnyTable.LowerBound")
for anyObj := range anyObjs {
obj := anyObj.(quickObj)
if cmp.Compare(obj.B, queryB) < 0 {
t.Logf("AnyTable.LowerBound() via bIndex has wrong objects, expected %v >= %v", []byte(obj.B), []byte(queryB))
return false
}
}
// Iterating over the secondary index returns the objects in order
// defined by the "B" key first and then by the "A" key.
if !isOrdered(t, false, table.Prefix(rtxn, bIndex.Query(""))) {
t.Logf("Prefix() via bIndex wrong order")
return false
}
if !isOrdered(t, false, table.LowerBound(rtxn, bIndex.Query(""))) {
t.Logf("Prefix() via bIndex wrong order")
return false
}
return true
}
require.NoError(t, quick.Check(check, &quick.Config{
MaxCount: 5000,
// Make 1-8 byte long strings as input data. Keep the strings shorter
// than the default quick value generation to hit the more interesting cases
// often.
Values: func(args []reflect.Value, rand *rand.Rand) {
if len(args) != 4 {
panic("unexpected args count")
}
for i := range args[:2] {
numBytes := 1 + rand.Intn(8)
bs := make([]byte, numBytes)
rand.Read(bs)
args[i] = reflect.ValueOf(string(bs))
}
// Remove 33% of the time
args[2] = reflect.ValueOf(rand.Intn(3) == 1)
// Use Modify 50% of the time
args[3] = reflect.ValueOf(rand.Intn(2) == 0)
},
}))
t.Logf("%d objects after check, %d inserts, %d removals", len(values), numInserted, numRemoved)
}
func Test_Quick_nonUniqueKey(t *testing.T) {
t.Parallel()
check := func(p1, s1, p2, s2 []byte) bool {
key1 := encodeNonUniqueKey(p1, s1)
expectedLen := encodedLength(p1) + 1 + encodedLength(s1) + 2
minLen := len(p1) + 1 + len(s1) + 2
if expectedLen < minLen {
t.Logf("expected length too short (%d), must be >= %d", expectedLen, minLen)
return false
}
if len(key1) != expectedLen {
t.Logf("length mismatch, expected %d, got %d", expectedLen, len(key1))
return false
}
nuk1 := nonUniqueKey(key1)
if len(nuk1.encodedPrimary()) < len(p1) {
t.Logf("encodedPrimary() length (%d) shorter than original (%d)", len(nuk1.encodedPrimary()), len(p1))
return false
}
if len(nuk1.encodedPrimary()) != encodedLength(p1) {
t.Logf("encodedPrimary() length (%d) does not match encodedLength() (%d)", len(nuk1.encodedPrimary()), len(p1))
return false
}
if len(nuk1.encodedSecondary()) < len(s1) {
t.Logf("encodedSecondary() length (%d) shorter than original (%d)", len(nuk1.encodedSecondary()), len(s1))
}
if len(nuk1.encodedSecondary()) != encodedLength(s1) {
t.Logf("encodedSecondary() length (%d) does not match encodedLength() (%d)", len(nuk1.encodedSecondary()), len(s1))
return false
}
// Do another key and check that ordering is preserved.
key2 := encodeNonUniqueKey(p2, s2)
scmp := bytes.Compare(s1, s2)
pcmp := bytes.Compare(p1, p2)
kcmp := bytes.Compare(key1, key2)
if (scmp == 0 && pcmp != kcmp) /* secondary key matches, primary key determines order */ ||
(scmp != 0 && scmp != kcmp) /* secondary key determines order */ {
t.Logf("ordering not preserved: key1=%v key2=%v, p1=%v, s1=%v, p2=%v, s2=%v", key1, key2, p1, s1, p2, s2)
return false
}
return true
}
require.NoError(t, quick.Check(check, &quick.Config{
MaxCount: 50000,
Values: func(args []reflect.Value, rand *rand.Rand) {
for i := range args {
numBytes := 1 + rand.Intn(8)
bs := make([]byte, numBytes)
rand.Read(bs)
args[i] = reflect.ValueOf(bs)
}
},
}))
}