From bdbe97545ae4b35f7ed7440d2669f4390a0c1424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20Tis=C3=A4ter?= Date: Wed, 7 May 2014 23:41:14 +0200 Subject: [PATCH] Drop client arguments from `WaitForLeader` --- command/agent/catalog_endpoint_test.go | 42 +++++++++++++------------- command/agent/dns_test.go | 22 +++++++------- command/agent/health_endpoint_test.go | 22 +++----------- command/agent/kvs_endpoint_test.go | 24 +++------------ command/agent/local_test.go | 4 +-- consul/catalog_endpoint_test.go | 4 +-- consul/leader_test.go | 5 +-- testutil/wait.go | 5 ++- 8 files changed, 52 insertions(+), 76 deletions(-) diff --git a/command/agent/catalog_endpoint_test.go b/command/agent/catalog_endpoint_test.go index fa553d112..9ae11c4db 100644 --- a/command/agent/catalog_endpoint_test.go +++ b/command/agent/catalog_endpoint_test.go @@ -17,15 +17,15 @@ func TestCatalogRegister(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() + testutil.WaitForLeader(t, srv.agent.RPC) + + // Register node 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) @@ -49,14 +49,14 @@ func TestCatalogDeregister(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() + testutil.WaitForLeader(t, srv.agent.RPC) + + // Register node 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) @@ -97,15 +97,15 @@ func TestCatalogNodes(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() + testutil.WaitForLeader(t, srv.agent.RPC) + + // 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() + testutil.WaitForLeader(t, srv.agent.RPC) + + // 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) @@ -198,6 +198,9 @@ func TestCatalogServices(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() + testutil.WaitForLeader(t, srv.agent.RPC) + + // Register node args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", @@ -207,9 +210,6 @@ func TestCatalogServices(t *testing.T) { }, } - 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,6 +240,9 @@ func TestCatalogServiceNodes(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() + testutil.WaitForLeader(t, srv.agent.RPC) + + // Register node args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", @@ -250,9 +253,6 @@ func TestCatalogServiceNodes(t *testing.T) { }, } - 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,6 +283,9 @@ func TestCatalogNodeServices(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() + testutil.WaitForLeader(t, srv.agent.RPC) + + // Register node args := &structs.RegisterRequest{ Datacenter: "dc1", Node: "foo", @@ -293,9 +296,6 @@ func TestCatalogNodeServices(t *testing.T) { }, } - 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) diff --git a/command/agent/dns_test.go b/command/agent/dns_test.go index e874e3f64..f1b0ba46a 100644 --- a/command/agent/dns_test.go +++ b/command/agent/dns_test.go @@ -72,7 +72,7 @@ func TestDNS_NodeLookup(t *testing.T) { Address: "127.0.0.1", } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -136,7 +136,7 @@ func TestDNS_NodeLookup_PeriodName(t *testing.T) { Address: "127.0.0.1", } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -178,7 +178,7 @@ func TestDNS_NodeLookup_AAAA(t *testing.T) { Address: "::4242:4242", } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -220,7 +220,7 @@ func TestDNS_NodeLookup_CNAME(t *testing.T) { Address: "www.google.com", } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -268,7 +268,7 @@ func TestDNS_ServiceLookup(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -329,7 +329,7 @@ func TestDNS_ServiceLookup_TagPeriod(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -390,7 +390,7 @@ func TestDNS_ServiceLookup_Dedup(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -467,7 +467,7 @@ func TestDNS_ServiceLookup_Dedup_SRV(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -601,7 +601,7 @@ func TestDNS_ServiceLookup_FilterCritical(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -660,7 +660,7 @@ func TestDNS_ServiceLookup_Randomize(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { @@ -723,7 +723,7 @@ func TestDNS_ServiceLookup_CNAME(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) var out struct{} if err := srv.agent.RPC("Catalog.Register", args, &out); err != nil { diff --git a/command/agent/health_endpoint_test.go b/command/agent/health_endpoint_test.go index 2f669846c..10f84be9d 100644 --- a/command/agent/health_endpoint_test.go +++ b/command/agent/health_endpoint_test.go @@ -16,11 +16,7 @@ func TestHealthChecksInState(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - args := &structs.RegisterRequest{ - Datacenter: "dc1", - } - - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) req, err := http.NewRequest("GET", "/v1/health/state/passing?dc=dc1", nil) if err != nil { @@ -47,11 +43,7 @@ func TestHealthNodeChecks(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - args := &structs.RegisterRequest{ - Datacenter: "dc1", - } - - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) req, err := http.NewRequest("GET", fmt.Sprintf("/v1/health/node/%s?dc=dc1", srv.agent.config.NodeName), nil) @@ -90,7 +82,7 @@ func TestHealthServiceChecks(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) // Create a service check var out struct{} @@ -123,11 +115,7 @@ func TestHealthServiceNodes(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - args := &structs.RegisterRequest{ - Datacenter: "dc1", - } - - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) req, err := http.NewRequest("GET", "/v1/health/service/consul?dc=dc1", nil) if err != nil { @@ -166,7 +154,7 @@ func TestHealthServiceNodes_PassingFilter(t *testing.T) { }, } - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) // Create a failing service check var out struct{} diff --git a/command/agent/kvs_endpoint_test.go b/command/agent/kvs_endpoint_test.go index 97851573c..3ccc12bdf 100644 --- a/command/agent/kvs_endpoint_test.go +++ b/command/agent/kvs_endpoint_test.go @@ -18,11 +18,7 @@ func TestKVSEndpoint_PUT_GET_DELETE(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - args := &structs.RegisterRequest{ - Datacenter: "dc1", - } - - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) keys := []string{ "baz", @@ -97,11 +93,7 @@ func TestKVSEndpoint_Recurse(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - args := &structs.RegisterRequest{ - Datacenter: "dc1", - } - - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) keys := []string{ "bar", @@ -197,11 +189,7 @@ func TestKVSEndpoint_CAS(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - args := &structs.RegisterRequest{ - Datacenter: "dc1", - } - - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) { buf := bytes.NewBuffer([]byte("test")) @@ -298,11 +286,7 @@ func TestKVSEndpoint_ListKeys(t *testing.T) { defer srv.Shutdown() defer srv.agent.Shutdown() - args := &structs.RegisterRequest{ - Datacenter: "dc1", - } - - testutil.WaitForLeader(t, srv.agent.RPC, args) + testutil.WaitForLeader(t, srv.agent.RPC) keys := []string{ "bar", diff --git a/command/agent/local_test.go b/command/agent/local_test.go index 8c3f8bda9..69a85a8e5 100644 --- a/command/agent/local_test.go +++ b/command/agent/local_test.go @@ -21,7 +21,7 @@ func TestAgentAntiEntropy_Services(t *testing.T) { Address: "127.0.0.1", } - testutil.WaitForLeader(t, agent.RPC, args) + testutil.WaitForLeader(t, agent.RPC) // Register info. Exists both, same (noop) var out struct{} @@ -142,7 +142,7 @@ func TestAgentAntiEntropy_Checks(t *testing.T) { Address: "127.0.0.1", } - testutil.WaitForLeader(t, agent.RPC, args) + testutil.WaitForLeader(t, agent.RPC) // Register info. Exists both, same (noop) var out struct{} diff --git a/consul/catalog_endpoint_test.go b/consul/catalog_endpoint_test.go index 3f01b49db..66059b5e7 100644 --- a/consul/catalog_endpoint_test.go +++ b/consul/catalog_endpoint_test.go @@ -259,8 +259,8 @@ func TestCatalogListNodes_StaleRaad(t *testing.T) { QueryOptions: structs.QueryOptions{AllowStale: true}, } - testutil.WaitForLeader(t, client1.Call, args) - testutil.WaitForLeader(t, client2.Call, args) + testutil.WaitForLeader(t, client1.Call) + testutil.WaitForLeader(t, client2.Call) // Use the follower as the client var client *rpc.Client diff --git a/consul/leader_test.go b/consul/leader_test.go index f1ea3b0db..09e1856e5 100644 --- a/consul/leader_test.go +++ b/consul/leader_test.go @@ -2,6 +2,7 @@ package consul import ( "fmt" + "github.com/hashicorp/consul/testutil" "github.com/hashicorp/consul/consul/structs" "github.com/hashicorp/serf/serf" "os" @@ -18,8 +19,8 @@ func TestLeader_RegisterMember(t *testing.T) { defer os.RemoveAll(dir2) defer c1.Shutdown() - // Wait until we have a leader - time.Sleep(100 * time.Millisecond) + client := rpcClient(t, s1) + testutil.WaitForLeader(t, client.Call) // Try to join addr := fmt.Sprintf("127.0.0.1:%d", diff --git a/testutil/wait.go b/testutil/wait.go index 78ef2d140..20ab8ea5e 100644 --- a/testutil/wait.go +++ b/testutil/wait.go @@ -29,9 +29,12 @@ func WaitForResult(test testFn, error errorFn) { type rpcFn func(string, interface {}, interface {}) error -func WaitForLeader(t *testing.T, rpc rpcFn, args interface{}) structs.IndexedNodes { +func WaitForLeader(t *testing.T, rpc rpcFn) structs.IndexedNodes { var out structs.IndexedNodes WaitForResult(func() (bool, error) { + args := &structs.RegisterRequest{ + Datacenter: "dc1", + } err := rpc("Catalog.ListNodes", args, &out) return out.QueryMeta.KnownLeader, err }, func(err error) {