Merge pull request #548 from yurishkuro/ys-fix-eureka-tests
Replace sleep with periodic checks
Peter Bourgon authored 6 years ago
GitHub committed 6 years ago
43 | 43 | registrar1.Register() |
44 | 44 | defer registrar1.Deregister() |
45 | 45 | |
46 | // This should be enough time for the Eureka server response cache to update. | |
47 | time.Sleep(time.Second) | |
48 | ||
49 | 46 | // Build a Eureka instancer. |
50 | 47 | instancer := NewInstancer( |
51 | 48 | &fargoConnection, |
54 | 51 | ) |
55 | 52 | defer instancer.Stop() |
56 | 53 | |
54 | // checks every 100ms (fr up to 10s) for the expected number of instances to be reported | |
55 | waitForInstances := func(count int) { | |
56 | for t := 0; t < 100; t++ { | |
57 | state := instancer.state() | |
58 | if len(state.Instances) == count { | |
59 | return | |
60 | } | |
61 | time.Sleep(100 * time.Millisecond) | |
62 | } | |
63 | state := instancer.state() | |
64 | if state.Err != nil { | |
65 | t.Error(state.Err) | |
66 | } | |
67 | if want, have := 1, len(state.Instances); want != have { | |
68 | t.Errorf("want %d, have %d", want, have) | |
69 | } | |
70 | } | |
71 | ||
57 | 72 | // We should have one instance immediately after subscriber instantiation. |
58 | state := instancer.state() | |
59 | if state.Err != nil { | |
60 | t.Error(state.Err) | |
61 | } | |
62 | if want, have := 1, len(state.Instances); want != have { | |
63 | t.Errorf("want %d, have %d", want, have) | |
64 | } | |
73 | waitForInstances(1) | |
65 | 74 | |
66 | 75 | // Register a second instance |
67 | 76 | registrar2 := NewRegistrar(&fargoConnection, instanceTest2, log.With(logger, "component", "registrar2")) |
70 | 79 | |
71 | 80 | // This should be enough time for a scheduled update assuming Eureka is |
72 | 81 | // configured with the properties mentioned in the function comments. |
73 | time.Sleep(2 * time.Second) | |
74 | ||
75 | // Now we should have two instances. | |
76 | state = instancer.state() | |
77 | if state.Err != nil { | |
78 | t.Error(state.Err) | |
79 | } | |
80 | if want, have := 2, len(state.Instances); want != have { | |
81 | t.Errorf("want %d, have %d", want, have) | |
82 | } | |
82 | waitForInstances(2) | |
83 | 83 | |
84 | 84 | // Deregister the second instance. |
85 | 85 | registrar2.Deregister() |
86 | 86 | |
87 | 87 | // Wait for another scheduled update. |
88 | time.Sleep(2 * time.Second) | |
89 | ||
90 | 88 | // And then there was one. |
91 | state = instancer.state() | |
92 | if state.Err != nil { | |
93 | t.Error(state.Err) | |
94 | } | |
95 | if want, have := 1, len(state.Instances); want != have { | |
96 | t.Errorf("want %d, have %d", want, have) | |
97 | } | |
89 | waitForInstances(1) | |
98 | 90 | } |