Skip to content

Commit

Permalink
test: fix race condition in daemon_test
Browse files Browse the repository at this point in the history
This patch stabilizes newly added TestRunWithLabelRefresh and TestRunWithoutLabelRefresh
by using waitForStarted to ensure that first load of HostInfo has
happened before new is changed.

And especially it adds a little wait to `WaitForCalled` to solve a race
condition where a test code might check new HostInfo when it was not yet
replaced with the newly loaded one. This also fixes
`TestReloadOnCertChange` test which has also failed when the suite was run
1000 times.

With this code, `go test --failfast --count 1000  ./daemon/` was
successful on my test vm.

Signed-off-by: Petr Vobornik <[email protected]>
  • Loading branch information
pvoborni committed Nov 22, 2023
1 parent 6cf0df3 commit 4abb731
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions daemon/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func TestRunWithLabelRefresh(t *testing.T) {
daemon.config.LabelRefreshInterval = 5 * time.Millisecond

go daemon.Run()
checkRunning(t, daemon)
waitForStarted(t, daemon)

// Check initial labels are set to Mock default
if daemon.hostInfo.Product != "testproduct" || daemon.hostInfo.Support != "testsupport" {
Expand All @@ -265,7 +265,7 @@ func TestRunWithLabelRefresh(t *testing.T) {
newHostInfo.ResetCalled()
newHostInfo.WaitForCalled(t, 1)
if daemon.hostInfo.Product != "anotherproduct" || daemon.hostInfo.Support != "premium" {
t.Fatalf("expected label refresh on label refresh interval")
t.Fatalf("expected label refresh on label refresh interval, got: %s, %s", daemon.hostInfo.Product, daemon.hostInfo.Support)
}

// Cleanup
Expand All @@ -278,7 +278,7 @@ func TestRunWithoutLabelRefresh(t *testing.T) {
daemon.config.LabelRefreshInterval = 0

go daemon.Run()
checkRunning(t, daemon)
waitForStarted(t, daemon)

// Check initial labels are set to Mock default
if daemon.hostInfo.Product != "testproduct" || daemon.hostInfo.Support != "testsupport" {
Expand Down Expand Up @@ -587,6 +587,7 @@ func (m *mockHostInfoProvider) WaitForCalled(t *testing.T, n uint) {
start := time.Now()
for {
if m.called == n {
time.Sleep(100 * time.Microsecond)
return
}
if time.Since(start) > 10*time.Millisecond {
Expand Down

0 comments on commit 4abb731

Please sign in to comment.