diff --git a/command/agent/catalog_endpoint_test.go b/command/agent/catalog_endpoint_test.go index 2d6dc39d4e..fa553d112a 100644 --- a/command/agent/catalog_endpoint_test.go +++ b/command/agent/catalog_endpoint_test.go @@ -2,6 +2,7 @@ package agent import ( "fmt" + "github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/consul/structs" "net/http" "net/http/httptest" @@ -16,18 +17,19 @@ func TestCatalogRegister(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Wait for a leader - time.Sleep(100 * time.Millisecond) + args := &structs.RegisterRequest{ + Datacenter: "dc1", + Node: "foo", + Address: "127.0.0.1", + } + + testutil.WaitForLeader(t, srv.agent.RPC, args) // Register node req, err := http.NewRequest("GET", "/v1/catalog/register", nil) if err != nil { t.Fatalf("err: %v", err) } - args := &structs.RegisterRequest{ - Node: "foo", - Address: "127.0.0.1", - } req.Body = encodeReq(args) obj, err := srv.CatalogRegister(nil, req) @@ -47,17 +49,18 @@ func TestCatalogDeregister(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Wait for a leader - time.Sleep(100 * time.Millisecond) + args := &structs.DeregisterRequest{ + Datacenter: "dc1", + Node: "foo", + } + + testutil.WaitForLeader(t, srv.agent.RPC, args) // Register node req, err := http.NewRequest("GET", "/v1/catalog/deregister", nil) if err != nil { t.Fatalf("err: %v", err) } - args := &structs.DeregisterRequest{ - Node: "foo", - } req.Body = encodeReq(args) obj, err := srv.CatalogDeregister(nil, req) @@ -77,9 +80,6 @@ func TestCatalogDatacenters(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Wait for initialization - time.Sleep(10 * time.Millisecond) - obj, err := srv.CatalogDatacenters(nil, nil) if err != nil { t.Fatalf("err: %v", err) @@ -97,15 +97,15 @@ func TestCatalogNodes(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Wait for a leader - time.Sleep(100 * time.Millisecond) - - // Register node args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", Address: "127.0.0.1", } + + testutil.WaitForLeader(t, srv.agent.RPC, args) + + // Register node var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { t.Fatalf("err: %v", err) @@ -137,13 +137,13 @@ func TestCatalogNodes_Blocking(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Wait for a leader - time.Sleep(100 * time.Millisecond) - - // Register node args := &structs.DCSpecificRequest{ Datacenter: "dc1", } + + testutil.WaitForLeader(t, srv.agent.RPC, args) + + // Register node var out structs.IndexedNodes if err := srv.agent.RPC("Catalog.ListNodes", *args, &out); err != nil { t.Fatalf("err: %v", err) @@ -152,7 +152,7 @@ func TestCatalogNodes_Blocking(t *testing.T) { // Do an update in a little while start := time.Now() go func() { - time.Sleep(100 * time.Millisecond) + time.Sleep(50 * time.Millisecond) args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", @@ -178,7 +178,7 @@ func TestCatalogNodes_Blocking(t *testing.T) { } // Should block for a while - if time.Now().Sub(start) < 100*time.Millisecond { + if time.Now().Sub(start) < 50 * time.Millisecond { t.Fatalf("too fast") } @@ -198,10 +198,6 @@ func TestCatalogServices(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Wait for a leader - time.Sleep(100 * time.Millisecond) - - // Register node args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", @@ -210,6 +206,10 @@ func TestCatalogServices(t *testing.T) { Service: "api", }, } + + testutil.WaitForLeader(t, srv.agent.RPC, args) + + // Register node var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { t.Fatalf("err: %v", err) @@ -240,10 +240,6 @@ func TestCatalogServiceNodes(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Wait for a leader - time.Sleep(100 * time.Millisecond) - - // Register node args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", @@ -253,6 +249,10 @@ func TestCatalogServiceNodes(t *testing.T) { Tags: []string{"a"}, }, } + + testutil.WaitForLeader(t, srv.agent.RPC, args) + + // Register node var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { t.Fatalf("err: %v", err) @@ -283,10 +283,6 @@ func TestCatalogNodeServices(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - // Wait for a leader - time.Sleep(100 * time.Millisecond) - - // Register node args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", @@ -296,6 +292,10 @@ func TestCatalogNodeServices(t *testing.T) { Tags: []string{"a"}, }, } + + testutil.WaitForLeader(t, srv.agent.RPC, args) + + // Register node var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { t.Fatalf("err: %v", err)