From 523ff80287995ee74685c40d304732a3ae7af1ca Mon Sep 17 00:00:00 2001 From: ncabatoff Date: Thu, 30 Jan 2020 11:02:48 -0500 Subject: [PATCH] Removing timing-dependent aspects of test. (#8261) --- .../consul_service_registration_test.go | 42 ++++--------------- 1 file changed, 9 insertions(+), 33 deletions(-) diff --git a/serviceregistration/consul/consul_service_registration_test.go b/serviceregistration/consul/consul_service_registration_test.go index 3b5cb8a25..4e6d88d37 100644 --- a/serviceregistration/consul/consul_service_registration_test.go +++ b/serviceregistration/consul/consul_service_registration_test.go @@ -64,9 +64,9 @@ func TestConsul_ServiceRegistration(t *testing.T) { t.Fatal(err) } - // transitionFrom waits patiently for the services in the Consul catalog to - // transition from a known value, and then returns the new value. - transitionFrom := func(t *testing.T, known map[string][]string) map[string][]string { + // waitForServices waits for the services in the Consul catalog to + // reach an expected value, returning the delta if that doesn't happen in time. + waitForServices := func(t *testing.T, expected map[string][]string) map[string][]string { t.Helper() // Wait for up to 10 seconds for i := 0; i < 10; i++ { @@ -74,12 +74,12 @@ func TestConsul_ServiceRegistration(t *testing.T) { if err != nil { t.Fatal(err) } - if diff := deep.Equal(services, known); diff != nil { + if diff := deep.Equal(services, expected); diff == nil { return services } time.Sleep(time.Second) } - t.Fatalf("Catalog Services never transitioned from %v", known) + t.Fatalf("Catalog Services never reached expected state %v", expected) return nil } @@ -122,27 +122,10 @@ func TestConsul_ServiceRegistration(t *testing.T) { t.Fatal(err) } - // Vault should not yet be registered with Consul - services, _, err := client.Catalog().Services(nil) - if err != nil { - t.Fatal(err) - } - if diff := deep.Equal(services, map[string][]string{ - "consul": []string{}, - }); diff != nil { - t.Fatal(diff) - } - - // Vault should soon be registered with Consul in standby mode - services = transitionFrom(t, map[string][]string{ - "consul": []string{}, - }) - if diff := deep.Equal(services, map[string][]string{ + waitForServices(t, map[string][]string{ "consul": []string{}, "vault": []string{"standby"}, - }); diff != nil { - t.Fatal(diff) - } + }) // Initialize and unseal the core keys, _ := vault.TestCoreInit(t, core) @@ -158,17 +141,10 @@ func TestConsul_ServiceRegistration(t *testing.T) { // Wait for the core to become active vault.TestWaitActive(t, core) - // Vault should soon be registered with Consul in active mode - services = transitionFrom(t, map[string][]string{ - "consul": []string{}, - "vault": []string{"standby"}, - }) - if diff := deep.Equal(services, map[string][]string{ + waitForServices(t, map[string][]string{ "consul": []string{}, "vault": []string{"active"}, - }); diff != nil { - t.Fatal(diff) - } + }) } func TestConsul_ServiceTags(t *testing.T) {