forked from cometbft/cometbft-db
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
120 lines (102 loc) · 3.07 KB
/
util_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
package db
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/require"
)
// Empty iterator for empty db.
func TestPrefixIteratorNoMatchNil(t *testing.T) {
for backend := range backends {
t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
db, dir := newTempDB(t, backend)
defer os.RemoveAll(dir)
itr, err := IteratePrefix(db, []byte("2"))
require.NoError(t, err)
checkInvalid(t, itr)
})
}
}
// Empty iterator for db populated after iterator created.
func TestPrefixIteratorNoMatch1(t *testing.T) {
for backend := range backends {
if backend == BoltDBBackend {
t.Log("bolt does not support concurrent writes while iterating")
continue
}
t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
db, dir := newTempDB(t, backend)
defer os.RemoveAll(dir)
itr, err := IteratePrefix(db, []byte("2"))
require.NoError(t, err)
err = db.SetSync(bz("1"), bz("value_1"))
require.NoError(t, err)
checkInvalid(t, itr)
})
}
}
// Empty iterator for prefix starting after db entry.
func TestPrefixIteratorNoMatch2(t *testing.T) {
for backend := range backends {
t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
db, dir := newTempDB(t, backend)
defer os.RemoveAll(dir)
err := db.SetSync(bz("3"), bz("value_3"))
require.NoError(t, err)
itr, err := IteratePrefix(db, []byte("4"))
require.NoError(t, err)
checkInvalid(t, itr)
})
}
}
// Iterator with single val for db with single val, starting from that val.
func TestPrefixIteratorMatch1(t *testing.T) {
for backend := range backends {
t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
db, dir := newTempDB(t, backend)
defer os.RemoveAll(dir)
err := db.SetSync(bz("2"), bz("value_2"))
require.NoError(t, err)
itr, err := IteratePrefix(db, bz("2"))
require.NoError(t, err)
checkValid(t, itr, true)
checkItem(t, itr, bz("2"), bz("value_2"))
checkNext(t, itr, false)
// Once invalid...
checkInvalid(t, itr)
})
}
}
// Iterator with prefix iterates over everything with same prefix.
func TestPrefixIteratorMatches1N(t *testing.T) {
for backend := range backends {
t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
db, dir := newTempDB(t, backend)
defer os.RemoveAll(dir)
// prefixed
err := db.SetSync(bz("a/1"), bz("value_1"))
require.NoError(t, err)
err = db.SetSync(bz("a/3"), bz("value_3"))
require.NoError(t, err)
// not
err = db.SetSync(bz("b/3"), bz("value_3"))
require.NoError(t, err)
err = db.SetSync(bz("a-3"), bz("value_3"))
require.NoError(t, err)
err = db.SetSync(bz("a.3"), bz("value_3"))
require.NoError(t, err)
err = db.SetSync(bz("abcdefg"), bz("value_3"))
require.NoError(t, err)
itr, err := IteratePrefix(db, bz("a/"))
require.NoError(t, err)
checkValid(t, itr, true)
checkItem(t, itr, bz("a/1"), bz("value_1"))
checkNext(t, itr, true)
checkItem(t, itr, bz("a/3"), bz("value_3"))
// Bad!
checkNext(t, itr, false)
// Once invalid...
checkInvalid(t, itr)
})
}
}