Skip to content

Commit 31f7214

Browse files
aykevldeadprogram
authored andcommitted
test: make tests deterministic with -scheduler=threads
1 parent 6faf36f commit 31f7214

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

testdata/channel.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"runtime"
55
"sync"
6+
"sync/atomic"
67
"time"
78
)
89

@@ -70,11 +71,13 @@ func main() {
7071
// Test multi-receiver.
7172
ch = make(chan int)
7273
wg.Add(3)
73-
go fastreceiver(ch)
74-
go fastreceiver(ch)
75-
go fastreceiver(ch)
74+
var result atomic.Uint32
75+
go fastreceiveradd(ch, &result)
76+
go fastreceiveradd(ch, &result)
77+
go fastreceiveradd(ch, &result)
7678
slowsender(ch)
7779
wg.Wait()
80+
println("sum of sums:", result.Load())
7881

7982
// Test iterator style channel.
8083
ch = make(chan int)
@@ -88,7 +91,10 @@ func main() {
8891
println("sum(100):", sum)
8992

9093
// Test simple selects.
91-
go selectDeadlock() // cannot use waitGroup here - never terminates
94+
wg.Add(1)
95+
go selectDeadlock()
96+
wg.Wait()
97+
9298
wg.Add(1)
9399
go selectNoOp()
94100
wg.Wait()
@@ -244,7 +250,7 @@ func receive(ch <-chan int) {
244250
func sender(ch chan int) {
245251
for i := 1; i <= 8; i++ {
246252
if i == 4 {
247-
time.Sleep(time.Microsecond)
253+
time.Sleep(time.Millisecond)
248254
println("slept")
249255
}
250256
ch <- i
@@ -290,6 +296,16 @@ func fastreceiver(ch chan int) {
290296
wg.Done()
291297
}
292298

299+
func fastreceiveradd(ch chan int, result *atomic.Uint32) {
300+
sum := 0
301+
for i := 0; i < 2; i++ {
302+
n := <-ch
303+
sum += n
304+
}
305+
result.Add(uint32(sum))
306+
wg.Done()
307+
}
308+
293309
func iterator(ch chan int, top int) {
294310
for i := 0; i < top; i++ {
295311
ch <- i
@@ -300,6 +316,7 @@ func iterator(ch chan int, top int) {
300316

301317
func selectDeadlock() {
302318
println("deadlocking")
319+
wg.Done()
303320
select {}
304321
println("unreachable")
305322
}

testdata/channel.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ received num: 8
1212
recv from closed channel: 0 false
1313
complex128: (+7.000000e+000+1.050000e+001i)
1414
sum of n: 149
15-
sum: 25
16-
sum: 29
17-
sum: 33
15+
sum of sums: 87
1816
sum(100): 4950
1917
deadlocking
2018
select no-op

testdata/goroutines.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ func acquire(m *sync.Mutex) {
9393
m.Lock()
9494
println("acquired mutex from goroutine")
9595
time.Sleep(2 * time.Millisecond)
96+
println("releasing mutex from goroutine")
9697
m.Unlock()
97-
println("released mutex from goroutine")
9898
}
9999

100100
func sub() {

testdata/goroutines.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ closure go call result: 1
1919
pre-acquired mutex
2020
releasing mutex
2121
acquired mutex from goroutine
22-
released mutex from goroutine
22+
releasing mutex from goroutine
2323
re-acquired mutex
2424
done
2525
called: Foo.Nowait

testdata/recover.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package main
22

33
import (
44
"runtime"
5-
"time"
5+
"sync"
66
)
77

8+
var wg sync.WaitGroup
9+
810
func main() {
911
println("# simple recover")
1012
recoverSimple()
@@ -113,14 +115,16 @@ func deferPanic() {
113115
}
114116

115117
func runtimeGoexit() {
118+
wg.Add(1)
116119
go func() {
117120
defer func() {
118121
println("Goexit deferred function, recover is nil:", recover() == nil)
122+
wg.Done()
119123
}()
120124

121125
runtime.Goexit()
122126
}()
123-
time.Sleep(time.Millisecond)
127+
wg.Wait()
124128
}
125129

126130
func printitf(msg string, itf interface{}) {

0 commit comments

Comments
 (0)