Skip to content

Commit 501ddf7

Browse files
committed
context: attempt to deflake timing tests
Passes on OpenBSD now when running it with -count=500. Presumably this will also fix the same problems seen on FreeBSD and Windows. Fixes #15158 Change-Id: I86451c901613dfa5ecff0c2ecc516527a3c011b3 Reviewed-on: https://go-review.googlesource.com/21840 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Andrew Gerrand <[email protected]>
1 parent d1feddb commit 501ddf7

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

src/context/context_test.go

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package context
66

77
import (
88
"fmt"
9-
"internal/testenv"
109
"math/rand"
1110
"runtime"
1211
"strings"
@@ -230,64 +229,55 @@ func TestChildFinishesFirst(t *testing.T) {
230229
}
231230
}
232231

233-
func testDeadline(c Context, wait time.Duration, t *testing.T) {
232+
func testDeadline(c Context, name string, failAfter time.Duration, t *testing.T) {
234233
select {
235-
case <-time.After(wait):
236-
t.Fatalf("context should have timed out")
234+
case <-time.After(failAfter):
235+
t.Fatalf("%s: context should have timed out", name)
237236
case <-c.Done():
238237
}
239238
if e := c.Err(); e != DeadlineExceeded {
240-
t.Errorf("c.Err() == %v want %v", e, DeadlineExceeded)
239+
t.Errorf("%s: c.Err() == %v; want %v", name, e, DeadlineExceeded)
241240
}
242241
}
243242

244243
func TestDeadline(t *testing.T) {
245-
if runtime.GOOS == "openbsd" {
246-
testenv.SkipFlaky(t, 15158)
247-
}
248-
c, _ := WithDeadline(Background(), time.Now().Add(100*time.Millisecond))
244+
c, _ := WithDeadline(Background(), time.Now().Add(50*time.Millisecond))
249245
if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
250246
t.Errorf("c.String() = %q want prefix %q", got, prefix)
251247
}
252-
testDeadline(c, 200*time.Millisecond, t)
248+
testDeadline(c, "WithDeadline", time.Second, t)
253249

254-
c, _ = WithDeadline(Background(), time.Now().Add(100*time.Millisecond))
250+
c, _ = WithDeadline(Background(), time.Now().Add(50*time.Millisecond))
255251
o := otherContext{c}
256-
testDeadline(o, 200*time.Millisecond, t)
252+
testDeadline(o, "WithDeadline+otherContext", time.Second, t)
257253

258-
c, _ = WithDeadline(Background(), time.Now().Add(100*time.Millisecond))
254+
c, _ = WithDeadline(Background(), time.Now().Add(50*time.Millisecond))
259255
o = otherContext{c}
260-
c, _ = WithDeadline(o, time.Now().Add(300*time.Millisecond))
261-
testDeadline(c, 200*time.Millisecond, t)
256+
c, _ = WithDeadline(o, time.Now().Add(4*time.Second))
257+
testDeadline(c, "WithDeadline+otherContext+WithDeadline", 2*time.Second, t)
262258
}
263259

264260
func TestTimeout(t *testing.T) {
265-
if runtime.GOOS == "openbsd" {
266-
testenv.SkipFlaky(t, 15158)
267-
}
268-
c, _ := WithTimeout(Background(), 100*time.Millisecond)
261+
c, _ := WithTimeout(Background(), 50*time.Millisecond)
269262
if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
270263
t.Errorf("c.String() = %q want prefix %q", got, prefix)
271264
}
272-
testDeadline(c, 200*time.Millisecond, t)
265+
testDeadline(c, "WithTimeout", time.Second, t)
273266

274-
c, _ = WithTimeout(Background(), 100*time.Millisecond)
267+
c, _ = WithTimeout(Background(), 50*time.Millisecond)
275268
o := otherContext{c}
276-
testDeadline(o, 200*time.Millisecond, t)
269+
testDeadline(o, "WithTimeout+otherContext", time.Second, t)
277270

278-
c, _ = WithTimeout(Background(), 100*time.Millisecond)
271+
c, _ = WithTimeout(Background(), 50*time.Millisecond)
279272
o = otherContext{c}
280-
c, _ = WithTimeout(o, 300*time.Millisecond)
281-
testDeadline(c, 200*time.Millisecond, t)
273+
c, _ = WithTimeout(o, 3*time.Second)
274+
testDeadline(c, "WithTimeout+otherContext+WithTimeout", 2*time.Second, t)
282275
}
283276

284277
func TestCanceledTimeout(t *testing.T) {
285-
if runtime.GOOS == "openbsd" {
286-
testenv.SkipFlaky(t, 15158)
287-
}
288-
c, _ := WithTimeout(Background(), 200*time.Millisecond)
278+
c, _ := WithTimeout(Background(), time.Second)
289279
o := otherContext{c}
290-
c, cancel := WithTimeout(o, 400*time.Millisecond)
280+
c, cancel := WithTimeout(o, 2*time.Second)
291281
cancel()
292282
time.Sleep(100 * time.Millisecond) // let cancelation propagate
293283
select {
@@ -398,9 +388,9 @@ func TestAllocs(t *testing.T) {
398388
gccgoLimit: 8,
399389
},
400390
{
401-
desc: "WithTimeout(bg, 100*time.Millisecond)",
391+
desc: "WithTimeout(bg, 5*time.Millisecond)",
402392
f: func() {
403-
c, cancel := WithTimeout(bg, 100*time.Millisecond)
393+
c, cancel := WithTimeout(bg, 5*time.Millisecond)
404394
cancel()
405395
<-c.Done()
406396
},
@@ -414,7 +404,11 @@ func TestAllocs(t *testing.T) {
414404
// TOOD(iant): Remove this when gccgo does do escape analysis.
415405
limit = test.gccgoLimit
416406
}
417-
if n := testing.AllocsPerRun(100, test.f); n > limit {
407+
numRuns := 100
408+
if testing.Short() {
409+
numRuns = 10
410+
}
411+
if n := testing.AllocsPerRun(numRuns, test.f); n > limit {
418412
t.Errorf("%s allocs = %f want %d", test.desc, n, int(limit))
419413
}
420414
}
@@ -494,9 +488,6 @@ func TestLayersTimeout(t *testing.T) {
494488
}
495489

496490
func testLayers(t *testing.T, seed int64, testTimeout bool) {
497-
if runtime.GOOS == "openbsd" {
498-
testenv.SkipFlaky(t, 15158)
499-
}
500491
rand.Seed(seed)
501492
errorf := func(format string, a ...interface{}) {
502493
t.Errorf(fmt.Sprintf("seed=%d: %s", seed, format), a...)
@@ -549,7 +540,7 @@ func testLayers(t *testing.T, seed int64, testTimeout bool) {
549540
if testTimeout {
550541
select {
551542
case <-ctx.Done():
552-
case <-time.After(timeout + 100*time.Millisecond):
543+
case <-time.After(timeout + time.Second):
553544
errorf("ctx should have timed out")
554545
}
555546
checkValues("after timeout")

src/context/withtimeout_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
func ExampleWithTimeout() {
1414
// Pass a context with a timeout to tell a blocking function that it
1515
// should abandon its work after the timeout elapses.
16-
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
16+
ctx, _ := context.WithTimeout(context.Background(), 50*time.Millisecond)
1717
select {
18-
case <-time.After(200 * time.Millisecond):
18+
case <-time.After(1 * time.Second):
1919
fmt.Println("overslept")
2020
case <-ctx.Done():
2121
fmt.Println(ctx.Err()) // prints "context deadline exceeded"

0 commit comments

Comments
 (0)