Skip to content

Commit 1863c3c

Browse files
committed
ci: try to fix race condition in testdata/goroutines.go
A race condition was possible because the 'acquire' goroutine might not have started in 4 milliseconds which changed the ordering of the test. This patch fixes it by making sure the goroutine has started (and locked the mutex) before continuing with the test.
1 parent 120d17c commit 1863c3c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

testdata/goroutines.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ func main() {
6161

6262
time.Sleep(2 * time.Millisecond)
6363

64-
var m sync.Mutex
64+
var m, wg sync.Mutex
6565
m.Lock()
6666
println("pre-acquired mutex")
67-
go acquire(&m)
67+
wg.Add(1)
68+
go acquire(&m, &wg)
69+
wg.Wait()
6870
time.Sleep(2 * time.Millisecond)
6971
println("releasing mutex")
7072
m.Unlock()
@@ -89,8 +91,9 @@ func main() {
8991
<-done
9092
}
9193

92-
func acquire(m *sync.Mutex) {
94+
func acquire(m *sync.Mutex, wg *sync.WaitGroup) {
9395
m.Lock()
96+
wg.Done()
9497
println("acquired mutex from goroutine")
9598
time.Sleep(2 * time.Millisecond)
9699
println("releasing mutex from goroutine")

0 commit comments

Comments
 (0)