Commit e3937d32 authored by Kirill Smelkov's avatar Kirill Smelkov

X tracing: Test for start/stop the world.

parent cf299b65
package xruntime
// TODO tests for stop/start the world.
import (
"runtime"
"sync/atomic"
"testing"
"time"
)
func TestStartStopTheWorld(t *testing.T) {
var x, stop int32
ready := make(chan int)
go func() {
// make sure the thread running this goroutine is different from thread for main g.
// this way we can be sure there are 2 OS threads in action and communicating via busywait should work.
runtime.LockOSThread()
ready <- 0
for atomic.LoadInt32(&stop) == 0 {
atomic.AddInt32(&x, 1)
// XXX as of go19 tight loops are not preemptible (golang.org/issues/10958)
// -> explicitly make sure we do not miss STW request.
runtime.Gosched()
}
}()
// wait for spawned goroutine to jump into its own thread
<-ready
// verify g and g2 are indeed running in parallel
xprev := atomic.LoadInt32(&x)
xnext := xprev
:= 0
tstart := time.Now()
for < 100 && time.Now().Sub(tstart) < time.Second {
xnext = atomic.LoadInt32(&x)
if xnext != xprev {
+= 1
xprev = xnext
}
}
if == 0 {
t.Fatal("g and g2 are not running in parallel")
}
// now stop the world and for 1s make sure g2 is not running in parallel with us
StopTheWorld("just for my reason")
xprev = atomic.LoadInt32(&x)
xnext = xprev
= 0
tstart = time.Now()
for time.Now().Sub(tstart) < time.Second {
for i := 0; i < 100 ; i++ {
xnext = atomic.LoadInt32(&x)
if xnext != xprev {
+= 1
xprev = xnext
}
}
}
StartTheWorld()
if != 0 {
t.Fatalf("g2 modified x at least %d times while the world was stopped", )
}
atomic.StoreInt32(&stop, 1)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment