Skip to content

Commit a4ded4b

Browse files
ianlancetaylorgopherbot
authored andcommitted
misc/cgo/test: remove timing dependency from TestParallelSleep
Rename it TestIssue1560 since it no longer sleeps. For #1560 Fixes #45586 Change-Id: I338eee9de43e871da142143943e9435218438e90 Reviewed-on: https://go-review.googlesource.com/c/go/+/400194 Reviewed-by: Bryan Mills <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent f95db21 commit a4ded4b

File tree

3 files changed

+33
-53
lines changed

3 files changed

+33
-53
lines changed

misc/cgo/test/callback_c.c

+4-27
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
// license that can be found in the LICENSE file.
44

55
#include <string.h>
6-
#include <sys/types.h>
7-
#include <unistd.h>
6+
87
#include "_cgo_export.h"
98

109
void
@@ -31,32 +30,10 @@ IntoC(void)
3130
BackIntoGo();
3231
}
3332

34-
#ifdef WIN32
35-
#include <windows.h>
36-
long long
37-
mysleep(int seconds) {
38-
long long st = GetTickCount();
39-
Sleep(1000 * seconds);
40-
return st;
41-
}
42-
#else
43-
#include <sys/time.h>
44-
long long
45-
mysleep(int seconds) {
46-
long long st;
47-
struct timeval tv;
48-
gettimeofday(&tv, NULL);
49-
st = tv.tv_sec * 1000 + tv.tv_usec / 1000;
50-
sleep(seconds);
51-
return st;
52-
}
53-
#endif
54-
55-
long long
56-
twoSleep(int n)
33+
void
34+
Issue1560InC(void)
5735
{
58-
BackgroundSleep(n);
59-
return mysleep(n);
36+
Issue1560FromC();
6037
}
6138

6239
void

misc/cgo/test/cgo_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import "testing"
1111
// These wrappers are here for gotest to find.
1212

1313
func Test1328(t *testing.T) { test1328(t) }
14+
func Test1560(t *testing.T) { test1560(t) }
1415
func Test1635(t *testing.T) { test1635(t) }
1516
func Test3250(t *testing.T) { test3250(t) }
1617
func Test3729(t *testing.T) { test3729(t) }
@@ -89,7 +90,6 @@ func TestLibgcc(t *testing.T) { testLibgcc(t) }
8990
func TestMultipleAssign(t *testing.T) { testMultipleAssign(t) }
9091
func TestNaming(t *testing.T) { testNaming(t) }
9192
func TestPanicFromC(t *testing.T) { testPanicFromC(t) }
92-
func TestParallelSleep(t *testing.T) { testParallelSleep(t) }
9393
func TestPrintf(t *testing.T) { testPrintf(t) }
9494
func TestReturnAfterGrow(t *testing.T) { testReturnAfterGrow(t) }
9595
func TestReturnAfterGrowFromGo(t *testing.T) { testReturnAfterGrowFromGo(t) }

misc/cgo/test/testx.go

+28-25
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"sync"
1919
"sync/atomic"
2020
"testing"
21-
"time"
2221
"unsafe"
2322
)
2423

@@ -30,8 +29,7 @@ extern void doAdd(int, int);
3029
void IntoC(void);
3130
3231
// issue 1560
33-
// mysleep returns the absolute start time in ms.
34-
long long mysleep(int seconds);
32+
extern void Issue1560InC(void);
3533
3634
// twoSleep returns the absolute start time of the first sleep
3735
// in ms.
@@ -183,35 +181,40 @@ func test1328(t *testing.T) {
183181
}
184182

185183
// issue 1560
184+
// Test that C functions and Go functions run in parallel.
186185

187-
var sleepDone = make(chan int64)
186+
var (
187+
issue1560 int32
188188

189-
// parallelSleep returns the absolute difference between the start time
190-
// of the two sleeps.
191-
func parallelSleep(n int) int64 {
192-
t := int64(C.twoSleep(C.int(n))) - <-sleepDone
193-
if t < 0 {
194-
return -t
189+
issue1560Ch = make(chan bool, 2)
190+
)
191+
192+
//export Issue1560FromC
193+
func Issue1560FromC() {
194+
for atomic.LoadInt32(&issue1560) != 1 {
195+
runtime.Gosched()
196+
}
197+
atomic.AddInt32(&issue1560, 1)
198+
for atomic.LoadInt32(&issue1560) != 3 {
199+
runtime.Gosched()
195200
}
196-
return t
201+
issue1560Ch <- true
197202
}
198203

199-
//export BackgroundSleep
200-
func BackgroundSleep(n int32) {
201-
go func() {
202-
sleepDone <- int64(C.mysleep(C.int(n)))
203-
}()
204+
func Issue1560FromGo() {
205+
atomic.AddInt32(&issue1560, 1)
206+
for atomic.LoadInt32(&issue1560) != 2 {
207+
runtime.Gosched()
208+
}
209+
atomic.AddInt32(&issue1560, 1)
210+
issue1560Ch <- true
204211
}
205212

206-
func testParallelSleep(t *testing.T) {
207-
sleepSec := 1
208-
dt := time.Duration(parallelSleep(sleepSec)) * time.Millisecond
209-
t.Logf("difference in start time for two sleep(%d) is %v", sleepSec, dt)
210-
// bug used to run sleeps in serial, producing a 2*sleepSec-second delay.
211-
// we detect if the start times of those sleeps are > 0.5*sleepSec-second.
212-
if dt >= time.Duration(sleepSec)*time.Second/2 {
213-
t.Fatalf("parallel %d-second sleeps slept for %f seconds", sleepSec, dt.Seconds())
214-
}
213+
func test1560(t *testing.T) {
214+
go Issue1560FromGo()
215+
go C.Issue1560InC()
216+
<-issue1560Ch
217+
<-issue1560Ch
215218
}
216219

217220
// issue 2462

0 commit comments

Comments
 (0)