LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package cgotest | 5 package cgotest |
6 | 6 |
7 /* | 7 /* |
8 #include <unistd.h> | 8 #include <unistd.h> |
9 | 9 |
10 unsigned int sleep(unsigned int seconds); | 10 unsigned int sleep(unsigned int seconds); |
11 | 11 |
12 extern void BackgroundSleep(int); | 12 extern void BackgroundSleep(int); |
13 void twoSleep(int); | 13 void twoSleep(int); |
14 */ | 14 */ |
15 import "C" | 15 import "C" |
16 | 16 |
17 import ( | 17 import ( |
| 18 "runtime" |
18 "testing" | 19 "testing" |
19 "time" | 20 "time" |
20 ) | 21 ) |
21 | 22 |
22 var sleepDone = make(chan bool) | 23 var sleepDone = make(chan bool) |
23 | 24 |
24 func parallelSleep(n int) { | 25 func parallelSleep(n int) { |
25 C.twoSleep(C.int(n)) | 26 C.twoSleep(C.int(n)) |
26 <-sleepDone | 27 <-sleepDone |
27 } | 28 } |
28 | 29 |
29 //export BackgroundSleep | 30 //export BackgroundSleep |
30 func BackgroundSleep(n int) { | 31 func BackgroundSleep(n int) { |
31 go func() { | 32 go func() { |
32 C.sleep(C.uint(n)) | 33 C.sleep(C.uint(n)) |
33 sleepDone <- true | 34 sleepDone <- true |
34 }() | 35 }() |
35 } | 36 } |
36 | 37 |
37 func testParallelSleep(t *testing.T) { | 38 func testParallelSleep(t *testing.T) { |
| 39 sleepSec := 1 |
| 40 if runtime.GOARCH == "arm" { |
| 41 // on ARM, the 1.3s deadline is frequently missed, |
| 42 // so increase sleep time to 2s |
| 43 sleepSec = 2 |
| 44 } |
38 start := time.Now() | 45 start := time.Now() |
39 » parallelSleep(1) | 46 » parallelSleep(sleepSec) |
40 dt := time.Now().Sub(start) | 47 dt := time.Now().Sub(start) |
41 » // bug used to run sleeps in serial, producing a 2-second delay. | 48 » // bug used to run sleeps in serial, producing a 2*sleepSec-second delay
. |
42 » if dt >= 1300*time.Millisecond { | 49 » if dt >= time.Duration(sleepSec)*1300*time.Millisecond { |
43 » » t.Fatalf("parallel 1-second sleeps slept for %f seconds", dt.Sec
onds()) | 50 » » t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleep
Sec, dt.Seconds()) |
44 } | 51 } |
45 } | 52 } |
LEFT | RIGHT |