ledisdb/server/cmd_replication_test.go

134 lines
2.6 KiB
Go
Raw Normal View History

2014-06-10 06:41:50 +04:00
package server
import (
"fmt"
"github.com/siddontang/ledisdb/config"
2014-06-10 06:41:50 +04:00
"os"
2014-09-18 18:20:18 +04:00
"reflect"
2014-06-10 06:41:50 +04:00
"testing"
"time"
)
func checkDataEqual(master *App, slave *App) error {
2014-09-18 18:20:18 +04:00
mdb, _ := master.ldb.Select(0)
sdb, _ := slave.ldb.Select(0)
mkeys, _ := mdb.Scan(nil, 100, true, "")
skeys, _ := sdb.Scan(nil, 100, true, "")
if len(mkeys) != len(skeys) {
return fmt.Errorf("keys number not equal %d != %d", len(mkeys), len(skeys))
2014-09-18 18:20:18 +04:00
} else if !reflect.DeepEqual(mkeys, skeys) {
return fmt.Errorf("keys not equal")
} else {
for _, k := range mkeys {
v1, _ := mdb.Get(k)
v2, _ := sdb.Get(k)
if !reflect.DeepEqual(v1, v2) {
return fmt.Errorf("value not equal")
}
2014-06-10 06:41:50 +04:00
}
}
return nil
}
func TestReplication(t *testing.T) {
data_dir := "/tmp/test_replication"
os.RemoveAll(data_dir)
2014-10-11 12:00:59 +04:00
masterCfg := config.NewConfigDefault()
2014-06-10 06:41:50 +04:00
masterCfg.DataDir = fmt.Sprintf("%s/master", data_dir)
masterCfg.Addr = "127.0.0.1:11182"
masterCfg.UseReplication = true
masterCfg.Replication.Sync = true
2014-10-11 12:00:59 +04:00
masterCfg.Replication.WaitSyncTime = 5000
2014-06-10 06:41:50 +04:00
var master *App
var slave *App
var err error
master, err = NewApp(masterCfg)
if err != nil {
t.Fatal(err)
}
2014-08-26 19:21:45 +04:00
defer master.Close()
2014-06-10 06:41:50 +04:00
2014-10-11 12:00:59 +04:00
slaveCfg := config.NewConfigDefault()
2014-06-10 06:41:50 +04:00
slaveCfg.DataDir = fmt.Sprintf("%s/slave", data_dir)
slaveCfg.Addr = "127.0.0.1:11183"
slaveCfg.SlaveOf = masterCfg.Addr
slaveCfg.UseReplication = true
2014-06-10 06:41:50 +04:00
slave, err = NewApp(slaveCfg)
if err != nil {
t.Fatal(err)
}
2014-08-26 19:21:45 +04:00
defer slave.Close()
2014-06-10 06:41:50 +04:00
go master.Run()
time.Sleep(1 * time.Second)
go slave.Run()
2014-06-10 06:41:50 +04:00
db, _ := master.ldb.Select(0)
value := make([]byte, 10)
db.Set([]byte("a"), value)
db.Set([]byte("b"), value)
2014-09-18 18:20:18 +04:00
db.Set([]byte("c"), value)
db.Set([]byte("d"), value)
2014-06-10 06:41:50 +04:00
time.Sleep(1 * time.Second)
if err = checkDataEqual(master, slave); err != nil {
t.Fatal(err)
}
db.Set([]byte("a1"), value)
db.Set([]byte("b1"), value)
2014-09-18 18:20:18 +04:00
db.Set([]byte("c1"), value)
db.Set([]byte("d1"), value)
2014-06-10 06:41:50 +04:00
//time.Sleep(1 * time.Second)
slave.ldb.WaitReplication()
2014-06-10 06:41:50 +04:00
if err = checkDataEqual(master, slave); err != nil {
t.Fatal(err)
}
2014-10-10 05:49:16 +04:00
slave.slaveof("", false, false)
2014-06-10 06:41:50 +04:00
db.Set([]byte("a2"), value)
db.Set([]byte("b2"), value)
2014-09-18 18:20:18 +04:00
db.Set([]byte("c2"), value)
db.Set([]byte("d2"), value)
2014-06-10 06:41:50 +04:00
db.Set([]byte("a3"), value)
db.Set([]byte("b3"), value)
2014-09-18 18:20:18 +04:00
db.Set([]byte("c3"), value)
db.Set([]byte("d3"), value)
2014-06-10 06:41:50 +04:00
if err = checkDataEqual(master, slave); err == nil {
t.Fatal("must error")
}
2014-10-10 05:49:16 +04:00
slave.slaveof(masterCfg.Addr, false, false)
2014-06-10 06:41:50 +04:00
time.Sleep(1 * time.Second)
if err = checkDataEqual(master, slave); err != nil {
t.Fatal(err)
}
slave.tryReSlaveof()
time.Sleep(1 * time.Second)
slave.ldb.WaitReplication()
if err = checkDataEqual(master, slave); err != nil {
t.Fatal(err)
}
2014-06-10 06:41:50 +04:00
}