Commit 501ddf71 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

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 <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarAndrew Gerrand <adg@golang.org>
parent d1feddb7
......@@ -6,7 +6,6 @@ package context
import (
"fmt"
"internal/testenv"
"math/rand"
"runtime"
"strings"
......@@ -230,64 +229,55 @@ func TestChildFinishesFirst(t *testing.T) {
}
}
func testDeadline(c Context, wait time.Duration, t *testing.T) {
func testDeadline(c Context, name string, failAfter time.Duration, t *testing.T) {
select {
case <-time.After(wait):
t.Fatalf("context should have timed out")
case <-time.After(failAfter):
t.Fatalf("%s: context should have timed out", name)
case <-c.Done():
}
if e := c.Err(); e != DeadlineExceeded {
t.Errorf("c.Err() == %v want %v", e, DeadlineExceeded)
t.Errorf("%s: c.Err() == %v; want %v", name, e, DeadlineExceeded)
}
}
func TestDeadline(t *testing.T) {
if runtime.GOOS == "openbsd" {
testenv.SkipFlaky(t, 15158)
}
c, _ := WithDeadline(Background(), time.Now().Add(100*time.Millisecond))
c, _ := WithDeadline(Background(), time.Now().Add(50*time.Millisecond))
if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
t.Errorf("c.String() = %q want prefix %q", got, prefix)
}
testDeadline(c, 200*time.Millisecond, t)
testDeadline(c, "WithDeadline", time.Second, t)
c, _ = WithDeadline(Background(), time.Now().Add(100*time.Millisecond))
c, _ = WithDeadline(Background(), time.Now().Add(50*time.Millisecond))
o := otherContext{c}
testDeadline(o, 200*time.Millisecond, t)
testDeadline(o, "WithDeadline+otherContext", time.Second, t)
c, _ = WithDeadline(Background(), time.Now().Add(100*time.Millisecond))
c, _ = WithDeadline(Background(), time.Now().Add(50*time.Millisecond))
o = otherContext{c}
c, _ = WithDeadline(o, time.Now().Add(300*time.Millisecond))
testDeadline(c, 200*time.Millisecond, t)
c, _ = WithDeadline(o, time.Now().Add(4*time.Second))
testDeadline(c, "WithDeadline+otherContext+WithDeadline", 2*time.Second, t)
}
func TestTimeout(t *testing.T) {
if runtime.GOOS == "openbsd" {
testenv.SkipFlaky(t, 15158)
}
c, _ := WithTimeout(Background(), 100*time.Millisecond)
c, _ := WithTimeout(Background(), 50*time.Millisecond)
if got, prefix := fmt.Sprint(c), "context.Background.WithDeadline("; !strings.HasPrefix(got, prefix) {
t.Errorf("c.String() = %q want prefix %q", got, prefix)
}
testDeadline(c, 200*time.Millisecond, t)
testDeadline(c, "WithTimeout", time.Second, t)
c, _ = WithTimeout(Background(), 100*time.Millisecond)
c, _ = WithTimeout(Background(), 50*time.Millisecond)
o := otherContext{c}
testDeadline(o, 200*time.Millisecond, t)
testDeadline(o, "WithTimeout+otherContext", time.Second, t)
c, _ = WithTimeout(Background(), 100*time.Millisecond)
c, _ = WithTimeout(Background(), 50*time.Millisecond)
o = otherContext{c}
c, _ = WithTimeout(o, 300*time.Millisecond)
testDeadline(c, 200*time.Millisecond, t)
c, _ = WithTimeout(o, 3*time.Second)
testDeadline(c, "WithTimeout+otherContext+WithTimeout", 2*time.Second, t)
}
func TestCanceledTimeout(t *testing.T) {
if runtime.GOOS == "openbsd" {
testenv.SkipFlaky(t, 15158)
}
c, _ := WithTimeout(Background(), 200*time.Millisecond)
c, _ := WithTimeout(Background(), time.Second)
o := otherContext{c}
c, cancel := WithTimeout(o, 400*time.Millisecond)
c, cancel := WithTimeout(o, 2*time.Second)
cancel()
time.Sleep(100 * time.Millisecond) // let cancelation propagate
select {
......@@ -398,9 +388,9 @@ func TestAllocs(t *testing.T) {
gccgoLimit: 8,
},
{
desc: "WithTimeout(bg, 100*time.Millisecond)",
desc: "WithTimeout(bg, 5*time.Millisecond)",
f: func() {
c, cancel := WithTimeout(bg, 100*time.Millisecond)
c, cancel := WithTimeout(bg, 5*time.Millisecond)
cancel()
<-c.Done()
},
......@@ -414,7 +404,11 @@ func TestAllocs(t *testing.T) {
// TOOD(iant): Remove this when gccgo does do escape analysis.
limit = test.gccgoLimit
}
if n := testing.AllocsPerRun(100, test.f); n > limit {
numRuns := 100
if testing.Short() {
numRuns = 10
}
if n := testing.AllocsPerRun(numRuns, test.f); n > limit {
t.Errorf("%s allocs = %f want %d", test.desc, n, int(limit))
}
}
......@@ -494,9 +488,6 @@ func TestLayersTimeout(t *testing.T) {
}
func testLayers(t *testing.T, seed int64, testTimeout bool) {
if runtime.GOOS == "openbsd" {
testenv.SkipFlaky(t, 15158)
}
rand.Seed(seed)
errorf := func(format string, a ...interface{}) {
t.Errorf(fmt.Sprintf("seed=%d: %s", seed, format), a...)
......@@ -549,7 +540,7 @@ func testLayers(t *testing.T, seed int64, testTimeout bool) {
if testTimeout {
select {
case <-ctx.Done():
case <-time.After(timeout + 100*time.Millisecond):
case <-time.After(timeout + time.Second):
errorf("ctx should have timed out")
}
checkValues("after timeout")
......
......@@ -13,9 +13,9 @@ import (
func ExampleWithTimeout() {
// Pass a context with a timeout to tell a blocking function that it
// should abandon its work after the timeout elapses.
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
ctx, _ := context.WithTimeout(context.Background(), 50*time.Millisecond)
select {
case <-time.After(200 * time.Millisecond):
case <-time.After(1 * time.Second):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err()) // prints "context deadline exceeded"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment