Remove all sleeps from `leader_test.go`

This commit is contained in:
William Tisäter 2014-05-09 02:04:52 +02:00
parent 4e9e14ff93
commit f4597403e3
1 changed files with 66 additions and 102 deletions

View File

@ -7,7 +7,7 @@ import (
"github.com/hashicorp/serf/serf" "github.com/hashicorp/serf/serf"
"os" "os"
"testing" "testing"
"time" "errors"
) )
func TestLeader_RegisterMember(t *testing.T) { func TestLeader_RegisterMember(t *testing.T) {
@ -19,9 +19,6 @@ func TestLeader_RegisterMember(t *testing.T) {
defer os.RemoveAll(dir2) defer os.RemoveAll(dir2)
defer c1.Shutdown() defer c1.Shutdown()
client := rpcClient(t, s1)
testutil.WaitForLeader(t, client.Call, "dc1")
// Try to join // Try to join
addr := fmt.Sprintf("127.0.0.1:%d", addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfLANConfig.MemberlistConfig.BindPort) s1.config.SerfLANConfig.MemberlistConfig.BindPort)
@ -29,8 +26,8 @@ func TestLeader_RegisterMember(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Wait for registration client := rpcClient(t, s1)
time.Sleep(10 * time.Millisecond) testutil.WaitForLeader(t, client.Call, "dc1")
// Client should be registered // Client should be registered
state := s1.fsm.State() state := s1.fsm.State()
@ -76,8 +73,8 @@ func TestLeader_FailedMember(t *testing.T) {
defer os.RemoveAll(dir2) defer os.RemoveAll(dir2)
defer c1.Shutdown() defer c1.Shutdown()
// Wait until we have a leader client := rpcClient(t, s1)
time.Sleep(100 * time.Millisecond) testutil.WaitForLeader(t, client.Call, "dc1")
// Try to join // Try to join
addr := fmt.Sprintf("127.0.0.1:%d", addr := fmt.Sprintf("127.0.0.1:%d",
@ -89,9 +86,6 @@ func TestLeader_FailedMember(t *testing.T) {
// Fail the member // Fail the member
c1.Shutdown() c1.Shutdown()
// Wait for failure detection
time.Sleep(500 * time.Millisecond)
// Should be registered // Should be registered
state := s1.fsm.State() state := s1.fsm.State()
_, found, _ := state.GetNode(c1.config.NodeName) _, found, _ := state.GetNode(c1.config.NodeName)
@ -110,9 +104,13 @@ func TestLeader_FailedMember(t *testing.T) {
if checks[0].Name != SerfCheckName { if checks[0].Name != SerfCheckName {
t.Fatalf("bad check: %v", checks[0]) t.Fatalf("bad check: %v", checks[0])
} }
if checks[0].Status != structs.HealthCritical {
t.Fatalf("bad check: %v", checks[0]) testutil.WaitForResult(func() (bool, error) {
} _, checks = state.NodeChecks(c1.config.NodeName)
return checks[0].Status == structs.HealthCritical, errors.New(checks[0].Status)
}, func(err error) {
t.Fatalf("check status is %v, should be critical", err)
})
} }
func TestLeader_LeftMember(t *testing.T) { func TestLeader_LeftMember(t *testing.T) {
@ -124,9 +122,6 @@ func TestLeader_LeftMember(t *testing.T) {
defer os.RemoveAll(dir2) defer os.RemoveAll(dir2)
defer c1.Shutdown() defer c1.Shutdown()
// Wait until we have a leader
time.Sleep(100 * time.Millisecond)
// Try to join // Try to join
addr := fmt.Sprintf("127.0.0.1:%d", addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfLANConfig.MemberlistConfig.BindPort) s1.config.SerfLANConfig.MemberlistConfig.BindPort)
@ -134,28 +129,28 @@ func TestLeader_LeftMember(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Wait for registration var found bool
time.Sleep(10 * time.Millisecond) state := s1.fsm.State()
// Should be registered // Should be registered
state := s1.fsm.State() testutil.WaitForResult(func() (bool, error) {
_, found, _ := state.GetNode(c1.config.NodeName) _, found, _ = state.GetNode(c1.config.NodeName)
if !found { return found == true, nil
t.Fatalf("client not registered") }, func(err error) {
} t.Fatalf("client should be registered")
})
// Node should leave // Node should leave
c1.Leave() c1.Leave()
c1.Shutdown() c1.Shutdown()
// Wait for failure detection
time.Sleep(500 * time.Millisecond)
// Should be deregistered // Should be deregistered
_, found, _ = state.GetNode(c1.config.NodeName) testutil.WaitForResult(func() (bool, error) {
if found { _, found, _ = state.GetNode(c1.config.NodeName)
t.Fatalf("client registered") return found == false, nil
} }, func(err error) {
t.Fatalf("client should not be registered")
})
} }
func TestLeader_ReapMember(t *testing.T) { func TestLeader_ReapMember(t *testing.T) {
@ -167,9 +162,6 @@ func TestLeader_ReapMember(t *testing.T) {
defer os.RemoveAll(dir2) defer os.RemoveAll(dir2)
defer c1.Shutdown() defer c1.Shutdown()
// Wait until we have a leader
time.Sleep(100 * time.Millisecond)
// Try to join // Try to join
addr := fmt.Sprintf("127.0.0.1:%d", addr := fmt.Sprintf("127.0.0.1:%d",
s1.config.SerfLANConfig.MemberlistConfig.BindPort) s1.config.SerfLANConfig.MemberlistConfig.BindPort)
@ -177,15 +169,16 @@ func TestLeader_ReapMember(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Wait for registration var found bool
time.Sleep(10 * time.Millisecond) state := s1.fsm.State()
// Should be registered // Should be registered
state := s1.fsm.State() testutil.WaitForResult(func() (bool, error) {
_, found, _ := state.GetNode(c1.config.NodeName) _, found, _ = state.GetNode(c1.config.NodeName)
if !found { return found == true, nil
t.Fatalf("client not registered") }, func(err error) {
} t.Fatalf("client should be registered")
})
// Simulate a node reaping // Simulate a node reaping
mems := s1.LANMembers() mems := s1.LANMembers()
@ -199,14 +192,13 @@ func TestLeader_ReapMember(t *testing.T) {
} }
s1.reconcileCh <- c1mem s1.reconcileCh <- c1mem
// Wait to reconcile
time.Sleep(10 * time.Millisecond)
// Should be deregistered // Should be deregistered
_, found, _ = state.GetNode(c1.config.NodeName) testutil.WaitForResult(func() (bool, error) {
if found { _, found, _ = state.GetNode(c1.config.NodeName)
t.Fatalf("client registered") return found == false, nil
} }, func(err error) {
t.Fatalf("client should not be registered")
})
} }
func TestLeader_Reconcile_ReapMember(t *testing.T) { func TestLeader_Reconcile_ReapMember(t *testing.T) {
@ -214,8 +206,8 @@ func TestLeader_Reconcile_ReapMember(t *testing.T) {
defer os.RemoveAll(dir1) defer os.RemoveAll(dir1)
defer s1.Shutdown() defer s1.Shutdown()
// Wait until we have a leader client := rpcClient(t, s1)
time.Sleep(100 * time.Millisecond) testutil.WaitForLeader(t, client.Call, "dc1")
// Register a non-existing member // Register a non-existing member
dead := structs.RegisterRequest{ dead := structs.RegisterRequest{
@ -270,14 +262,13 @@ func TestLeader_Reconcile(t *testing.T) {
t.Fatalf("client registered") t.Fatalf("client registered")
} }
// Wait for leader
time.Sleep(100 * time.Millisecond)
// Should be registered // Should be registered
_, found, _ = state.GetNode(c1.config.NodeName) testutil.WaitForResult(func() (bool, error) {
if !found { _, found, _ = state.GetNode(c1.config.NodeName)
t.Fatalf("client not registered") return found == true, nil
} }, func(err error) {
t.Fatalf("client should be registered")
})
} }
func TestLeader_LeftServer(t *testing.T) { func TestLeader_LeftServer(t *testing.T) {
@ -304,48 +295,30 @@ func TestLeader_LeftServer(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Wait until we have 3 peers
start := time.Now()
CHECK1:
for _, s := range servers { for _, s := range servers {
peers, _ := s.raftPeers.Peers() testutil.WaitForResult(func() (bool, error) {
if len(peers) != 3 { peers, _ := s.raftPeers.Peers()
if time.Now().Sub(start) >= 2*time.Second { return len(peers) == 3, nil
t.Fatalf("should have 3 peers") }, func(err error) {
} else { t.Fatalf("should have 3 peers")
time.Sleep(100 * time.Millisecond) })
goto CHECK1
}
}
} }
// Kill any server // Kill any server
servers[0].Shutdown() servers[0].Shutdown()
// Wait for failure detection
time.Sleep(500 * time.Millisecond)
// Force remove the non-leader (transition to left state) // Force remove the non-leader (transition to left state)
if err := servers[1].RemoveFailedNode(servers[0].config.NodeName); err != nil { if err := servers[1].RemoveFailedNode(servers[0].config.NodeName); err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Wait for intent propagation
time.Sleep(500 * time.Millisecond)
// Wait until we have 2 peers
start = time.Now()
CHECK2:
for _, s := range servers[1:] { for _, s := range servers[1:] {
peers, _ := s.raftPeers.Peers() testutil.WaitForResult(func() (bool, error) {
if len(peers) != 2 { peers, _ := s.raftPeers.Peers()
if time.Now().Sub(start) >= 2*time.Second { return len(peers) == 2, nil
t.Fatalf("should have 2 peers") }, func(err error) {
} else { t.Fatalf("should have 2 peers")
time.Sleep(100 * time.Millisecond) })
goto CHECK2
}
}
} }
} }
@ -367,24 +340,15 @@ func TestLeader_MultiBootstrap(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Wait until we have 2 peers
start := time.Now()
CHECK1:
for _, s := range servers { for _, s := range servers {
peers := s.serfLAN.Members() testutil.WaitForResult(func() (bool, error) {
if len(peers) != 2 { peers := s.serfLAN.Members()
if time.Now().Sub(start) >= 2*time.Second { return len(peers) == 2, nil
t.Fatalf("should have 2 peers") }, func(err error) {
} else { t.Fatalf("should have 2 peers")
time.Sleep(100 * time.Millisecond) })
goto CHECK1
}
}
} }
// Wait to ensure no peer is added
time.Sleep(200 * time.Millisecond)
// Ensure we don't have multiple raft peers // Ensure we don't have multiple raft peers
for _, s := range servers { for _, s := range servers {
peers, _ := s.raftPeers.Peers() peers, _ := s.raftPeers.Peers()