-
Notifications
You must be signed in to change notification settings - Fork 6
/
writer_test.go
345 lines (257 loc) · 6.3 KB
/
writer_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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package cellar
import (
"crypto/rand"
fmt "fmt"
"io"
"log"
rnd "math/rand"
"testing"
"time"
)
func genRandBytes(size int) []byte {
key := make([]byte, size)
var err error
if _, err = io.ReadFull(rand.Reader, key); err != nil {
panic(err)
}
return key
}
func genSeedBytes(size int, seed int) []byte {
buf := make([]byte, size)
for i := 0; i < size; i++ {
buf[i] = byte((i + seed) % 256)
}
return buf
}
func checkSeedBytes(data []byte, seed int) error {
for i := 0; i < len(data); i++ {
expect := byte((i + seed) % 256)
if data[i] != expect {
return fmt.Errorf("Given seed %d expected %d at position %d but got %d", seed, expect, i, data[i])
}
}
return nil
}
func TestWithClosing(t *testing.T) {
var w *Writer
var err error
folder := getFolder()
key := genRandBytes(16)
w, err = NewWriter(folder, 1000, key)
defer closeWriter(t, w)
assert(t, err, "NewWriter")
var valuesWritten int
var k int
for j := 0; j < 5; j++ {
for i := 0; i < 30; i++ {
valuesWritten += 64
if _, err = w.Append(genSeedBytes(64, k)); err != nil {
t.Fatalf("Append failed: %s", err)
}
k++
if k%17 == 0 {
assertCheckpoint(t, w)
}
}
assertCheckpoint(t, w)
w.Checkpoint()
err = w.Close()
assert(t, err, "Closing")
w, err = NewWriter(folder, 1000, key)
assert(t, err, "Opening writer")
}
reader := NewReader(folder, key)
var valuesRead int
var n int
err = reader.Scan(func(pos *ReaderInfo, s []byte) error {
if err := checkSeedBytes(s, n); err != nil {
t.Fatalf("Failed seed check: %s", err)
}
valuesRead += len(s)
n++
return nil
})
assert(t, err, "ReadAll")
if valuesRead != valuesWritten {
t.Fatalf("Expected to read %d bytes but read %d", valuesWritten, valuesRead)
}
}
func closeWriter(t *testing.T, w *Writer) {
err := w.Close()
if err != nil {
t.Fatalf("Failed to close the writer %s", err)
}
}
func assertCheckpoint(t *testing.T, w *Writer) {
_, err := w.Checkpoint()
if err != nil {
t.Fatalf("Failed to checkpoint %s", err)
}
}
func TestUserCheckpoints(t *testing.T) {
var (
w *Writer
err error
pos int64
)
folder := getFolder()
key := genRandBytes(16)
w, err = NewWriter(folder, 1000, key)
defer closeWriter(t, w)
assert(t, err, "NewWriter")
pos, err = w.GetUserCheckpoint("custom")
assert(t, err, "GetCheckpoint")
if pos != 0 {
t.Fatal("Checkpoint should be 0")
}
assert(t, w.PutUserCheckpoint("custom", 42), "PutCheckpoint")
pos, err = w.GetUserCheckpoint("custom")
assert(t, err, "GetCheckpoint")
if pos != 42 {
t.Fatal("Checkpoint should be 42")
}
}
func TestSingleChunkDB(t *testing.T) {
log.Print("Starting single chunk")
defer log.Print("Single chunk over")
var w *Writer
var err error
folder := getFolder()
key := genRandBytes(16)
w, err = NewWriter(folder, 1000, key)
defer closeWriter(t, w)
assert(t, err, "NewWriter")
var valuesWritten int
for i := 0; i < 2; i++ {
valuesWritten += 64
if _, err = w.Append(genSeedBytes(64, i)); err != nil {
t.Fatalf("Append failed: %s", err)
}
}
assertCheckpoint(t, w)
w.Close()
var valuesRead int
var n int
reader := NewReader(folder, key)
err = reader.Scan(func(pos *ReaderInfo, s []byte) error {
if err := checkSeedBytes(s, n); err != nil {
t.Fatalf("Failed seed check: %s", err)
}
valuesRead += len(s)
n++
return nil
})
assert(t, err, "ReadAll")
if valuesRead != valuesWritten {
t.Fatalf("Expected to read %d bytes but read %d", valuesWritten, valuesRead)
}
}
func TestReadingWithOffset(t *testing.T) {
}
func TestSimpleKey(t *testing.T) {
var w *Writer
var err error
folder := getFolder()
key := genRandBytes(16)
w, err = NewWriter(folder, 1000, key)
defer closeWriter(t, w)
assert(t, err, "NewWriter")
var valuesWritten int
for i := 0; i < 30; i++ {
valuesWritten += 64
if _, err = w.Append(genSeedBytes(64, i)); err != nil {
t.Fatalf("Append failed: %s", err)
}
}
assertCheckpoint(t, w)
reader := NewReader(folder, key)
var valuesRead int
var n int
err = reader.Scan(func(pos *ReaderInfo, s []byte) error {
if err := checkSeedBytes(s, n); err != nil {
t.Fatalf("Failed seed check: %s", err)
}
valuesRead += len(s)
n++
return nil
})
assert(t, err, "ReadAll")
if valuesRead != valuesWritten {
t.Fatalf("Expected to read %d bytes but read %d", valuesWritten, valuesRead)
}
}
type rec struct {
pos int64
seed int
size int
}
func TestFuzz(t *testing.T) {
seed := time.Now().UnixNano()
r := rnd.New(rnd.NewSource(seed))
folder := getFolder()
maxIterations := 1000
maxValueLength := r.Intn(1024*128) + 10
maxBufferSize := r.Intn(maxValueLength*maxIterations/2) + 1
key := genRandBytes(16)
t.Logf("maxVal %d; maxBuffer %d; seed %d", maxValueLength, maxBufferSize, seed)
var writer *Writer
var err error
var recs []rec
for i := 0; i <= maxIterations; i++ {
if r.Intn(17) == 13 || i == maxIterations {
if writer != nil {
assertCheckpoint(t, writer)
writer.Checkpoint()
err = writer.Close()
assert(t, err, "Closing writer")
writer = nil
}
recordsSaved := len(recs)
reader := NewReader(folder, key)
recordPos := 0
if r.Intn(5) > 2 && recordsSaved > 0 {
// pick a random pos to scan from
recordPos = r.Intn(recordsSaved)
}
r := recs[recordPos]
reader.StartPos = r.pos
scanSeed := r.seed
var bytesRead int
var recordsRead int
var bytesWritten int
var recordsWritten int
for i := recordPos; i < recordsSaved; i++ {
bytesWritten += recs[i].size
recordsWritten++
}
reader.Scan(func(p *ReaderInfo, b []byte) error {
bytesRead += len(b)
recordsRead++
if err := checkSeedBytes(b, scanSeed); err != nil {
t.Fatalf("Failed to verify data: %s", err)
}
scanSeed++
return nil
})
if bytesWritten != bytesRead {
t.Fatalf("Written %d bytes but read %d bytes from %d (%d). Records: %d, %d", bytesWritten, bytesRead, reader.StartPos, bytesRead+int(reader.StartPos), recordsWritten, recordsRead)
}
}
if writer == nil {
writer, err = NewWriter(folder, int64(maxBufferSize), key)
assert(t, err, "new writer")
}
valSize := r.Intn(maxValueLength)
val := genSeedBytes(valSize, i)
pos := writer.VolatilePos()
_, err = writer.Append(val)
recs = append(recs, rec{
pos: pos,
seed: i,
size: valSize,
})
if err != nil {
assert(t, err, "append")
}
}
}