From a796b6c069024e3005ebdbb27689eb51c53e2fdf Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Tue, 6 Jun 2017 10:45:17 -0400 Subject: [PATCH] Replace sleep with periodic checks --- sd/eureka/integration_test.go | 50 +++++++++++++++-------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/sd/eureka/integration_test.go b/sd/eureka/integration_test.go index 3cd6cae50..210882b44 100644 --- a/sd/eureka/integration_test.go +++ b/sd/eureka/integration_test.go @@ -44,9 +44,6 @@ func TestIntegration(t *testing.T) { registrar1.Register() defer registrar1.Deregister() - // This should be enough time for the Eureka server response cache to update. - time.Sleep(time.Second) - // Build a Eureka instancer. instancer := NewInstancer( &fargoConnection, @@ -55,15 +52,27 @@ func TestIntegration(t *testing.T) { ) defer instancer.Stop() - // We should have one instance immediately after subscriber instantiation. - state := instancer.state() - if state.Err != nil { - t.Error(state.Err) - } - if want, have := 1, len(state.Instances); want != have { - t.Errorf("want %d, have %d", want, have) + // checks every 100ms (fr up to 10s) for the expected number of instances to be reported + waitForInstances := func(count int) { + for t := 0; t < 100; t++ { + state := instancer.state() + if len(state.Instances) == count { + return + } + time.Sleep(100 * time.Millisecond) + } + state := instancer.state() + if state.Err != nil { + t.Error(state.Err) + } + if want, have := 1, len(state.Instances); want != have { + t.Errorf("want %d, have %d", want, have) + } } + // We should have one instance immediately after subscriber instantiation. + waitForInstances(1) + // Register a second instance registrar2 := NewRegistrar(&fargoConnection, instanceTest2, log.With(logger, "component", "registrar2")) registrar2.Register() @@ -71,29 +80,12 @@ func TestIntegration(t *testing.T) { // This should be enough time for a scheduled update assuming Eureka is // configured with the properties mentioned in the function comments. - time.Sleep(2 * time.Second) - - // Now we should have two instances. - state = instancer.state() - if state.Err != nil { - t.Error(state.Err) - } - if want, have := 2, len(state.Instances); want != have { - t.Errorf("want %d, have %d", want, have) - } + waitForInstances(2) // Deregister the second instance. registrar2.Deregister() // Wait for another scheduled update. - time.Sleep(2 * time.Second) - // And then there was one. - state = instancer.state() - if state.Err != nil { - t.Error(state.Err) - } - if want, have := 1, len(state.Instances); want != have { - t.Errorf("want %d, have %d", want, have) - } + waitForInstances(1) }