Skip to content

Commit 3f1b96d

Browse files
committed
test: fix the data race in TestEtcdElectionsLost
Wait for the goroutine to complete before the test exits, so its `StopElections` does not produce a log after the test has completed, which causes a data race. Signed-off-by: Utku Ozdemir <[email protected]>
1 parent 17d0394 commit 3f1b96d

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

internal/backend/runtime/omni/state_etcd_election_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package omni_test
77

88
import (
99
"context"
10+
"sync"
1011
"testing"
1112
"time"
1213

@@ -54,14 +55,18 @@ func TestEtcdElectionsLost(t *testing.T) {
5455
errCh := make(chan error, 1)
5556
electionKey := uuid.NewString()
5657

58+
var wg sync.WaitGroup
59+
60+
defer wg.Wait()
61+
5762
// run mock runner, it should win the elections and keep running
58-
go func() {
63+
wg.Go(func() {
5964
errCh <- state.RunElections(ctx, electionKey, logger)
6065

6166
defer state.StopElections(electionKey) //nolint:errcheck
6267

6368
mockRunner(ctx, 1, started, nil)
64-
}()
69+
})
6570

6671
select {
6772
case id := <-started:

0 commit comments

Comments
 (0)