ledisdb/ledis/dump_test.go

74 lines
1.5 KiB
Go
Raw Normal View History

2014-05-28 10:20:45 +04:00
package ledis
import (
"bytes"
2014-06-19 13:19:40 +04:00
"github.com/siddontang/ledisdb/leveldb"
2014-05-28 10:20:45 +04:00
"os"
"testing"
)
func TestDump(t *testing.T) {
os.RemoveAll("/tmp/test_ledis_master")
os.RemoveAll("/tmp/test_ledis_slave")
2014-05-28 10:20:45 +04:00
var masterConfig = []byte(`
{
"data_dir" : "/tmp/test_ledis_master",
2014-05-28 10:20:45 +04:00
"data_db" : {
"compression":true,
"block_size" : 32768,
"write_buffer_size" : 2097152,
"cache_size" : 20971520
}
}
`)
master, err := OpenWithJsonConfig(masterConfig)
2014-05-28 10:20:45 +04:00
if err != nil {
t.Fatal(err)
}
var slaveConfig = []byte(`
{
"data_dir" : "/tmp/test_ledis_slave",
2014-05-28 10:20:45 +04:00
"data_db" : {
"compression":true,
"block_size" : 32768,
"write_buffer_size" : 2097152,
"cache_size" : 20971520
}
}
`)
var slave *Ledis
if slave, err = OpenWithJsonConfig(slaveConfig); err != nil {
2014-05-28 10:20:45 +04:00
t.Fatal(err)
}
db, _ := master.Select(0)
db.Set([]byte("a"), []byte("1"))
db.Set([]byte("b"), []byte("2"))
db.Set([]byte("c"), []byte("3"))
if err := master.DumpFile("/tmp/testdb.dump"); err != nil {
t.Fatal(err)
}
if _, err := slave.LoadDumpFile("/tmp/testdb.dump"); err != nil {
t.Fatal(err)
}
2014-06-19 13:19:40 +04:00
it := master.ldb.RangeLimitIterator(nil, nil, leveldb.RangeClose, 0, -1)
2014-05-28 10:20:45 +04:00
for ; it.Valid(); it.Next() {
key := it.Key()
value := it.Value()
if v, err := slave.ldb.Get(key); err != nil {
t.Fatal(err)
} else if !bytes.Equal(v, value) {
t.Fatal("load dump error")
}
}
}