discovery/kubernetes: Fix race in test setup
@@ -65,6 +65,21 @@ func (d k8sDiscoveryTest) Run(t *testing.T) {
// Run discoverer and start a goroutine to read results.
go d.discovery.Run(ctx, ch)
+
+ // Ensure that discovery has a discoverer set. This prevents a race
+ // condition where the above go routine may or may not have set a
+ // discoverer yet.
+ for {
+ dis := d.discovery.(*Discovery)
+ dis.RLock()
+ l := len(dis.discoverers)
+ dis.RUnlock()
+ if l > 0 {
+ break
+ }
+ time.Sleep(10 * time.Millisecond)
+ }
+
resChan := make(chan map[string]*targetgroup.Group)
go readResultWithTimeout(t, ch, d.expectedMaxItems, time.Second, resChan)
