mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
etcd: add joinThroughFollower test
This commit is contained in:
parent
638f36956b
commit
0ccd09532b
@ -2,8 +2,11 @@ package etcd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/coreos/etcd/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestKillLeader(t *testing.T) {
|
func TestKillLeader(t *testing.T) {
|
||||||
@ -64,6 +67,38 @@ func TestRandomKill(t *testing.T) {
|
|||||||
afterTest(t)
|
afterTest(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJoinThroughFollower(t *testing.T) {
|
||||||
|
tests := []int{3, 4, 5, 6}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
es := make([]*Server, tt)
|
||||||
|
hs := make([]*httptest.Server, tt)
|
||||||
|
for i := 0; i < tt; i++ {
|
||||||
|
c := config.New()
|
||||||
|
if i > 0 {
|
||||||
|
c.Peers = []string{hs[i-1].URL}
|
||||||
|
}
|
||||||
|
es[i], hs[i] = initTestServer(c, int64(i), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
go es[0].Bootstrap()
|
||||||
|
|
||||||
|
for i := 1; i < tt; i++ {
|
||||||
|
go es[i].Run()
|
||||||
|
waitLeader(es[:i])
|
||||||
|
}
|
||||||
|
waitCluster(t, es)
|
||||||
|
|
||||||
|
for i := range hs {
|
||||||
|
es[len(hs)-i-1].Stop()
|
||||||
|
}
|
||||||
|
for i := range hs {
|
||||||
|
hs[len(hs)-i-1].Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
afterTest(t)
|
||||||
|
}
|
||||||
|
|
||||||
type leadterm struct {
|
type leadterm struct {
|
||||||
lead int64
|
lead int64
|
||||||
term int64
|
term int64
|
||||||
@ -98,6 +133,9 @@ func isSameLead(ls []leadterm) bool {
|
|||||||
m[ls[i]] = m[ls[i]] + 1
|
m[ls[i]] = m[ls[i]] + 1
|
||||||
}
|
}
|
||||||
if len(m) == 1 {
|
if len(m) == 1 {
|
||||||
|
if ls[0].lead == -1 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
// todo(xiangli): printout the current cluster status for debugging....
|
// todo(xiangli): printout the current cluster status for debugging....
|
||||||
|
@ -1,39 +0,0 @@
|
|||||||
package test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestJoinThroughFollower(t *testing.T) {
|
|
||||||
procAttr := new(os.ProcAttr)
|
|
||||||
procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
|
|
||||||
|
|
||||||
_, etcds, err := CreateCluster(2, procAttr, false)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("cannot create cluster")
|
|
||||||
}
|
|
||||||
defer DestroyCluster(etcds)
|
|
||||||
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
|
|
||||||
newEtcd, err := os.StartProcess(EtcdBinPath, []string{"etcd", "-data-dir=/tmp/node3", "-name=node3", "-addr=127.0.0.1:4003", "-peer-addr=127.0.0.1:7003", "-peers=127.0.0.1:7002", "-f"}, procAttr)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("failed starting node3")
|
|
||||||
}
|
|
||||||
defer func() {
|
|
||||||
newEtcd.Kill()
|
|
||||||
newEtcd.Release()
|
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
|
|
||||||
leader, err := getLeader("http://127.0.0.1:4003")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal("failed getting leader from node3:", err)
|
|
||||||
}
|
|
||||||
if leader != "http://127.0.0.1:7001" {
|
|
||||||
t.Fatal("expect=http://127.0.0.1:7001 got=", leader)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user