-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuffer_test.go
105 lines (75 loc) · 1.78 KB
/
buffer_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
package cellar
import (
"os"
"path"
"testing"
)
func TestWrites(t *testing.T) {
folder := getFolder()
b := &BufferDto{
FileName: "temp",
MaxBytes: 100000,
}
var (
buf *Buffer
err error
)
if buf, err = openBuffer(b, folder); err != nil {
panic(err)
}
buf.writeBytes(makeSlice(1))
assertExists(t, path.Join(folder, "temp"))
assertPos(t, buf, 1)
assert(t, buf.writeBytes(make([]byte, 10)), "writeBytes")
assertPos(t, buf, 11)
assert(t, buf.flush(), "flush")
assertPos(t, buf, 11)
err = buf.writeBytes(make([]byte, 10))
assert(t, err, "writeBytes")
assertPos(t, buf, 21)
}
func assert(t *testing.T, err error, op string) {
if err != nil {
t.Fatalf("Failed %s: %s", op, err)
}
}
func TestExist(t *testing.T) {
folder := getFolder()
b := &BufferDto{
FileName: "temp",
MaxBytes: 100000,
}
var buf *Buffer
var err error
buf, err = openBuffer(b, folder)
assert(t, err, "openBuffer")
assert(t, buf.writeBytes(makeSlice(1)), "writeVarInt")
assertPos(t, buf, 1)
assert(t, buf.writeBytes(make([]byte, 10)), "writeBytes")
assertExists(t, path.Join(folder, "temp"))
buf.endRecord()
key := []byte("example key 1234")
var chunk *ChunkDto
chunk, err = buf.compress(key)
assert(t, err, "compress")
assertExists(t, path.Join(folder, chunk.FileName))
if chunk.UncompressedByteSize != 11 {
t.Fatalf("chunk size should match")
}
if chunk.Records != 1 {
t.Fatalf("Chunk should have %d records", 1)
}
if chunk.StartPos != 0 {
t.Fatalf("Chunk start pos should be %d", 0)
}
}
func assertPos(t *testing.T, b *Buffer, expected int64) {
if b.pos != expected {
t.Fatalf("Expected pos to be %d but got %d", expected, b.pos)
}
}
func assertExists(t *testing.T, path string) {
if _, e := os.Stat(path); e != nil {
t.Fatal("Buffer files should exist")
}
}