Files
redis-go/database/replication_test.go
2022-07-02 11:45:56 +08:00

136 lines
3.1 KiB
Go

package database
import (
"bytes"
"github.com/hdt3213/godis/config"
"github.com/hdt3213/godis/lib/utils"
"github.com/hdt3213/godis/redis/client"
"github.com/hdt3213/godis/redis/connection"
"github.com/hdt3213/godis/redis/protocol"
"github.com/hdt3213/godis/redis/protocol/asserts"
"testing"
"time"
)
func TestReplication(t *testing.T) {
mdb := &MultiDB{}
mdb.dbSet = make([]*DB, 16)
for i := range mdb.dbSet {
singleDB := makeDB()
singleDB.index = i
mdb.dbSet[i] = singleDB
}
mdb.replication = initReplStatus()
masterCli, err := client.MakeClient("127.0.0.1:6379")
if err != nil {
t.Error(err)
}
masterCli.Start()
// sync with master
ret := masterCli.Send(utils.ToCmdLine("set", "1", "1"))
asserts.AssertStatusReply(t, ret, "OK")
conn := &connection.FakeConn{}
ret = mdb.Exec(conn, utils.ToCmdLine("SLAVEOF", "127.0.0.1", "6379"))
asserts.AssertStatusReply(t, ret, "OK")
success := false
for i := 0; i < 30; i++ {
// wait for sync
time.Sleep(time.Second)
ret = mdb.Exec(conn, utils.ToCmdLine("GET", "1"))
bulkRet, ok := ret.(*protocol.BulkReply)
if ok {
if bytes.Equal(bulkRet.Arg, []byte("1")) {
success = true
break
}
}
}
if !success {
t.Error("sync failed")
return
}
// receive aof
ret = masterCli.Send(utils.ToCmdLine("set", "1", "2"))
asserts.AssertStatusReply(t, ret, "OK")
success = false
for i := 0; i < 10; i++ {
// wait for sync
time.Sleep(time.Second)
ret = mdb.Exec(conn, utils.ToCmdLine("GET", "1"))
bulkRet, ok := ret.(*protocol.BulkReply)
if ok {
if bytes.Equal(bulkRet.Arg, []byte("2")) {
success = true
break
}
}
}
if !success {
t.Error("sync failed")
return
}
err = mdb.replication.sendAck2Master()
if err != nil {
t.Error(err)
return
}
// test reconnect
config.Properties.ReplTimeout = 1
_ = mdb.replication.masterConn.Close()
ret = masterCli.Send(utils.ToCmdLine("set", "1", "3"))
asserts.AssertStatusReply(t, ret, "OK")
mdb.slaveCron()
success = false
for i := 0; i < 10; i++ {
// wait for sync
time.Sleep(time.Second)
ret = mdb.Exec(conn, utils.ToCmdLine("GET", "1"))
bulkRet, ok := ret.(*protocol.BulkReply)
if ok {
if bytes.Equal(bulkRet.Arg, []byte("3")) {
success = true
break
}
}
}
if !success {
t.Error("sync failed")
return
}
// test slave of no one
ret = mdb.Exec(conn, utils.ToCmdLine("SLAVEOF", "NO", "ONE"))
asserts.AssertStatusReply(t, ret, "OK")
ret = masterCli.Send(utils.ToCmdLine("set", "1", "4"))
asserts.AssertStatusReply(t, ret, "OK")
ret = mdb.Exec(conn, utils.ToCmdLine("GET", "1"))
asserts.AssertBulkReply(t, ret, "3")
ret = mdb.Exec(conn, utils.ToCmdLine("SLAVEOF", "127.0.0.1", "6379"))
asserts.AssertStatusReply(t, ret, "OK")
success = false
for i := 0; i < 30; i++ {
// wait for sync
time.Sleep(time.Second)
ret = mdb.Exec(conn, utils.ToCmdLine("GET", "1"))
bulkRet, ok := ret.(*protocol.BulkReply)
if ok {
if bytes.Equal(bulkRet.Arg, []byte("4")) {
success = true
break
}
}
}
if !success {
t.Error("sync failed")
return
}
err = mdb.replication.close()
if err != nil {
t.Error("cannot close")
}
}