summaryrefslogtreecommitdiff
path: root/misc/cgo/test/issue1560.go
diff options
context:
space:
mode:
Diffstat (limited to 'misc/cgo/test/issue1560.go')
-rw-r--r--misc/cgo/test/issue1560.go42
1 files changed, 36 insertions, 6 deletions
diff --git a/misc/cgo/test/issue1560.go b/misc/cgo/test/issue1560.go
index 3faa966e7..147ce94b5 100644
--- a/misc/cgo/test/issue1560.go
+++ b/misc/cgo/test/issue1560.go
@@ -15,6 +15,7 @@ void twoSleep(int);
import "C"
import (
+ "runtime"
"testing"
"time"
)
@@ -27,19 +28,48 @@ func parallelSleep(n int) {
}
//export BackgroundSleep
-func BackgroundSleep(n int) {
+func BackgroundSleep(n int32) {
go func() {
C.sleep(C.uint(n))
sleepDone <- true
}()
}
+// wasteCPU starts a background goroutine to waste CPU
+// to cause the power management to raise the CPU frequency.
+// On ARM this has the side effect of making sleep more accurate.
+func wasteCPU() chan struct{} {
+ done := make(chan struct{})
+ go func() {
+ for {
+ select {
+ case <-done:
+ return
+ default:
+ }
+ }
+ }()
+ // pause for a short amount of time to allow the
+ // power management to recognise load has risen.
+ <-time.After(300 * time.Millisecond)
+ return done
+}
+
func testParallelSleep(t *testing.T) {
+ if runtime.GOARCH == "arm" {
+ // on ARM, the 1.3s deadline is frequently missed,
+ // and burning cpu seems to help
+ defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(2))
+ defer close(wasteCPU())
+ }
+
+ sleepSec := 1
start := time.Now()
- parallelSleep(1)
- dt := time.Now().Sub(start)
- // bug used to run sleeps in serial, producing a 2-second delay.
- if dt >= 1300*time.Millisecond {
- t.Fatalf("parallel 1-second sleeps slept for %f seconds", dt.Seconds())
+ parallelSleep(sleepSec)
+ dt := time.Since(start)
+ t.Logf("sleep(%d) slept for %v", sleepSec, dt)
+ // bug used to run sleeps in serial, producing a 2*sleepSec-second delay.
+ if dt >= time.Duration(sleepSec)*1300*time.Millisecond {
+ t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds())
}
}