-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_test.go
319 lines (268 loc) · 7.66 KB
/
db_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
package bitcask
import (
"bitcask/utils"
"github.com/stretchr/testify/assert"
"os"
"testing"
)
func destroyDB(db *DB) {
if db != nil {
if db.activeFile != nil {
_ = db.Close()
}
err := os.RemoveAll(db.cfg.DirPath)
if err != nil {
panic(err)
}
}
}
// TestOpen is a test function for the Open function.
func TestOpen(t *testing.T) {
cfg := DefaultConfig
// Create a temporary directory for testing.
temp, err := os.MkdirTemp("", "bitcask-test-open")
assert.Nil(t, err)
cfg.DirPath = temp
// Open a new Bitcask database.
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
}
// TestDB_Put is a unit test function for the Put method of the DB struct.
func TestDB_Put(t *testing.T) {
// Create a temporary directory for testing
cfg := DefaultConfig
cfg.DataFileSize = 64 * 1024 * 1024
//temp, err := os.MkdirTemp("", "bitcask-test-put")
//assert.Nil(t, err)
//cfg.DirPath = temp
// Open a new DB instance
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Test putting a new key-value pair
key := utils.GetTestKey(1)
val := utils.GetTestValue(24)
err = db.Put(key, val)
assert.Nil(t, err)
val1, err := db.Get(key)
assert.Nil(t, err)
assert.Equal(t, val, val1)
// Test putting an existing key-value pair
val = utils.GetTestValue(24)
err = db.Put(key, val)
assert.Nil(t, err)
val2, err := db.Get(key)
assert.Nil(t, err)
assert.Equal(t, val, val2)
// Test putting a key with an empty key
err = db.Put(nil, val)
assert.Equal(t, err, ErrKeyIsEmpty)
// Test putting a value with an empty value
err = db.Put(key, nil)
assert.Nil(t, err)
val3, err := db.Get(key)
assert.Nil(t, err)
assert.Equal(t, len(val3), 0)
//TODO removed this test
////Test putting a large number of key-value pairs
for i := 0; i < 1000000; i++ {
err := db.Put(utils.GetTestKey(i), utils.GetTestValue(128))
assert.Nil(t, err)
}
// Restart the DB instance and test the Put method again
assert.Equal(t, 2, len(db.oldFile))
err = db.Close()
assert.Nil(t, err)
db, err = Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.Put(key, val)
assert.Nil(t, err)
}
// TestDB_Get is a unit test function for the Get method of the DB struct.
func TestDB_Get(t *testing.T) {
// Create a temporary directory for testing
cfg := DefaultConfig
cfg.DataFileSize = 64 * 1024 * 1024
temp, err := os.MkdirTemp("", "bitcask-test-get")
assert.Nil(t, err)
cfg.DirPath = temp
// Open a new DB instance
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Test getting an existing key
key := utils.GetTestKey(1)
val := utils.GetTestValue(24)
// Put the key-value pair into the database
err = db.Put(key, val)
assert.Nil(t, err)
// Get the value associated with the key
get1, err := db.Get(key)
assert.Nil(t, err)
assert.Equal(t, get1, val)
// Test getting a non-existing key
get2, err := db.Get([]byte("unknown key"))
assert.Nil(t, get2)
assert.Equal(t, err, ErrKeyNotFound)
// Test updating and getting a key
valNew := utils.GetTestValue(24)
err = db.Put(key, valNew)
assert.Nil(t, err)
get3, err := db.Get(key)
assert.Nil(t, err)
assert.Equal(t, get3, valNew)
// Test deleting and getting a key
err = db.Delete(key)
assert.Nil(t, err)
get4, err := db.Get(key)
assert.Equal(t, 0, len(get4))
assert.Equal(t, ErrKeyNotFound, err)
// Test getting a key from old files
for i := 0; i < 1000000; i++ {
err := db.Put(utils.GetTestKey(i), utils.GetTestValue(128))
assert.Nil(t, err)
}
assert.Equal(t, 2, len(db.oldFile))
get5, err := db.Get(utils.GetTestKey(1))
assert.Nil(t, err)
assert.NotNil(t, get5)
// Restart the database and verify the deletion
err = db.Close()
assert.Nil(t, err)
db, err = Open(cfg)
assert.Nil(t, err)
get6, err := db.Get(key)
assert.Nil(t, err)
assert.NotNil(t, get6)
}
// TestDB_Delete is a unit test function for the Delete method of the DB struct.
func TestDB_Delete(t *testing.T) {
// Create a temporary directory for testing
cfg := DefaultConfig
temp, err := os.MkdirTemp("", "bitcask-test-delete")
assert.Nil(t, err)
cfg.DirPath = temp
// Open a new DB instance
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Delete an existing key
key := utils.GetTestKey(1)
val := utils.GetTestValue(24)
err = db.Put(key, val)
assert.Nil(t, err)
err = db.Delete(key)
assert.Nil(t, err)
// Delete a non-existing key
err = db.Delete([]byte("unknown key"))
assert.Nil(t, err)
// Delete an empty key
err = db.Delete(nil)
assert.Equal(t, err, ErrKeyIsEmpty)
// Delete a key and then put it again
err = db.Put(key, val)
assert.Nil(t, err)
err = db.Delete(key)
assert.Nil(t, err)
// Put a key and then get it to verify the deletion
err = db.Put(key, val)
assert.Nil(t, err)
get, err := db.Get(key)
assert.Nil(t, err)
assert.Equal(t, get, val)
// Restart the database and verify the deletion
err = db.Close()
assert.Nil(t, err)
db, err = Open(cfg)
_, err = db.Get([]byte("unknown key"))
assert.Equal(t, ErrKeyNotFound, err)
val2, err := db.Get(utils.GetTestKey(1))
assert.Nil(t, err)
assert.Equal(t, val, val2)
}
// TestDB_ListKeys is a unit test function for the ListKeys method of the DB struct.
func TestDB_ListKeys(t *testing.T) {
// Create a temporary directory for testing
cfg := DefaultConfig
temp, err := os.MkdirTemp("", "bitcask-test-list-keys")
assert.Nil(t, err)
cfg.DirPath = temp
// Open a new DB instance
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Test when the database is empty
keys1 := db.ListKeys()
assert.Equal(t, 0, len(keys1))
// Test when there is only one key-value pair
err = db.Put(utils.GetTestKey(10), utils.GetTestValue(10))
assert.Nil(t, err)
keys2 := db.ListKeys()
assert.Equal(t, 1, len(keys2))
// Test when there are multiple key-value pairs
err = db.Put(utils.GetTestKey(11), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put(utils.GetTestKey(12), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put(utils.GetTestKey(13), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put(utils.GetTestKey(14), utils.GetTestValue(10))
assert.Nil(t, err)
keys3 := db.ListKeys()
assert.Equal(t, 5, len(keys3))
}
// TestDB_Fold is a test function for the Fold method of the DB struct.
func TestDB_Fold(t *testing.T) {
// Create a temporary directory for testing
cfg := DefaultConfig
temp, err := os.MkdirTemp("", "bitcask-test-fold")
assert.Nil(t, err)
cfg.DirPath = temp
// Open a new DB instance
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
// Put some test data into the DB
err = db.Put(utils.GetTestKey(10), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put(utils.GetTestKey(11), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put(utils.GetTestKey(12), utils.GetTestValue(10))
assert.Nil(t, err)
err = db.Put(utils.GetTestKey(13), utils.GetTestValue(10))
assert.Nil(t, err)
// Fold over the DB and perform some assertions
err = db.Fold(func(key, value []byte) bool {
assert.NotNil(t, key)
return true
})
assert.Nil(t, err)
}
// TestDB_Close is a unit test function for the Close method of the DB struct.
func TestDB_Close(t *testing.T) {
// Create a temporary directory for testing
cfg := DefaultConfig
temp, err := os.MkdirTemp("", "bitcask-test-close")
assert.Nil(t, err)
cfg.DirPath = temp
// Open a new DB instance
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
}
// TestDB_Sync is a unit test function for the Sync method of the DB struct.
func TestDB_Sync(t *testing.T) {
// Create a temporary directory for testing
cfg := DefaultConfig
temp, err := os.MkdirTemp("", "bitcask-test-sync")
assert.Nil(t, err)
cfg.DirPath = temp
// Open a new DB instance
db, err := Open(cfg)
assert.Nil(t, err)
assert.NotNil(t, db)
err = db.Sync()
assert.Nil(t, err)
}