2014-05-28 10:20:45 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-08-07 12:49:48 +04:00
|
|
|
"github.com/siddontang/ledisdb/config"
|
2014-07-25 13:58:00 +04:00
|
|
|
"github.com/siddontang/ledisdb/store"
|
2014-05-28 10:20:45 +04:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDump(t *testing.T) {
|
2014-10-11 12:00:59 +04:00
|
|
|
cfgM := config.NewConfigDefault()
|
2014-08-07 12:49:48 +04:00
|
|
|
cfgM.DataDir = "/tmp/test_ledis_master"
|
2014-05-28 10:20:45 +04:00
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
os.RemoveAll(cfgM.DataDir)
|
2014-05-28 10:20:45 +04:00
|
|
|
|
2014-08-07 12:49:48 +04:00
|
|
|
master, err := Open(cfgM)
|
2014-05-28 10:20:45 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-10-11 12:00:59 +04:00
|
|
|
cfgS := config.NewConfigDefault()
|
2014-08-07 12:49:48 +04:00
|
|
|
cfgS.DataDir = "/tmp/test_ledis_slave"
|
2014-10-23 09:34:12 +04:00
|
|
|
os.RemoveAll(cfgS.DataDir)
|
2014-05-28 10:20:45 +04:00
|
|
|
|
|
|
|
var slave *Ledis
|
2014-08-07 12:49:48 +04:00
|
|
|
if slave, err = Open(cfgS); 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-07-25 13:58:00 +04:00
|
|
|
it := master.ldb.RangeLimitIterator(nil, nil, store.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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|