discovery/kubernetes: improve test logic for waiting for discoverers (#9584)

When running tests in parallel, 10 milliseconds may not be enough for
all discoverers to register, which will make test flaky.

This commit changes the waiting logic to wait for number of discoverers
to stop increasing during given time frame, which should be large enough
for single discoverer to register in test environment.

A following run passes with this commit:

go test -failfast -race -count 100 -v ./discovery/kubernetes/

Signed-off-by: Mateusz Gozdek <mgozdekof@gmail.com>
This commit is contained in:
Mateusz Gozdek 2021-11-02 22:17:32 +01:00 committed by GitHub
parent c3beca72e2
commit ea924746b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 3 deletions

View File

@ -86,15 +86,18 @@ func (d k8sDiscoveryTest) Run(t *testing.T) {
// Ensure that discovery has a discoverer set. This prevents a race // Ensure that discovery has a discoverer set. This prevents a race
// condition where the above go routine may or may not have set a // condition where the above go routine may or may not have set a
// discoverer yet. // discoverer yet.
for { lastDiscoverersCount := 0
dis := d.discovery.(*Discovery) dis := d.discovery.(*Discovery)
for {
dis.RLock() dis.RLock()
l := len(dis.discoverers) l := len(dis.discoverers)
dis.RUnlock() dis.RUnlock()
if l > 0 { if l > 0 && l == lastDiscoverersCount {
break break
} }
time.Sleep(10 * time.Millisecond) time.Sleep(100 * time.Millisecond)
lastDiscoverersCount = l
} }
resChan := make(chan map[string]*targetgroup.Group) resChan := make(chan map[string]*targetgroup.Group)