-
Notifications
You must be signed in to change notification settings - Fork 0
/
wal_test.go
225 lines (187 loc) · 4.52 KB
/
wal_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
package wal
import (
"strconv"
"testing"
"github.com/matryer/is"
)
type testEntry []byte
func (t *testEntry) Marshal() (data []byte, err error) {
return *t, nil
}
func (t *testEntry) Unmarshal(_data []byte) error {
*t = _data
return nil
}
func createTestWal(is *is.I, _dir string, _split int64, _size int64) Wal {
w, err := Open(_dir+"test", _split, _size)
is.NoErr(err)
return w
}
func createTestWalFilled(is *is.I, _dir string, _size int64) Wal {
w := createTestWal(is, _dir, 4, _size)
for i := int64(0); i < _size*2; i++ {
e := []byte("this is pre filled test data: " + strconv.FormatInt(i+1, 10))
_, err := w.Write(e)
is.NoErr(err)
}
return w
}
func TestWalOpen(t *testing.T) {
is := is.New(t)
dir := "tmp/walopen/"
defer cleanup(dir)
prepare(dir)
w, err := Open(dir+"test", 4, 20)
is.NoErr(err)
is.True(w != nil)
is.Equal(w.Name(), dir+"test")
err = w.Close()
is.NoErr(err)
}
func TestWalWrite(t *testing.T) {
is := is.New(t)
dir := "tmp/walwrite/"
defer cleanup(dir)
prepare(dir)
_size := int64(20)
w := createTestWal(is, dir, 4, _size)
defer w.Close()
for i := int64(0); i < _size; i++ {
e := []byte("this is test data: " + strconv.FormatInt(i+1, 10))
seqNum, err := w.Write(e)
is.NoErr(err)
is.True(seqNum > 0)
is.Equal(seqNum, uint64(i+1))
}
is.Equal(w.SeqNum(), uint64(_size))
}
func TestWalSync(t *testing.T) {
is := is.New(t)
dir := "tmp/walsync/"
defer cleanup(dir)
prepare(dir)
_size := int64(20)
w := createTestWalFilled(is, dir, _size)
defer w.Close()
seqNum, err := w.Sync()
is.NoErr(err)
is.Equal(seqNum, uint64(40))
safeSeqNum := w.Safe()
is.Equal(safeSeqNum, uint64(40))
}
func TestWalReadAt(t *testing.T) {
is := is.New(t)
dir := "tmp/walread/"
defer cleanup(dir)
prepare(dir)
_size := int64(20)
w := createTestWalFilled(is, dir, _size)
defer w.Close()
for i := int64(0); i < _size*2; i++ {
testData := []byte("this is pre filled test data: " + strconv.FormatInt(i+1, 10))
e := testEntry{}
err := w.ReadAt(&e, uint64(i+1))
is.NoErr(err)
is.Equal([]byte(e), testData)
}
}
func TestWalReadFromStart(t *testing.T) {
is := is.New(t)
dir := "tmp/walreadfromstart/"
defer cleanup(dir)
prepare(dir)
_size := int64(5)
w := createTestWalFilled(is, dir, _size)
defer w.Close()
out, err := w.ReadFrom(1, 10)
is.NoErr(err)
i := 1
for envelope := range out {
testData := []byte("this is pre filled test data: " + strconv.Itoa(i))
is.NoErr(envelope.Err)
is.Equal(envelope.SeqNum, uint64(i))
is.Equal(string(envelope.Payload), string(testData))
i++
}
is.Equal(10, i-1)
}
func TestWalReadFrom(t *testing.T) {
is := is.New(t)
dir := "tmp/walreadfrom/"
defer cleanup(dir)
prepare(dir)
_size := int64(20)
w := createTestWalFilled(is, dir, _size)
defer w.Close()
out, err := w.ReadFrom(10, 10)
is.NoErr(err)
i := 10
for envelope := range out {
testData := []byte("this is pre filled test data: " + strconv.Itoa(i))
is.NoErr(envelope.Err)
is.Equal(envelope.SeqNum, uint64(i))
is.Equal(string(envelope.Payload), string(testData))
i++
}
is.Equal(i, 20)
}
func TestWalTruncate(t *testing.T) {
is := is.New(t)
dir := "tmp/waltruncate/"
defer cleanup(dir)
prepare(dir)
_size := int64(20)
w := createTestWalFilled(is, dir, _size)
defer w.Close()
truncSeqNum := uint64(16)
e := testEntry{}
err := w.ReadAt(&e, truncSeqNum)
is.NoErr(err)
err = w.Truncate(truncSeqNum)
is.NoErr(err)
e2 := testEntry{}
err = w.ReadAt(&e2, truncSeqNum)
is.Equal(err, ErrEntryNotFound)
err = w.ReadAt(&e2, truncSeqNum-1)
is.NoErr(err)
is.Equal(truncSeqNum-1, w.SeqNum())
}
func TestWalReopen(t *testing.T) {
is := is.New(t)
dir := "tmp/walreopen/"
defer cleanup(dir)
prepare(dir)
_size := int64(20)
w := createTestWalFilled(is, dir, _size)
err := w.Close()
is.NoErr(err)
wRe, err := Open(dir+"test", 4, _size)
is.NoErr(err)
defer wRe.Close()
for i := int64(0); i < _size*2; i++ {
testData := []byte("this is pre filled test data: " + strconv.FormatInt(i+1, 10))
e := testEntry{}
err := wRe.ReadAt(&e, uint64(i+1))
is.NoErr(err)
is.Equal([]byte(e), testData)
}
}
func TestWalReopenWrite(t *testing.T) {
is := is.New(t)
dir := "tmp/walreopenwrite/"
defer cleanup(dir)
prepare(dir)
_size := int64(20)
w := createTestWalFilled(is, dir, _size)
err := w.Close()
is.NoErr(err)
wRe, err := Open(dir+"test", 4, _size)
is.NoErr(err)
defer wRe.Close()
for i := int64(1); i <= 5; i++ {
data := []byte("this is pre filled test data: " + strconv.FormatInt(i+1, 10))
seqNum, err := wRe.Write(data)
is.NoErr(err)
is.Equal(seqNum, uint64(i+40))
}
}