Package list golang-github-go-kit-kit / a796b6c
Replace sleep with periodic checks Yuri Shkuro 4 years ago
1 changed file(s) with 21 addition(s) and 29 deletion(s). Raw diff Collapse all Expand all
4343 registrar1.Register()
4444 defer registrar1.Deregister()
4545
46 // This should be enough time for the Eureka server response cache to update.
47 time.Sleep(time.Second)
48
4946 // Build a Eureka instancer.
5047 instancer := NewInstancer(
5148 &fargoConnection,
5451 )
5552 defer instancer.Stop()
5653
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
5772 // 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)
6574
6675 // Register a second instance
6776 registrar2 := NewRegistrar(&fargoConnection, instanceTest2, log.With(logger, "component", "registrar2"))
7079
7180 // This should be enough time for a scheduled update assuming Eureka is
7281 // 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)
8383
8484 // Deregister the second instance.
8585 registrar2.Deregister()
8686
8787 // Wait for another scheduled update.
88 time.Sleep(2 * time.Second)
89
9088 // 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)
9890 }