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
1 changed files with 31 additions and 22 deletions

View File

@ -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")
})
}