etcd: replace buildServer with runServer in tests

This commit is contained in:
Xiang Li 2014-08-07 14:26:37 -07:00 committed by Yicheng Qin
parent 0881021e54
commit 3229c91dbb
4 changed files with 57 additions and 40 deletions

View File

@ -58,7 +58,8 @@ func TestKillLeader(t *testing.T) {
c.DataDir = es[lead].cfg.DataDir
c.Addr = hs[lead].Listener.Addr().String()
id := es[lead].id
e, h, err := buildServer(t, c, id)
e, h := initTestServer(c, id, false)
err := startServer(t, e)
if err != nil {
t.Fatalf("#%d.%d: %v", i, j, err)
}
@ -99,7 +100,8 @@ func TestKillRandom(t *testing.T) {
c.DataDir = es[k].cfg.DataDir
c.Addr = hs[k].Listener.Addr().String()
id := es[k].id
e, h, err := buildServer(t, c, id)
e, h := initTestServer(c, id, false)
err := startServer(t, e)
if err != nil {
t.Fatal(err)
}
@ -148,10 +150,10 @@ func TestClusterConfigReload(t *testing.T) {
waitCluster(t, es)
lead, _ := waitLeader(es)
conf := conf.NewClusterConfig()
conf.ActiveSize = 15
conf.RemoveDelay = 60
if err := es[lead].p.setClusterConfig(conf); err != nil {
cc := conf.NewClusterConfig()
cc.ActiveSize = 15
cc.RemoveDelay = 60
if err := es[lead].p.setClusterConfig(cc); err != nil {
t.Fatalf("#%d: setClusterConfig err = %v", i, err)
}
@ -165,7 +167,8 @@ func TestClusterConfigReload(t *testing.T) {
c.DataDir = es[k].cfg.DataDir
c.Addr = hs[k].Listener.Addr().String()
id := es[k].id
e, h, err := buildServer(t, c, id)
e, h := initTestServer(c, id, false)
err := startServer(t, e)
if err != nil {
t.Fatal(err)
}
@ -176,8 +179,8 @@ func TestClusterConfigReload(t *testing.T) {
lead, _ = waitLeader(es)
// wait for msgAppResp to commit all entries
time.Sleep(2 * defaultHeartbeat * es[lead].tickDuration)
if g := es[lead].p.clusterConfig(); !reflect.DeepEqual(g, conf) {
t.Errorf("#%d: clusterConfig = %+v, want %+v", i, g, conf)
if g := es[lead].p.clusterConfig(); !reflect.DeepEqual(g, cc) {
t.Errorf("#%d: clusterConfig = %+v, want %+v", i, g, cc)
}
destoryCluster(t, es, hs)
@ -204,7 +207,8 @@ func TestMultiNodeKillOne(t *testing.T) {
c.DataDir = es[idx].cfg.DataDir
c.Addr = hs[idx].Listener.Addr().String()
id := es[idx].id
e, h, err := buildServer(t, c, id)
e, h := initTestServer(c, id, false)
err := startServer(t, e)
if err != nil {
t.Fatalf("#%d.%d: %v", i, j, err)
}
@ -245,7 +249,8 @@ func TestMultiNodeKillAllAndRecovery(t *testing.T) {
c.DataDir = es[k].cfg.DataDir
c.Addr = hs[k].Listener.Addr().String()
id := es[k].id
e, h, err := buildServer(t, c, id)
e, h := initTestServer(c, id, false)
err := startServer(t, e)
if err != nil {
t.Fatalf("#%d.%d: %v", i, k, err)
}

View File

@ -57,7 +57,8 @@ func TestBadDiscoveryService(t *testing.T) {
c := conf.New()
c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
_, _, err := buildServer(t, c, bootstrapId)
e, h := initTestServer(c, bootstrapId, false)
err := startServer(t, e)
w := `discovery service error`
if err == nil || !strings.HasPrefix(err.Error(), w) {
t.Errorf("err = %v, want %s prefix", err, w)
@ -69,6 +70,8 @@ func TestBadDiscoveryService(t *testing.T) {
t.Fatal("Discovery server never called")
}
ts.Close()
destroyServer(t, e, h)
afterTest(t)
}
@ -82,13 +85,15 @@ func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
c := conf.New()
c.Discovery = ts.URL + "/v2/keys/_etcd/registry/1"
c.Peers = []string{hs[0].URL}
_, _, err := buildServer(t, c, bootstrapId)
e, h := initTestServer(c, bootstrapId, false)
err := startServer(t, e)
w := `discovery service error`
if err == nil || !strings.HasPrefix(err.Error(), w) {
t.Errorf("err = %v, want %s prefix", err, w)
}
destoryCluster(t, es, hs)
destroyServer(t, e, h)
ts.Close()
afterTest(t)
}
@ -96,24 +101,27 @@ func TestBadDiscoveryServiceWithAdvisedPeers(t *testing.T) {
func TestBootstrapByEmptyPeers(t *testing.T) {
c := conf.New()
id := genId()
e, h, err := buildServer(t, c, id)
e, h := initTestServer(c, id, false)
err := startServer(t, e)
if err != nil {
t.Error(err)
}
if e.p.node.Leader() != id {
t.Error("leader = %x, want %x", e.p.node.Leader(), id)
t.Errorf("leader = %x, want %x", e.p.node.Leader(), id)
}
destroyServer(t, e, h)
afterTest(t)
}
func TestBootstrapByDiscoveryService(t *testing.T) {
de, dh, _ := buildServer(t, conf.New(), genId())
de, dh := initTestServer(conf.New(), genId(), false)
err := startServer(t, de)
c := conf.New()
c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
e, h, err := buildServer(t, c, bootstrapId)
e, h := initTestServer(c, bootstrapId, false)
err = startServer(t, e)
if err != nil {
t.Fatalf("build server err = %v, want nil", err)
}
@ -129,7 +137,8 @@ func TestRunByAdvisedPeers(t *testing.T) {
c := conf.New()
c.Peers = []string{hs[0].URL}
e, h, err := buildServer(t, c, bootstrapId)
e, h := initTestServer(c, bootstrapId, false)
err := startServer(t, e)
if err != nil {
t.Fatalf("build server err = %v, want nil", err)
}
@ -144,7 +153,8 @@ func TestRunByAdvisedPeers(t *testing.T) {
}
func TestRunByDiscoveryService(t *testing.T) {
de, dh, _ := buildServer(t, cfg.New(), genId())
de, dh := initTestServer(conf.New(), genId(), false)
err := startServer(t, de)
tc := NewTestClient()
v := url.Values{}
@ -162,9 +172,10 @@ func TestRunByDiscoveryService(t *testing.T) {
}
resp.Body.Close()
c := cfg.New()
c := conf.New()
c.Discovery = dh.URL + "/v2/keys/_etcd/registry/1"
e, h, err := buildServer(t, c, bootstrapId)
e, h := initTestServer(c, bootstrapId, false)
err = startServer(t, e)
if err != nil {
t.Fatalf("build server err = %v, want nil", err)
}
@ -182,18 +193,17 @@ func TestRunByDataDir(t *testing.T) {
TestSingleNodeRecovery(t)
}
func buildServer(t *testing.T, c *cfg.Config, id int64) (e *Server, h *httptest.Server, err error) {
e, h = initTestServer(c, id, false)
func startServer(t *testing.T, e *Server) error {
var err error
go func() { err = e.Run() }()
for {
if e.mode.Get() == participantMode {
break
}
if err != nil {
h.Close()
return nil, nil, err
return err
}
time.Sleep(10 * time.Millisecond)
}
return e, h, nil
return nil
}

View File

@ -90,7 +90,7 @@ func TestAdd(t *testing.T) {
es := make([]*Server, tt)
hs := make([]*httptest.Server, tt)
for i := 0; i < tt; i++ {
c := cfg.New()
c := conf.New()
if i > 0 {
c.Peers = []string{hs[0].URL}
}
@ -149,7 +149,7 @@ func TestRemove(t *testing.T) {
waitCluster(t, es)
lead, _ := waitLeader(es)
cfg := cfg.NewClusterConfig()
cfg := conf.NewClusterConfig()
cfg.ActiveSize = 0
if err := es[lead].p.setClusterConfig(cfg); err != nil {
t.Fatalf("#%d: setClusterConfig err = %v", k, err)
@ -216,7 +216,7 @@ func TestBecomeStandby(t *testing.T) {
}
id := int64(i)
cfg := cfg.NewClusterConfig()
cfg := conf.NewClusterConfig()
cfg.SyncInterval = 1000
cfg.ActiveSize = size - 1
@ -320,9 +320,10 @@ func TestSingleNodeRecovery(t *testing.T) {
if err != nil {
panic(err)
}
c := cfg.New()
c := conf.New()
c.DataDir = dataDir
e, h, _ := buildServer(t, c, id)
e, h := initTestServer(c, id, false)
startServer(t, e)
key := "/foo"
ev, err := e.p.Set(key, false, "bar", time.Now().Add(time.Second*100))
@ -348,9 +349,10 @@ func TestSingleNodeRecovery(t *testing.T) {
time.Sleep(2 * time.Second)
c = cfg.New()
c = conf.New()
c.DataDir = dataDir
e, h, _ = buildServer(t, c, id)
e, h = initTestServer(c, id, false)
startServer(t, e)
waitLeader([]*Server{e})
w, err = e.p.Watch(key, false, false, ev.Index())
@ -395,7 +397,7 @@ func TestRestoreSnapshotFromLeader(t *testing.T) {
}
// create one to join the cluster
c := cfg.New()
c := conf.New()
c.Peers = []string{hs[0].URL}
e, h := initTestServer(c, 1, false)
go e.Run()
@ -445,7 +447,7 @@ func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
var seed string
for i := range es {
c := cfg.New()
c := conf.New()
if seed != "" {
c.Peers = []string{seed}
}
@ -468,7 +470,7 @@ func buildCluster(number int, tls bool) ([]*Server, []*httptest.Server) {
return es, hs
}
func initTestServer(c *cfg.Config, id int64, tls bool) (e *Server, h *httptest.Server) {
func initTestServer(c *conf.Config, id int64, tls bool) (e *Server, h *httptest.Server) {
if c.DataDir == "" {
n, err := ioutil.TempDir(os.TempDir(), "etcd")
if err != nil {

View File

@ -136,16 +136,16 @@ func TestGetAdminConfigEndPoint(t *testing.T) {
t.Errorf("#%d: ContentType = %d, want application/json", i, g)
}
conf := new(conf.ClusterConfig)
err = json.NewDecoder(r.Body).Decode(conf)
cc := new(conf.ClusterConfig)
err = json.NewDecoder(r.Body).Decode(cc)
r.Body.Close()
if err != nil {
t.Errorf("%v", err)
continue
}
w := conf.NewClusterConfig()
if !reflect.DeepEqual(conf, w) {
t.Errorf("#%d: config = %+v, want %+v", i, conf, w)
if !reflect.DeepEqual(cc, w) {
t.Errorf("#%d: config = %+v, want %+v", i, cc, w)
}
}