discovery/kubernetes: Fix race in test setup

Signed-off-by: Frederic Branczyk <fbranczyk@gmail.com>
This commit is contained in:
Frederic Branczyk 2020-02-18 09:24:48 +01:00 committed by Julien Pivotto
parent ef63d8d16d
commit d06f1034db
1 changed files with 15 additions and 0 deletions

View File

@ -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)