-
Notifications
You must be signed in to change notification settings - Fork 1
/
logfile_test.go
156 lines (132 loc) · 3.01 KB
/
logfile_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
package golbat
import (
"bytes"
"io"
"io/ioutil"
"math"
"os"
"testing"
"github.com/neotse/golbat/internel"
"github.com/stretchr/testify/require"
)
func TestHeader(t *testing.T) {
h := header{
klen: 1,
vlen: 1,
recordType: byte(Value),
}
res := make([]byte, maxHeaderSize)
len := h.Encode(res)
require.Equal(t, 3, len)
h2 := h
require.Equal(t, 3, h.Decode(res))
require.EqualValues(t, h2, h)
h = header{
klen: math.MaxUint16,
vlen: math.MaxUint16,
recordType: byte(Delete),
}
res = make([]byte, maxHeaderSize)
len = h.Encode(res)
require.Equal(t, 7, len)
h2 = h
require.Equal(t, 7, h.Decode(res))
require.EqualValues(t, h2, h)
h = header{
klen: math.MaxUint32,
vlen: math.MaxUint32,
recordType: byte(ValPtr),
}
res = make([]byte, maxHeaderSize)
len = h.Encode(res)
require.Equal(t, maxHeaderSize, len)
h2 = h
require.Equal(t, maxHeaderSize, h.Decode(res))
require.EqualValues(t, h2, h)
reader := bytes.NewReader(res)
hashReader := internel.NewHashReader(reader)
len, _ = h.decodeFrom(hashReader)
require.Equal(t, maxHeaderSize, len)
require.EqualValues(t, h2, h)
}
func TestValPtr(t *testing.T) {
v := valPtr{
fid: 1,
len: 10,
offset: 100,
}
b := v.Encode()
v2 := v
v.Decode(b)
require.EqualValues(t, v2, v)
}
const logSize = 1024 * 4 // 4KB
func TestLogFile(t *testing.T) {
dir, _ := ioutil.TempDir("", "golbat-test")
defer removeDir(dir)
f, _ := ioutil.TempFile(dir, "logfile")
var log logFile
buf := &bytes.Buffer{}
log.Open(f.Name(), os.O_CREATE|os.O_RDWR, logSize)
defer log.Close()
const k = "key1"
const v = "golbat-test: logfile"
// write a entry
e := &entry{
key: []byte(k),
value: []byte(v),
rtype: Value,
}
log.WriteEntry(e, buf)
// read back
e2, _ := log.ReadEntry(log.Data, 0)
require.EqualValues(t, k, string(e2.key))
require.EqualValues(t, v, string(e2.value))
require.EqualValues(t, Value, e2.rtype)
// write more
sz := log.pos
end := sz * 101
for i := 0; i < 100; i++ {
log.WriteEntry(e, buf)
}
require.Equal(t, end, log.pos)
// iterate and check every entry
w := func(entry *entry, _ valPtr) error {
require.EqualValues(t, e2.key, entry.key)
require.EqualValues(t, e2.value, entry.value)
require.EqualValues(t, e2.hlen, entry.hlen)
require.EqualValues(t, e2.rtype, entry.rtype)
return nil
}
n, err := log.iterate(0, w)
require.Equal(t, end, n)
require.Nil(t, err)
// flush
log.Flush(end)
fs, _ := f.Stat()
require.True(t, fs.Size() >= int64(end))
// read by valptr
vp := valPtr{
fid: 0,
len: sz,
offset: 50 * sz,
}
bs, err := log.readWithValPtr(vp)
require.Nil(t, err)
e3, _ := log.ReadEntry(bs, 0)
require.EqualValues(t, e2, e3)
vp.offset = end
_, err = log.readWithValPtr(vp)
require.NotNil(t, err)
require.ErrorIs(t, err, io.EOF)
vp.offset = end - sz
vp.len = sz + 1
_, err = log.readWithValPtr(vp)
require.NotNil(t, err)
require.ErrorIs(t, err, io.EOF)
}
func removeDir(dir string) {
if err := os.RemoveAll(dir); err != nil {
panic(err)
}
}