diff --git a/consul/catalog_endpoint_test.go b/consul/catalog_endpoint_test.go index a553538b89..3f01b49db6 100644 --- a/consul/catalog_endpoint_test.go +++ b/consul/catalog_endpoint_test.go @@ -2,8 +2,8 @@ package consul import ( "fmt" - "github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/consul/structs" + "github.com/hashicorp/consul/testutil" "net/rpc" "os" "sort" @@ -254,8 +254,13 @@ func TestCatalogListNodes_StaleRaad(t *testing.T) { t.Fatalf("err: %v", err) } - // Wait for a leader - time.Sleep(100 * time.Millisecond) + args := structs.DCSpecificRequest{ + Datacenter: "dc1", + QueryOptions: structs.QueryOptions{AllowStale: true}, + } + + testutil.WaitForLeader(t, client1.Call, args) + testutil.WaitForLeader(t, client2.Call, args) // Use the follower as the client var client *rpc.Client @@ -271,10 +276,6 @@ func TestCatalogListNodes_StaleRaad(t *testing.T) { s2.fsm.State().EnsureNode(1, structs.Node{"foo", "127.0.0.1"}) } - args := structs.DCSpecificRequest{ - Datacenter: "dc1", - QueryOptions: structs.QueryOptions{AllowStale: true}, - } var out structs.IndexedNodes if err := client.Call("Catalog.ListNodes", &args, &out); err != nil { t.Fatalf("err: %v", err) diff --git a/testutil/wait.go b/testutil/wait.go index 6cc2d95597..78ef2d140c 100644 --- a/testutil/wait.go +++ b/testutil/wait.go @@ -27,14 +27,15 @@ func WaitForResult(test testFn, error errorFn) { } } -type clientRPC func(string, interface {}, interface {}) error +type rpcFn func(string, interface {}, interface {}) error -func WaitForLeader(t *testing.T, rpc clientRPC, args interface{}) { +func WaitForLeader(t *testing.T, rpc rpcFn, args interface{}) structs.IndexedNodes { + var out structs.IndexedNodes WaitForResult(func() (bool, error) { - var out structs.IndexedNodes err := rpc("Catalog.ListNodes", args, &out) return out.QueryMeta.KnownLeader, err }, func(err error) { t.Fatalf("failed to find leader: %v", err) }) + return out }