代码拉取完成,页面将自动刷新
package tiny_bitcask
import (
"fmt"
"github.com/stretchr/testify/assert"
"os"
"testing"
"tiny-bitcask/storage"
)
func BitCaskTest(t *testing.T, opt *Options, test func(db *DB)) {
if opt == nil {
opt = DefaultOptions
}
db, err := NewDB(opt)
assert.NoError(t, err)
test(db)
os.RemoveAll(opt.Dir)
}
func TestDB_Base(t *testing.T) {
var test = func(db *DB) {
err := db.Set([]byte("test_key"), []byte("test_value"))
assert.NoError(t, err)
value, err := db.Get([]byte("test_key"))
assert.NoError(t, err)
assert.Equal(t, "test_value", string(value))
err = db.Set([]byte("test_key"), []byte("test_value_2"))
assert.NoError(t, err)
value, err = db.Get([]byte("test_key"))
assert.NoError(t, err)
assert.Equal(t, "test_value_2", string(value))
}
BitCaskTest(t, nil, test)
}
func TestDB_SegmentSize(t *testing.T) {
opt := &Options{
Dir: "db",
SegmentSize: 4 * storage.KB,
}
var test = func(db *DB) {
for i := 0; i < 1000; i++ {
key := fmt.Sprintf("test_key_%d", i)
value := fmt.Sprintf("test_value_%d", i)
err := db.Set([]byte(key), []byte(value))
assert.NoError(t, err)
}
}
BitCaskTest(t, opt, test)
}
func TestDB_Merge(t *testing.T) {
opt := &Options{
Dir: "db",
SegmentSize: 4 * storage.KB,
}
var test = func(db *DB) {
key := "test_key"
for i := 0; i < 1000; i++ {
value := fmt.Sprintf("test_value_%d", i)
err := db.Set([]byte(key), []byte(value))
assert.NoError(t, err)
}
err := db.Merge()
assert.NoError(t, err)
value, err := db.Get([]byte("test_key"))
assert.NoError(t, err)
assert.Equal(t, "test_value_999", string(value))
}
BitCaskTest(t, opt, test)
}
func TestDB_Delete(t *testing.T) {
var test = func(db *DB) {
err := db.Set([]byte("test_key"), []byte("test_value"))
assert.NoError(t, err)
value, err := db.Get([]byte("test_key"))
assert.NoError(t, err)
assert.Equal(t, "test_value", string(value))
err = db.Delete([]byte("test_key"))
assert.NoError(t, err)
value, err = db.Get([]byte("test_key"))
assert.Nil(t, value)
assert.ErrorAs(t, KeyNotFoundErr, &err)
}
BitCaskTest(t, nil, test)
}
func TestDB_Delete_Merge(t *testing.T) {
opt := &Options{
Dir: "db",
SegmentSize: 4 * storage.KB,
}
var test = func(db *DB) {
key := "test_key"
for i := 0; i < 1000; i++ {
value := fmt.Sprintf("test_value_%d", i)
err := db.Set([]byte(key), []byte(value))
assert.NoError(t, err)
}
err := db.Delete([]byte("test_key"))
assert.NoError(t, err)
err = db.Merge()
assert.NoError(t, err)
value, err := db.Get([]byte("test_key"))
assert.Nil(t, value)
assert.Equal(t, KeyNotFoundErr, err)
}
BitCaskTest(t, opt, test)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。