2014-06-04 10:42:02 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"github.com/siddontang/go-leveldb/leveldb"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReplication(t *testing.T) {
|
|
|
|
var master *Ledis
|
|
|
|
var slave *Ledis
|
|
|
|
var err error
|
|
|
|
|
2014-06-06 06:34:57 +04:00
|
|
|
os.RemoveAll("/tmp/repl_repl")
|
2014-06-04 10:42:02 +04:00
|
|
|
|
|
|
|
master, err = Open([]byte(`
|
|
|
|
{
|
2014-06-06 06:34:57 +04:00
|
|
|
"data_dir" : "/tmp/test_repl/master",
|
2014-06-06 10:57:18 +04:00
|
|
|
"use_bin_log" : true
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
slave, err = Open([]byte(`
|
|
|
|
{
|
2014-06-06 06:34:57 +04:00
|
|
|
"data_dir" : "/tmp/test_repl/slave"
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
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"))
|
|
|
|
|
2014-06-07 12:56:22 +04:00
|
|
|
binLogName := "/tmp/test_repl/master/bin_log/ledis-bin.0000001"
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-06-09 13:23:32 +04:00
|
|
|
err = slave.ReplicateFromBinLog(binLogName)
|
2014-06-04 10:42:02 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
it := master.ldb.Iterator(nil, nil, leveldb.RangeClose, 0, -1)
|
|
|
|
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("replication error", len(v), len(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|