From dcff5137f98f29cb617a8747dcb26a3180a1fbfa Mon Sep 17 00:00:00 2001 From: James Phillips Date: Thu, 26 Jan 2017 19:05:56 -0800 Subject: [PATCH] Adds a little wait after we get the event we were seeking. --- testutil/server.go | 1 - testutil/wait.go | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/testutil/server.go b/testutil/server.go index f97db74440..22c1138ec8 100644 --- a/testutil/server.go +++ b/testutil/server.go @@ -336,7 +336,6 @@ func (s *TestServer) waitForLeader() { if _, ok := taggedAddresses["lan"]; !ok { return false, fmt.Errorf("No lan tagged addresses") } - return true, nil }, func(err error) { defer s.Stop() diff --git a/testutil/wait.go b/testutil/wait.go index f32562994c..bd240796ff 100644 --- a/testutil/wait.go +++ b/testutil/wait.go @@ -1,6 +1,7 @@ package testutil import ( + "fmt" "testing" "time" @@ -22,6 +23,7 @@ func WaitForResult(try testFn, fail errorFn) { var success bool success, err = try() if success { + time.Sleep(25 * time.Millisecond) return } @@ -39,11 +41,20 @@ type rpcFn func(string, interface{}, interface{}) error func WaitForLeader(t *testing.T, rpc rpcFn, dc string) structs.IndexedNodes { var out structs.IndexedNodes WaitForResult(func() (bool, error) { + // Ensure we have a leader and a node registration. args := &structs.DCSpecificRequest{ Datacenter: dc, } - err := rpc("Catalog.ListNodes", args, &out) - return out.QueryMeta.KnownLeader && out.Index > 0, err + if err := rpc("Catalog.ListNodes", args, &out); err != nil { + return false, fmt.Errorf("Catalog.ListNodes failed: %v", err) + } + if !out.QueryMeta.KnownLeader { + return false, fmt.Errorf("No leader") + } + if out.Index == 0 { + return false, fmt.Errorf("Consul index is 0") + } + return true, nil }, func(err error) { t.Fatalf("failed to find leader: %v", err) })