Remove all sleeps from server_test.go

This commit is contained in:
William Tisäter 2014-05-09 01:38:58 +02:00
parent 176e3eb9df
commit 4e9e14ff93

View File

@ -2,11 +2,13 @@ package consul
import ( import (
"fmt" "fmt"
"github.com/hashicorp/consul/testutil"
"io/ioutil" "io/ioutil"
"net" "net"
"os" "os"
"testing" "testing"
"time" "time"
"errors"
) )
var nextPort = 15000 var nextPort = 15000
@ -168,16 +170,16 @@ func TestServer_JoinWAN(t *testing.T) {
t.Fatalf("bad len") t.Fatalf("bad len")
} }
time.Sleep(10 * time.Millisecond)
// Check the remoteConsuls has both // Check the remoteConsuls has both
if len(s1.remoteConsuls) != 2 { if len(s1.remoteConsuls) != 2 {
t.Fatalf("remote consul missing") 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") t.Fatalf("remote consul missing")
} })
} }
func TestServer_Leave(t *testing.T) { func TestServer_Leave(t *testing.T) {
@ -197,17 +199,22 @@ func TestServer_Leave(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
time.Sleep(time.Second) var p1 []net.Addr
var p2 []net.Addr
p1, _ := s1.raftPeers.Peers() testutil.WaitForResult(func() (bool, error) {
if len(p1) != 2 { p1, _ = s1.raftPeers.Peers()
t.Fatalf("should have 2 peers: %v", p1) 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() testutil.WaitForResult(func() (bool, error) {
if len(p2) != 2 { p2, _ = s2.raftPeers.Peers()
t.Fatalf("should have 2 peers: %v", p2) return len(p2) == 2, errors.New(fmt.Sprintf("%v", p1))
} }, func(err error) {
t.Fatalf("should have 2 peers: %v", err)
})
// Issue a leave // Issue a leave
if err := s2.Leave(); err != nil { if err := s2.Leave(); err != nil {
@ -272,14 +279,16 @@ func TestServer_JoinLAN_TLS(t *testing.T) {
t.Fatalf("bad len") t.Fatalf("bad len")
} }
// Wait a while
time.Sleep(100 * time.Millisecond)
// Verify Raft has established a peer // Verify Raft has established a peer
if s1.Stats()["raft"]["num_peers"] != "1" { testutil.WaitForResult(func() (bool, error) {
t.Fatalf("bad: %v", s1.Stats()["raft"]) return s1.Stats()["raft"]["num_peers"] == "1", nil
} }, func(err error) {
if s2.Stats()["raft"]["num_peers"] != "1" { t.Fatalf("no peer established")
t.Fatalf("bad: %v", s2.Stats()["raft"]) })
}
testutil.WaitForResult(func() (bool, error) {
return s2.Stats()["raft"]["num_peers"] == "1", nil
}, func(err error) {
t.Fatalf("no peer established")
})
} }