diff options
author | Ayke van Laethem <[email protected]> | 2024-11-04 10:16:35 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-12-06 11:55:34 +0100 |
commit | 31f72141561dafe3f0323599ca0307b34c3fc1da (patch) | |
tree | ebb50bdb3113581f995d075f02dd9eab1306efc0 | |
parent | 6faf36fc6420809d442aa7d0efdb5e3b524d0289 (diff) | |
download | tinygo-31f72141561dafe3f0323599ca0307b34c3fc1da.tar.gz tinygo-31f72141561dafe3f0323599ca0307b34c3fc1da.zip |
test: make tests deterministic with -scheduler=threads
-rw-r--r-- | testdata/channel.go | 27 | ||||
-rw-r--r-- | testdata/channel.txt | 4 | ||||
-rw-r--r-- | testdata/goroutines.go | 2 | ||||
-rw-r--r-- | testdata/goroutines.txt | 2 | ||||
-rw-r--r-- | testdata/recover.go | 8 |
5 files changed, 31 insertions, 12 deletions
diff --git a/testdata/channel.go b/testdata/channel.go index a7d0e99e4..9c0fee5b7 100644 --- a/testdata/channel.go +++ b/testdata/channel.go @@ -3,6 +3,7 @@ package main import ( "runtime" "sync" + "sync/atomic" "time" ) @@ -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) @@ -88,7 +91,10 @@ func main() { println("sum(100):", sum) // Test simple selects. - go selectDeadlock() // cannot use waitGroup here - never terminates + wg.Add(1) + go selectDeadlock() + wg.Wait() + wg.Add(1) go selectNoOp() wg.Wait() @@ -244,7 +250,7 @@ func receive(ch <-chan int) { func sender(ch chan int) { for i := 1; i <= 8; i++ { if i == 4 { - time.Sleep(time.Microsecond) + time.Sleep(time.Millisecond) println("slept") } ch <- i @@ -290,6 +296,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 @@ -300,6 +316,7 @@ func iterator(ch chan int, top int) { func selectDeadlock() { println("deadlocking") + wg.Done() select {} println("unreachable") } diff --git a/testdata/channel.txt b/testdata/channel.txt index bd3a4419d..44cda5ef7 100644 --- a/testdata/channel.txt +++ b/testdata/channel.txt @@ -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 diff --git a/testdata/goroutines.go b/testdata/goroutines.go index fe4347df9..cf19cc3ca 100644 --- a/testdata/goroutines.go +++ b/testdata/goroutines.go @@ -93,8 +93,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() { diff --git a/testdata/goroutines.txt b/testdata/goroutines.txt index 1430ee0a2..f1e4fc1e7 100644 --- a/testdata/goroutines.txt +++ b/testdata/goroutines.txt @@ -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 diff --git a/testdata/recover.go b/testdata/recover.go index 260bf91bd..6fdf282e7 100644 --- a/testdata/recover.go +++ b/testdata/recover.go @@ -2,9 +2,11 @@ package main import ( "runtime" - "time" + "sync" ) +var wg sync.WaitGroup + func main() { println("# simple recover") recoverSimple() @@ -113,14 +115,16 @@ func deferPanic() { } func runtimeGoexit() { + wg.Add(1) go func() { defer func() { println("Goexit deferred function, recover is nil:", recover() == nil) + wg.Done() }() runtime.Goexit() }() - time.Sleep(time.Millisecond) + wg.Wait() } func printitf(msg string, itf interface{}) { |