-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogstore_test.go
328 lines (278 loc) · 7.4 KB
/
logstore_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
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
package main
import (
"bufio"
"bytes"
"io/ioutil"
"log"
"math/rand"
"os"
"path"
"path/filepath"
"testing"
"time"
)
// Use fixtures to test getting values from a loaded log file
func TestGet(t *testing.T) {
dir, err := filepath.Abs(path.Join("test_dbs", "basic"))
if err != nil {
panic(err)
}
logStore, err := CreateLogStore(dir, nil)
if err != nil {
t.Errorf("couldn't create log store %s", err)
return
}
var b bytes.Buffer
w := bufio.NewWriter(&b)
k := "a"
v := "b"
found, err := logStore.StreamGet(k, w)
if found != true && err != nil {
t.Errorf("for \"%s\", found should be true and err should be nil: %s", k, err)
}
if b.String() != v {
t.Errorf("got \"%s\"; want \"%s\"", b.String(), v)
}
b.Reset()
k = "c"
v = "dd"
found, err = logStore.StreamGet(k, w)
if found != true && err != nil {
t.Errorf("for \"%s\", found should be true and err should be nil: %s", k, err)
}
if b.String() != v {
t.Errorf("got \"%s\"; want \"%s\"", b.String(), v)
}
b.Reset()
k = "e"
v = "f"
found, err = logStore.StreamGet(k, w)
if found != true && err != nil {
t.Errorf("for \"%s\", found should be true and err should be nil: %s", k, err)
}
if b.String() != v {
t.Errorf("got \"%s\"; want \"%s\"", b.String(), v)
}
}
// Use fixtures to test (not) getting expired values from a loaded log file
func TestGetExpired(t *testing.T) {
dir, err := filepath.Abs(path.Join("test_dbs", "basic"))
if err != nil {
log.Fatal(err)
}
logStore, err := CreateLogStore(dir, nil)
if err != nil {
t.Errorf("couldn't create log store %s", err)
return
}
var b bytes.Buffer
w := bufio.NewWriter(&b)
k := "x"
found, err := logStore.StreamGet(k, w)
if found != false && err != nil {
t.Errorf("for \"%s\", found should be false and err should be nil: %s", k, err)
}
if len(b.String()) != 0 {
t.Errorf("for \"%s\", bytes shouldn't be written", k)
}
}
// Use fixtures to test (not) getting overwritten/deleted values from a loaded log file
func TestGetExpiredLogFileOverwrite(t *testing.T) {
dir, err := filepath.Abs(path.Join("test_dbs", "basic"))
if err != nil {
log.Fatal(err)
}
logStore, err := CreateLogStore(dir, nil)
if err != nil {
t.Errorf("couldn't create log store %s", err)
return
}
var b bytes.Buffer
w := bufio.NewWriter(&b)
k := "q"
found, err := logStore.StreamGet(k, w)
if found != false && err != nil {
t.Errorf("for \"%s\", found should be false and err should be nil: %s", k, err)
}
if len(b.String()) != 0 {
t.Errorf("for \"%s\", bytes shouldn't be written", k)
}
}
// Test setting/getting in an active log file
func TestSet(t *testing.T) {
tempDir, err := ioutil.TempDir("test_dbs/temp", "testset")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)
dir, err := filepath.Abs(tempDir)
if err != nil {
log.Fatal(err)
}
logStore, err := CreateLogStore(dir, nil)
if err != nil {
t.Errorf("couldn't create log store %s", err)
return
}
// Add item
k := "n"
v := "m"
err = logStore.Set(k, int(time.Now().UnixMilli())+1000, []byte(v))
if err != nil {
t.Errorf("couldn't set key \"%s\" %s", k, err)
}
var b bytes.Buffer
w := bufio.NewWriter(&b)
// Check add was successful
found, err := logStore.StreamGet(k, w)
if found != true && err != nil {
t.Errorf("for \"%s\", found should be true and err should be nil: %s", k, err)
}
if b.String() != v {
t.Errorf("got \"%s\"; want \"%s\"", b.String(), v)
}
b.Reset()
// Overwrite existing item
k = "n"
v = "mm"
err = logStore.Set(k, int(time.Now().UnixMilli())+1000, []byte(v))
if err != nil {
t.Errorf("couldn't set key \"%s\" for the second time %s", k, err)
}
// Check overwrite was successful
found, err = logStore.StreamGet(k, w)
if found != true && err != nil {
t.Errorf("for \"%s\", found should be true and err should be nil: %s", k, err)
}
if b.String() != v {
t.Errorf("got \"%s\"; want \"%s\"", b.String(), v)
}
b.Reset()
// Add already expired item
k = "ex"
v = "1"
err = logStore.Set(k, int(time.Now().UnixMilli())-1000, []byte("1"))
if err != nil {
t.Errorf("couldn't set key \"%s\" %s", k, err)
}
// Check expired item isn't found
found, err = logStore.StreamGet(k, w)
if found != false && err != nil {
t.Errorf("for \"%s\", found should be false and err should be nil: %s", k, err)
}
if len(b.String()) != 0 {
t.Errorf("not found items should write to writer")
}
}
// Test log files are rolled correctly
func TestRollingLogFile(t *testing.T) {
tempDir, err := ioutil.TempDir("test_dbs/temp", "testrollinglogfile")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)
dir, err := filepath.Abs(tempDir)
if err != nil {
log.Fatal(err)
}
// Use a small max bytes to check rolling the log file works correctly
logStore, err := CreateLogStore(dir, &LogStoreOptions{maxLogFileBytes: 32})
if err != nil {
t.Errorf("couldn't create log store %s", err)
return
}
// Add to the first log file
k1 := "a"
v1 := "________________1"
err = logStore.Set(k1, int(time.Now().UnixMilli())+1000, []byte(v1))
if err != nil {
t.Errorf("couldn't set key \"%s\" %s", k1, err)
}
// Add to the second log file
k2 := "b"
v2 := "________________2"
err = logStore.Set(k2, int(time.Now().UnixMilli())+1000, []byte(v2))
if err != nil {
t.Errorf("couldn't set key \"%s\" %s", k2, err)
}
var b bytes.Buffer
w := bufio.NewWriter(&b)
// Check adds were successful
found, err := logStore.StreamGet(k1, w)
if found != true && err != nil {
t.Errorf("for \"%s\", found should be true and err should be nil: %s", k1, err)
}
if b.String() != v1 {
t.Errorf("got \"%s\"; want \"%s\"", b.String(), v1)
}
b.Reset()
found, err = logStore.StreamGet(k2, w)
if found != true && err != nil {
t.Errorf("for \"%s\", found should be true and err should be nil: %s", k2, err)
}
if b.String() != v2 {
t.Errorf("got \"%s\"; want \"%s\"", b.String(), v2)
}
}
// Test writing and loading a log file for correctness
func TestLoadLogFile(t *testing.T) {
tempDir, err := ioutil.TempDir("test_dbs/temp", "testloadlogfile")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tempDir)
dir, err := filepath.Abs(tempDir)
if err != nil {
log.Fatal(err)
}
logStore, err := CreateLogStore(dir, nil)
if err != nil {
t.Errorf("couldn't create log store %s", err)
return
}
keys := make([][]byte, 0)
values := make([][]byte, 0)
// Cover edge case of empty values
k := []byte("a")
keys = append(keys, k)
v := []byte("")
values = append(values, v)
logStore.Set(string(k), int(time.Now().UnixMilli())+100000, v)
// Empty keys too
k = []byte("")
keys = append(keys, k)
v = []byte("zz")
values = append(values, v)
logStore.Set(string(k), int(time.Now().UnixMilli())+100000, v)
// Add some random items
for i := 0; i < 8; i++ {
k := rndFileString(16)
keys = append(keys, k)
v := rndData(256)
values = append(values, v)
logStore.Set(string(k), int(time.Now().UnixMilli())+100000, v)
}
logStore2, err := CreateLogStore(dir, nil)
if err != nil {
t.Errorf("couldn't create log store %s", err)
return
}
for i := range keys {
k := keys[i]
v := values[i]
var b bytes.Buffer
w := bufio.NewWriter(&b)
found, err := logStore2.StreamGet(string(k), w)
if found != true && err != nil {
t.Errorf("for \"%s\", found should be true and err should be nil: %s", k, err)
}
if !bytes.Equal(b.Bytes(), v) {
t.Errorf("got \"%s\"; want \"%s\"", b.String(), v)
}
}
}
func rndData(length int) []byte {
data := make([]byte, length)
rand.Read(data)
return data
}