Skip to content

Commit 25b8392

Browse files
aykevldeadprogram
authored andcommitted
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 3f8a110 commit 25b8392

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

testdata/goroutines.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,15 @@ func main() {
6262
time.Sleep(2 * time.Millisecond)
6363

6464
var m sync.Mutex
65+
var wg sync.WaitGroup
6566
m.Lock()
6667
println("pre-acquired mutex")
67-
go acquire(&m)
68+
wg.Add(1)
69+
go acquire(&m, &wg)
6870
time.Sleep(2 * time.Millisecond)
6971
println("releasing mutex")
7072
m.Unlock()
73+
wg.Wait()
7174
time.Sleep(2 * time.Millisecond)
7275
m.Lock()
7376
println("re-acquired mutex")
@@ -89,8 +92,9 @@ func main() {
8992
<-done
9093
}
9194

92-
func acquire(m *sync.Mutex) {
95+
func acquire(m *sync.Mutex, wg *sync.WaitGroup) {
9396
m.Lock()
97+
wg.Done()
9498
println("acquired mutex from goroutine")
9599
time.Sleep(2 * time.Millisecond)
96100
println("releasing mutex from goroutine")

0 commit comments

Comments
 (0)