-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwal_test.go
204 lines (164 loc) · 3.73 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
package wal
import (
"fmt"
"github.com/stretchr/testify/require"
"math/rand"
"os"
"path"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const testDBPath = "testdb"
func cleanup() {
_ = os.RemoveAll(testDBPath)
}
func closeWAL(wal *WAL) {
_ = wal.Close()
}
func TestWal_New_Close(t *testing.T) {
defer cleanup()
w, err := New(testDBPath)
require.NoError(t, err)
require.NotNil(t, w)
err = w.Close()
assert.NoError(t, err)
}
func TestWal_Put_Get(t *testing.T) {
defer cleanup()
w, err := New(testDBPath)
require.NoError(t, err)
defer closeWAL(w)
err = w.Put("key1", []byte("value1"))
assert.NoError(t, err)
value, err := w.Get("key1")
require.NoError(t, err)
assert.Equal(t, []byte("value1"), value)
value, err = w.Get("nonexistent")
require.ErrorIs(t, err, ErrKeyNotFound)
assert.Nil(t, value)
}
func TestWal_Delete(t *testing.T) {
defer cleanup()
w, err := New(testDBPath)
require.NoError(t, err)
defer closeWAL(w)
err = w.Put("key1", []byte("value1"))
assert.NoError(t, err)
err = w.Delete("key1")
assert.NoError(t, err)
value, err := w.Get("key1")
assert.ErrorIs(t, err, ErrKeyNotFound)
assert.Nil(t, value)
}
func TestWal_PutBatch(t *testing.T) {
defer cleanup()
w, err := New(testDBPath)
assert.NoError(t, err)
defer closeWAL(w)
batch := map[string][]byte{
"key1": []byte("value1"),
"key2": []byte("value2"),
"key3": []byte("value3"),
}
err = w.PutBatch(batch)
assert.NoError(t, err)
for key, expectedValue := range batch {
value, err := w.Get(key)
assert.NoError(t, err)
assert.Equal(t, expectedValue, value)
}
}
func TestWal_Entries(t *testing.T) {
defer cleanup()
w, err := New(testDBPath)
assert.NoError(t, err)
defer closeWAL(w)
data := map[string][]byte{
"a": []byte("value1"),
"b": []byte("value2"),
"c": []byte("value3"),
}
err = w.PutBatch(data)
assert.NoError(t, err)
got := map[string][]byte{}
for k, v := range w.Entries() {
got[k] = v
}
assert.Equal(t, data, got)
}
func TestWal_EntriesWithPrefix(t *testing.T) {
defer cleanup()
w, err := New(testDBPath)
assert.NoError(t, err)
defer closeWAL(w)
data := map[string][]byte{
"prefix1_a": []byte("value1"),
"prefix1_b": []byte("value2"),
"prefix2_c": []byte("value3"),
}
err = w.PutBatch(data)
assert.NoError(t, err)
got := map[string][]byte{}
for k, v := range w.EntriesWithPrefix("prefix1") {
got[k] = v
}
want := map[string][]byte{
"prefix1_a": []byte("value1"),
"prefix1_b": []byte("value2"),
}
assert.Equal(t, want, got)
}
func TestWal_EntriesBetween(t *testing.T) {
defer cleanup()
w, err := New(testDBPath)
require.NoError(t, err)
defer closeWAL(w)
data := map[string][]byte{
"a": []byte("value1"),
"b": []byte("value2"),
"c": []byte("value3"),
"d": []byte("value4"),
}
err = w.PutBatch(data)
require.NoError(t, err)
got := map[string][]byte{}
for k, v := range w.EntriesBetween("a", "d") {
got[k] = v
}
want := map[string][]byte{
"a": []byte("value1"),
"b": []byte("value2"),
"c": []byte("value3"),
}
assert.Equal(t, want, got)
}
func generateData(numRecords int) map[string][]byte {
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
data := make(map[string][]byte, numRecords)
for i := 0; i < numRecords; i++ {
val := make([]byte, 1024)
rnd.Read(val)
key := fmt.Sprintf("key_%d", i)
data[key] = val
}
return data
}
func BenchmarkWAL_Put(b *testing.B) {
data := generateData(1000)
walFile := path.Join(os.TempDir(), fmt.Sprintf("walbench_%d", time.Now().UnixNano()))
w, err := New(walFile)
require.NoError(b, err)
defer closeWAL(w)
defer func(name string) {
_ = os.Remove(name)
}(walFile)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for k, v := range data {
err := w.Put(k, v)
require.NoError(b, err)
}
}
b.StopTimer()
}