Debian Codegraph @

keys_test.go

raw
1 package diskv
2
3 import (
4 "reflect"
5 "runtime"
6 "strings"
7 "testing"
8 )
9
10 var (
11 keysTestData = map[string]string{
12 "ab01cd01": "When we started building CoreOS",
13 "ab01cd02": "we looked at all the various components available to us",
14 "ab01cd03": "re-using the best tools",
15 "ef01gh04": "and building the ones that did not exist",
16 "ef02gh05": "We believe strongly in the Unix philosophy",
17 "xxxxxxxx": "tools should be independently useful",
18 }
19
20 prefixes = []string{
21 "", // all
22 "a",
23 "ab",
24 "ab0",
25 "ab01",
26 "ab01cd0",
27 "ab01cd01",
28 "ab01cd01x", // none
29 "b", // none
30 "b0", // none
31 "0", // none
32 "01", // none
33 "e",
34 "ef",
35 "efx", // none
36 "ef01gh0",
37 "ef01gh04",
38 "ef01gh05",
39 "ef01gh06", // none
40 }
41 )
42
43 func TestKeysFlat(t *testing.T) {
44 transform := func(s string) []string {
45 if s == "" {
46 t.Fatalf(`transform should not be called with ""`)
47 }
48 return []string{}
49 }
50 d := New(Options{
51 BasePath: "test-data",
52 Transform: transform,
53 })
54 defer d.EraseAll()
55
56 for k, v := range keysTestData {
57 d.Write(k, []byte(v))
58 }
59
60 checkKeys(t, d.Keys(nil), keysTestData)
61 }
62
63 func TestKeysNested(t *testing.T) {
64 d := New(Options{
65 BasePath: "test-data",
66 Transform: blockTransform(2),
67 })
68 defer d.EraseAll()
69
70 for k, v := range keysTestData {
71 d.Write(k, []byte(v))
72 }
73
74 checkKeys(t, d.Keys(nil), keysTestData)
75 }
76
77 func TestKeysPrefixFlat(t *testing.T) {
78 d := New(Options{
79 BasePath: "test-data",
80 })
81 defer d.EraseAll()
82
83 for k, v := range keysTestData {
84 d.Write(k, []byte(v))
85 }
86
87 for _, prefix := range prefixes {
88 checkKeys(t, d.KeysPrefix(prefix, nil), filterPrefix(keysTestData, prefix))
89 }
90 }
91
92 func TestKeysPrefixNested(t *testing.T) {
93 d := New(Options{
94 BasePath: "test-data",
95 Transform: blockTransform(2),
96 })
97 defer d.EraseAll()
98
99 for k, v := range keysTestData {
100 d.Write(k, []byte(v))
101 }
102
103 for _, prefix := range prefixes {
104 checkKeys(t, d.KeysPrefix(prefix, nil), filterPrefix(keysTestData, prefix))
105 }
106 }
107
108 func TestKeysCancel(t *testing.T) {
109 d := New(Options{
110 BasePath: "test-data",
111 })
112 defer d.EraseAll()
113
114 for k, v := range keysTestData {
115 d.Write(k, []byte(v))
116 }
117
118 var (
119 cancel = make(chan struct{})
120 received = 0
121 cancelAfter = len(keysTestData) / 2
122 )
123
124 for key := range d.Keys(cancel) {
125 received++
126
127 if received >= cancelAfter {
128 close(cancel)
129 runtime.Gosched() // allow walker to detect cancel
130 }
131
132 t.Logf("received %d: %q", received, key)
133 }
134
135 if want, have := cancelAfter, received; want != have {
136 t.Errorf("want %d, have %d", want, have)
137 }
138 }
139
140 func checkKeys(t *testing.T, c <-chan string, want map[string]string) {
141 for k := range c {
142 if _, ok := want[k]; !ok {
143 t.Errorf("%q yielded but not expected", k)
144 continue
145 }
146
147 delete(want, k)
148 t.Logf("%q yielded OK", k)
149 }
150
151 if len(want) != 0 {
152 t.Errorf("%d expected key(s) not yielded: %s", len(want), strings.Join(flattenKeys(want), ", "))
153 }
154 }
155
156 func blockTransform(blockSize int) func(string) []string {
157 return func(s string) []string {
158 var (
159 sliceSize = len(s) / blockSize
160 pathSlice = make([]string, sliceSize)
161 )
162 for i := 0; i < sliceSize; i++ {
163 from, to := i*blockSize, (i*blockSize)+blockSize
164 pathSlice[i] = s[from:to]
165 }
166 return pathSlice
167 }
168 }
169
170 func filterPrefix(in map[string]string, prefix string) map[string]string {
171 out := map[string]string{}
172 for k, v := range in {
173 if strings.HasPrefix(k, prefix) {
174 out[k] = v
175 }
176 }
177 return out
178 }
179
180 func TestFilterPrefix(t *testing.T) {
181 input := map[string]string{
182 "all": "",
183 "and": "",
184 "at": "",
185 "available": "",
186 "best": "",
187 "building": "",
188 "components": "",
189 "coreos": "",
190 "did": "",
191 "exist": "",
192 "looked": "",
193 "not": "",
194 "ones": "",
195 "re-using": "",
196 "started": "",
197 "that": "",
198 "the": "",
199 "to": "",
200 "tools": "",
201 "us": "",
202 "various": "",
203 "we": "",
204 "when": "",
205 }
206
207 for prefix, want := range map[string]map[string]string{
208 "a": map[string]string{"all": "", "and": "", "at": "", "available": ""},
209 "al": map[string]string{"all": ""},
210 "all": map[string]string{"all": ""},
211 "alll": map[string]string{},
212 "c": map[string]string{"components": "", "coreos": ""},
213 "co": map[string]string{"components": "", "coreos": ""},
214 "com": map[string]string{"components": ""},
215 } {
216 have := filterPrefix(input, prefix)
217 if !reflect.DeepEqual(want, have) {
218 t.Errorf("%q: want %v, have %v", prefix, flattenKeys(want), flattenKeys(have))
219 }
220 }
221 }
222
223 func flattenKeys(m map[string]string) []string {
224 a := make([]string, 0, len(m))
225 for k := range m {
226 a = append(a, k)
227 }
228 return a
229 }