From 4e9e14ff93232bc7ae9d7f3dfc0251a46737bd6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Tisa=CC=88ter?= Date: Fri, 9 May 2014 01:38:58 +0200 Subject: [PATCH] Remove all sleeps from `server_test.go` --- consul/server_test.go | 53 +++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/consul/server_test.go b/consul/server_test.go index 45edac53d9..1b03cf83de 100644 --- a/consul/server_test.go +++ b/consul/server_test.go @@ -2,11 +2,13 @@ package consul import ( "fmt" + "github.com/hashicorp/consul/testutil" "io/ioutil" "net" "os" "testing" "time" + "errors" ) var nextPort = 15000 @@ -168,16 +170,16 @@ func TestServer_JoinWAN(t *testing.T) { t.Fatalf("bad len") } - time.Sleep(10 * time.Millisecond) - // Check the remoteConsuls has both if len(s1.remoteConsuls) != 2 { t.Fatalf("remote consul missing") } - if len(s2.remoteConsuls) != 2 { + testutil.WaitForResult(func() (bool, error) { + return len(s2.remoteConsuls) == 2, nil + }, func(err error) { t.Fatalf("remote consul missing") - } + }) } func TestServer_Leave(t *testing.T) { @@ -197,17 +199,22 @@ func TestServer_Leave(t *testing.T) { t.Fatalf("err: %v", err) } - time.Sleep(time.Second) + var p1 []net.Addr + var p2 []net.Addr - p1, _ := s1.raftPeers.Peers() - if len(p1) != 2 { - t.Fatalf("should have 2 peers: %v", p1) - } + testutil.WaitForResult(func() (bool, error) { + p1, _ = s1.raftPeers.Peers() + return len(p1) == 2, errors.New(fmt.Sprintf("%v", p1)) + }, func(err error) { + t.Fatalf("should have 2 peers: %v", err) + }) - p2, _ := s2.raftPeers.Peers() - if len(p2) != 2 { - t.Fatalf("should have 2 peers: %v", p2) - } + testutil.WaitForResult(func() (bool, error) { + p2, _ = s2.raftPeers.Peers() + return len(p2) == 2, errors.New(fmt.Sprintf("%v", p1)) + }, func(err error) { + t.Fatalf("should have 2 peers: %v", err) + }) // Issue a leave if err := s2.Leave(); err != nil { @@ -272,14 +279,16 @@ func TestServer_JoinLAN_TLS(t *testing.T) { t.Fatalf("bad len") } - // Wait a while - time.Sleep(100 * time.Millisecond) - // Verify Raft has established a peer - if s1.Stats()["raft"]["num_peers"] != "1" { - t.Fatalf("bad: %v", s1.Stats()["raft"]) - } - if s2.Stats()["raft"]["num_peers"] != "1" { - t.Fatalf("bad: %v", s2.Stats()["raft"]) - } + testutil.WaitForResult(func() (bool, error) { + return s1.Stats()["raft"]["num_peers"] == "1", nil + }, func(err error) { + t.Fatalf("no peer established") + }) + + testutil.WaitForResult(func() (bool, error) { + return s2.Stats()["raft"]["num_peers"] == "1", nil + }, func(err error) { + t.Fatalf("no peer established") + }) }