gocache/kvstore_test.go

133 lines
3.1 KiB
Go
Raw Normal View History

2025-02-23 12:46:27 +08:00
package gocache
import (
"os"
"testing"
)
2025-02-23 13:34:39 +08:00
var mumlimit int64 = 1024 * 1024 * 1024
var bucketCount = 16
2025-02-23 12:46:27 +08:00
func TestNewKVStore(t *testing.T) {
2025-02-23 13:34:39 +08:00
store := NewKVStore("test.db", mumlimit, bucketCount)
2025-02-23 12:46:27 +08:00
if store == nil {
t.Error("Expected a new KVStore instance, got nil")
}
}
func TestPutAndGet(t *testing.T) {
2025-02-23 13:34:39 +08:00
store := NewKVStore("test.db", mumlimit, bucketCount)
2025-02-23 22:02:37 +08:00
// 测试字符串类型
2025-02-23 12:46:27 +08:00
store.Put("key1", "value1")
2025-02-23 22:02:37 +08:00
store.wg.Wait() // 等待异步操作完成
2025-02-23 12:46:27 +08:00
value, exists := store.Get("key1")
if !exists || value != "value1" {
t.Error("Expected value 'value1' for key 'key1'")
}
2025-02-23 22:02:37 +08:00
// 测试整数类型
store.Put("key2", 123)
store.wg.Wait() // 等待异步操作完成
value, exists = store.Get("key2")
if !exists || value != 123 {
t.Error("Expected value 123 for key 'key2'")
}
// 测试布尔类型
store.Put("key3", true)
store.wg.Wait() // 等待异步操作完成
value, exists = store.Get("key3")
if !exists || value != true {
t.Error("Expected value true for key 'key3'")
}
// 测试数组类型
store.Put("key4", []string{"a", "b", "c"})
store.wg.Wait() // 等待异步操作完成
value, exists = store.Get("key4")
if !exists {
t.Error("Expected value for key 'key4'")
}
2025-02-23 12:46:27 +08:00
}
func TestDelete(t *testing.T) {
2025-02-23 13:34:39 +08:00
store := NewKVStore("test.db", mumlimit, bucketCount)
2025-02-23 12:46:27 +08:00
store.Put("key1", "value1")
2025-02-23 22:02:37 +08:00
store.wg.Wait() // 等待异步操作完成
2025-02-23 12:46:27 +08:00
store.Delete("key1")
2025-02-23 22:02:37 +08:00
store.wg.Wait() // 等待异步操作完成
2025-02-23 12:46:27 +08:00
_, exists := store.Get("key1")
if exists {
t.Error("Expected key 'key1' to be deleted")
}
}
2025-02-23 22:02:37 +08:00
func TestLogOperation(t *testing.T) {
2025-02-23 13:34:39 +08:00
store := NewKVStore("test.db", mumlimit, bucketCount)
2025-02-23 22:02:37 +08:00
err := store.LogOperation("put", "key1", "value1")
2025-02-23 12:46:27 +08:00
if err != nil {
2025-02-23 22:02:37 +08:00
t.Error("Failed to log operation")
2025-02-23 12:46:27 +08:00
}
// Clean up
os.Remove("test.db.log")
}
2025-02-23 22:02:37 +08:00
func createDB() {
2025-02-23 13:34:39 +08:00
store := NewKVStore("test.db", mumlimit, bucketCount)
2025-02-23 22:02:37 +08:00
store.Put("key1", "value1")
store.Put("key2", 123)
store.Put("key3", true)
store.Put("key4", []string{"a", "b", "c"})
store.wg.Wait()
}
2025-02-23 12:46:27 +08:00
2025-02-23 22:02:37 +08:00
func TestRecoverFromLog(t *testing.T) {
createDB()
store2 := NewKVStore("test.db", mumlimit, bucketCount)
// store2.RecoverFromLog()
value, exists := store2.Get("key1")
if !exists || value != "value1" {
t.Error("Expected value 'value1' for key 'key1' after recovery")
}
2025-02-23 12:46:27 +08:00
}
func TestTransaction(t *testing.T) {
2025-02-23 13:34:39 +08:00
store := NewKVStore("test.db", mumlimit, bucketCount)
2025-02-23 22:02:37 +08:00
// 测试字符串类型
2025-02-23 12:46:27 +08:00
store.BeginTransaction()
store.PutInTransaction("key1", "value1")
store.Commit()
value, exists := store.Get("key1")
if !exists || value != "value1" {
t.Error("Expected value 'value1' for key 'key1' after commit")
}
2025-02-23 22:02:37 +08:00
// 测试整数类型
2025-02-23 12:46:27 +08:00
store.BeginTransaction()
2025-02-23 22:02:37 +08:00
store.PutInTransaction("key2", 123)
store.Commit()
2025-02-23 12:46:27 +08:00
2025-02-23 22:02:37 +08:00
value, exists = store.Get("key2")
if !exists || value != 123 {
t.Error("Expected value 123 for key 'key2' after commit")
2025-02-23 12:46:27 +08:00
}
2025-02-23 22:02:37 +08:00
// 测试回滚
2025-02-23 12:46:27 +08:00
store.BeginTransaction()
store.PutInTransaction("key3", "value3")
2025-02-23 22:02:37 +08:00
store.Rollback()
2025-02-23 12:46:27 +08:00
2025-02-23 22:02:37 +08:00
_, exists = store.Get("key3")
if exists {
t.Error("Expected key 'key3' to be rolled back")
2025-02-23 12:46:27 +08:00
}
// Clean up
os.Remove("test.db")
os.Remove("test.db.log")
}