mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-17 11:13:27 +00:00
test: make tests deterministic with -scheduler=threads
This commit is contained in:
committed by
Ron Evans
parent
6faf36fc64
commit
31f7214156
Vendored
+22
-5
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -70,11 +71,13 @@ func main() {
|
|||||||
// Test multi-receiver.
|
// Test multi-receiver.
|
||||||
ch = make(chan int)
|
ch = make(chan int)
|
||||||
wg.Add(3)
|
wg.Add(3)
|
||||||
go fastreceiver(ch)
|
var result atomic.Uint32
|
||||||
go fastreceiver(ch)
|
go fastreceiveradd(ch, &result)
|
||||||
go fastreceiver(ch)
|
go fastreceiveradd(ch, &result)
|
||||||
|
go fastreceiveradd(ch, &result)
|
||||||
slowsender(ch)
|
slowsender(ch)
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
println("sum of sums:", result.Load())
|
||||||
|
|
||||||
// Test iterator style channel.
|
// Test iterator style channel.
|
||||||
ch = make(chan int)
|
ch = make(chan int)
|
||||||
@@ -88,7 +91,10 @@ func main() {
|
|||||||
println("sum(100):", sum)
|
println("sum(100):", sum)
|
||||||
|
|
||||||
// Test simple selects.
|
// Test simple selects.
|
||||||
go selectDeadlock() // cannot use waitGroup here - never terminates
|
wg.Add(1)
|
||||||
|
go selectDeadlock()
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go selectNoOp()
|
go selectNoOp()
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
@@ -244,7 +250,7 @@ func receive(ch <-chan int) {
|
|||||||
func sender(ch chan int) {
|
func sender(ch chan int) {
|
||||||
for i := 1; i <= 8; i++ {
|
for i := 1; i <= 8; i++ {
|
||||||
if i == 4 {
|
if i == 4 {
|
||||||
time.Sleep(time.Microsecond)
|
time.Sleep(time.Millisecond)
|
||||||
println("slept")
|
println("slept")
|
||||||
}
|
}
|
||||||
ch <- i
|
ch <- i
|
||||||
@@ -290,6 +296,16 @@ func fastreceiver(ch chan int) {
|
|||||||
wg.Done()
|
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) {
|
func iterator(ch chan int, top int) {
|
||||||
for i := 0; i < top; i++ {
|
for i := 0; i < top; i++ {
|
||||||
ch <- i
|
ch <- i
|
||||||
@@ -300,6 +316,7 @@ func iterator(ch chan int, top int) {
|
|||||||
|
|
||||||
func selectDeadlock() {
|
func selectDeadlock() {
|
||||||
println("deadlocking")
|
println("deadlocking")
|
||||||
|
wg.Done()
|
||||||
select {}
|
select {}
|
||||||
println("unreachable")
|
println("unreachable")
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+1
-3
@@ -12,9 +12,7 @@ received num: 8
|
|||||||
recv from closed channel: 0 false
|
recv from closed channel: 0 false
|
||||||
complex128: (+7.000000e+000+1.050000e+001i)
|
complex128: (+7.000000e+000+1.050000e+001i)
|
||||||
sum of n: 149
|
sum of n: 149
|
||||||
sum: 25
|
sum of sums: 87
|
||||||
sum: 29
|
|
||||||
sum: 33
|
|
||||||
sum(100): 4950
|
sum(100): 4950
|
||||||
deadlocking
|
deadlocking
|
||||||
select no-op
|
select no-op
|
||||||
|
|||||||
Vendored
+1
-1
@@ -93,8 +93,8 @@ func acquire(m *sync.Mutex) {
|
|||||||
m.Lock()
|
m.Lock()
|
||||||
println("acquired mutex from goroutine")
|
println("acquired mutex from goroutine")
|
||||||
time.Sleep(2 * time.Millisecond)
|
time.Sleep(2 * time.Millisecond)
|
||||||
|
println("releasing mutex from goroutine")
|
||||||
m.Unlock()
|
m.Unlock()
|
||||||
println("released mutex from goroutine")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sub() {
|
func sub() {
|
||||||
|
|||||||
Vendored
+1
-1
@@ -19,7 +19,7 @@ closure go call result: 1
|
|||||||
pre-acquired mutex
|
pre-acquired mutex
|
||||||
releasing mutex
|
releasing mutex
|
||||||
acquired mutex from goroutine
|
acquired mutex from goroutine
|
||||||
released mutex from goroutine
|
releasing mutex from goroutine
|
||||||
re-acquired mutex
|
re-acquired mutex
|
||||||
done
|
done
|
||||||
called: Foo.Nowait
|
called: Foo.Nowait
|
||||||
|
|||||||
Vendored
+6
-2
@@ -2,9 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
println("# simple recover")
|
println("# simple recover")
|
||||||
recoverSimple()
|
recoverSimple()
|
||||||
@@ -113,14 +115,16 @@ func deferPanic() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runtimeGoexit() {
|
func runtimeGoexit() {
|
||||||
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
println("Goexit deferred function, recover is nil:", recover() == nil)
|
println("Goexit deferred function, recover is nil:", recover() == nil)
|
||||||
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
runtime.Goexit()
|
runtime.Goexit()
|
||||||
}()
|
}()
|
||||||
time.Sleep(time.Millisecond)
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
|
||||||
func printitf(msg string, itf interface{}) {
|
func printitf(msg string, itf interface{}) {
|
||||||
|
|||||||
Reference in New Issue
Block a user