Skip to content

Commit

Permalink
test: make tests deterministic with -scheduler=threads
Browse files Browse the repository at this point in the history
  • Loading branch information
aykevl committed Nov 4, 2024
1 parent 036e8a5 commit 6ae73cb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
19 changes: 16 additions & 3 deletions testdata/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"runtime"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -70,11 +71,13 @@ func main() {
// Test multi-receiver.
ch = make(chan int)
wg.Add(3)
go fastreceiver(ch)
go fastreceiver(ch)
go fastreceiver(ch)
var result atomic.Uint32
go fastreceiveradd(ch, &result)
go fastreceiveradd(ch, &result)
go fastreceiveradd(ch, &result)
slowsender(ch)
wg.Wait()
println("sum of sums:", result.Load())

// Test iterator style channel.
ch = make(chan int)
Expand Down Expand Up @@ -290,6 +293,16 @@ func fastreceiver(ch chan int) {
wg.Done()
}

func fastreceiveradd(ch chan int, result *atomic.Uint32) {
sum := 0
for i := 0; i < 2; i++ {
n := <-ch
sum += n
}
result.Add(uint32(sum))
wg.Done()
}

func iterator(ch chan int, top int) {
for i := 0; i < top; i++ {
ch <- i
Expand Down
4 changes: 1 addition & 3 deletions testdata/channel.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ received num: 8
recv from closed channel: 0 false
complex128: (+7.000000e+000+1.050000e+001i)
sum of n: 149
sum: 25
sum: 29
sum: 33
sum of sums: 87
sum(100): 4950
deadlocking
select no-op
Expand Down
2 changes: 1 addition & 1 deletion testdata/goroutines.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ func acquire(m *sync.Mutex) {
m.Lock()
println("acquired mutex from goroutine")
time.Sleep(2 * time.Millisecond)
println("releasing mutex from goroutine")
m.Unlock()
println("released mutex from goroutine")
}

func sub() {
Expand Down
2 changes: 1 addition & 1 deletion testdata/goroutines.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ closure go call result: 1
pre-acquired mutex
releasing mutex
acquired mutex from goroutine
released mutex from goroutine
releasing mutex from goroutine
re-acquired mutex
done
called: Foo.Nowait
Expand Down

0 comments on commit 6ae73cb

Please sign in to comment.