ledisdb/rpl/store_test.go

167 lines
2.7 KiB
Go
Raw Permalink Normal View History

2014-09-22 13:50:51 +04:00
package rpl
2014-09-15 18:42:25 +04:00
import (
2014-11-15 16:20:12 +03:00
"github.com/siddontang/ledisdb/config"
2014-09-15 18:42:25 +04:00
"io/ioutil"
"os"
"testing"
)
func TestGoLevelDBStore(t *testing.T) {
// Create a test dir
dir, err := ioutil.TempDir("", "wal")
if err != nil {
t.Fatalf("err: %v ", err)
}
defer os.RemoveAll(dir)
// New level
2014-10-16 05:35:35 +04:00
l, err := NewGoLevelDBStore(dir, 0)
2014-09-15 18:42:25 +04:00
if err != nil {
t.Fatalf("err: %v ", err)
}
defer l.Close()
testLogs(t, l)
}
2014-11-07 11:35:54 +03:00
func TestFileStore(t *testing.T) {
// Create a test dir
dir, err := ioutil.TempDir("", "ldb")
if err != nil {
t.Fatalf("err: %v ", err)
}
defer os.RemoveAll(dir)
// New level
2014-11-15 16:20:12 +03:00
cfg := config.NewConfigDefault()
cfg.Replication.MaxLogFileSize = 4096
l, err := NewFileStore(dir, cfg)
2014-11-07 11:35:54 +03:00
if err != nil {
t.Fatalf("err: %v ", err)
}
defer l.Close()
testLogs(t, l)
}
2014-09-22 13:50:51 +04:00
func testLogs(t *testing.T, l LogStore) {
2014-09-15 18:42:25 +04:00
// Should be no first index
idx, err := l.FirstID()
if err != nil {
t.Fatalf("err: %v ", err)
}
if idx != 0 {
t.Fatalf("bad idx: %d", idx)
}
// Should be no last index
idx, err = l.LastID()
if err != nil {
t.Fatalf("err: %v ", err)
}
if idx != 0 {
t.Fatalf("bad idx: %d", idx)
}
// Try a filed fetch
var out Log
2014-11-07 11:35:54 +03:00
if err := l.GetLog(10, &out); err != ErrLogNotFound {
2014-09-15 18:42:25 +04:00
t.Fatalf("err: %v ", err)
}
2014-11-07 11:35:54 +03:00
data := make([]byte, 1024)
2014-09-15 18:42:25 +04:00
// Write out a log
log := Log{
ID: 1,
2014-11-07 11:35:54 +03:00
Data: data,
2014-09-15 18:42:25 +04:00
}
for i := 1; i <= 10; i++ {
log.ID = uint64(i)
if err := l.StoreLog(&log); err != nil {
t.Fatalf("err: %v", err)
}
}
// Attempt to write multiple logs
for i := 11; i <= 20; i++ {
nl := &Log{
ID: uint64(i),
2014-11-07 11:35:54 +03:00
Data: data,
2014-09-15 18:42:25 +04:00
}
2014-10-31 10:40:47 +03:00
if err := l.StoreLog(nl); err != nil {
t.Fatalf("err: %v", err)
}
2014-09-15 18:42:25 +04:00
}
2014-11-07 11:35:54 +03:00
// Try to fetch
if err := l.GetLog(1, &out); err != nil {
t.Fatalf("err: %v ", err)
}
2014-09-15 18:42:25 +04:00
// Try to fetch
if err := l.GetLog(10, &out); err != nil {
t.Fatalf("err: %v ", err)
}
// Try to fetch
if err := l.GetLog(20, &out); err != nil {
t.Fatalf("err: %v ", err)
}
// Check the lowest index
idx, err = l.FirstID()
if err != nil {
t.Fatalf("err: %v ", err)
}
if idx != 1 {
t.Fatalf("bad idx: %d", idx)
}
// Check the highest index
idx, err = l.LastID()
if err != nil {
t.Fatalf("err: %v ", err)
}
if idx != 20 {
t.Fatalf("bad idx: %d", idx)
}
2014-11-07 11:35:54 +03:00
if err = l.Clear(); err != nil {
t.Fatalf("err :%v", err)
}
// Check the lowest index
idx, err = l.FirstID()
if err != nil {
t.Fatalf("err: %v ", err)
}
if idx != 0 {
t.Fatalf("bad idx: %d", idx)
}
// Check the highest index
idx, err = l.LastID()
if err != nil {
t.Fatalf("err: %v ", err)
}
if idx != 0 {
t.Fatalf("bad idx: %d", idx)
}
// Write out a log
log = Log{
ID: 1,
Data: data,
}
for i := 1; i <= 10; i++ {
log.ID = uint64(i)
if err := l.StoreLog(&log); err != nil {
t.Fatalf("err: %v", err)
}
}
2014-09-15 18:42:25 +04:00
}