Skip to content

Commit e86286b

Browse files
ianlancetayloradg
authored andcommitted
runtime: more deflaking of TestCgoCheckBytes
Fixes #14519. Change-Id: I8f78f67a463e6467e09df90446f7ebd28789d6c9 Reviewed-on: https://go-review.googlesource.com/19933 Reviewed-by: Brad Fitzpatrick <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-on: https://go-review.googlesource.com/22071 Run-TryBot: Andrew Gerrand <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 16b00c9 commit e86286b

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

src/runtime/crash_cgo_test.go

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package runtime_test
88

99
import (
10+
"fmt"
1011
"internal/testenv"
1112
"os/exec"
1213
"runtime"
@@ -161,22 +162,35 @@ func TestCgoCheckBytes(t *testing.T) {
161162
t.Fatal(err)
162163
}
163164

164-
cmd := testEnv(exec.Command(exe, "CgoCheckBytes"))
165-
cmd.Env = append(cmd.Env, "GODEBUG=cgocheck=0")
165+
// Try it 10 times to avoid flakiness.
166+
const tries = 10
167+
var tot1, tot2 time.Duration
168+
for i := 0; i < tries; i++ {
169+
cmd := testEnv(exec.Command(exe, "CgoCheckBytes"))
170+
cmd.Env = append(cmd.Env, "GODEBUG=cgocheck=0", fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
166171

167-
start := time.Now()
168-
cmd.Run()
169-
d1 := time.Since(start)
172+
start := time.Now()
173+
cmd.Run()
174+
d1 := time.Since(start)
170175

171-
cmd = testEnv(exec.Command(exe, "CgoCheckBytes"))
176+
cmd = testEnv(exec.Command(exe, "CgoCheckBytes"))
177+
cmd.Env = append(cmd.Env, fmt.Sprintf("GO_CGOCHECKBYTES_TRY=%d", i))
172178

173-
start = time.Now()
174-
cmd.Run()
175-
d2 := time.Since(start)
179+
start = time.Now()
180+
cmd.Run()
181+
d2 := time.Since(start)
176182

177-
if d1*20 < d2 {
178-
t.Errorf("cgo check too slow: got %v, expected at most %v", d1, d2*10)
183+
if d1*20 > d2 {
184+
// The slow version (d2) was less than 20 times
185+
// slower than the fast version (d1), so OK.
186+
return
187+
}
188+
189+
tot1 += d1
190+
tot2 += d2
179191
}
192+
193+
t.Errorf("cgo check too slow: got %v, expected at most %v", tot2/tries, (tot1/tries)*20)
180194
}
181195

182196
func TestCgoCCodeSIGPROF(t *testing.T) {

src/runtime/testdata/testprogcgo/cgo.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ void foo2(void* p) {}
1111
import "C"
1212
import (
1313
"fmt"
14+
"os"
1415
"runtime"
16+
"strconv"
1517
"time"
1618
"unsafe"
1719
)
@@ -83,8 +85,16 @@ func CgoTraceback() {
8385
}
8486

8587
func CgoCheckBytes() {
86-
b := make([]byte, 1e6)
87-
for i := 0; i < 1e3; i++ {
88+
try, _ := strconv.Atoi(os.Getenv("GO_CGOCHECKBYTES_TRY"))
89+
if try <= 0 {
90+
try = 1
91+
}
92+
b := make([]byte, 1e6*try)
93+
start := time.Now()
94+
for i := 0; i < 1e3*try; i++ {
8895
C.foo2(unsafe.Pointer(&b[0]))
96+
if time.Since(start) > time.Second {
97+
break
98+
}
8999
}
90100
}

0 commit comments

Comments
 (0)