aboutsummaryrefslogtreecommitdiff
path: root/src/context
diff options
context:
space:
mode:
authorBrad Fitzpatrick <bradfitz@golang.org>2016-04-11 18:16:55 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2016-04-11 23:15:02 +0000
commit501ddf7189cf97ef27eb870ad134a312f80ae585 (patch)
treeedb3fe684c5e7987200fa73fd4d4755b9e2cb449 /src/context
parentd1feddb7ae6389ee4175ee85b7168cf58a04b952 (diff)
downloadgo-501ddf7189cf97ef27eb870ad134a312f80ae585.tar.gz
go-501ddf7189cf97ef27eb870ad134a312f80ae585.zip
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: Andrew Gerrand <adg@golang.org>
Diffstat (limited to 'src/context')
-rw-r--r--src/context/context_test.go65
-rw-r--r--src/context/withtimeout_test.go4
2 files changed, 30 insertions, 39 deletions
diff --git a/src/context/context_test.go b/src/context/context_test.go
index 0616704dd8..aa26161d2b 100644
--- a/src/context/context_test.go
+++ b/src/context/context_test.go
@@ -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")
diff --git a/src/context/withtimeout_test.go b/src/context/withtimeout_test.go
index 3ab6fc347f..2aea303bed 100644
--- a/src/context/withtimeout_test.go
+++ b/src/context/withtimeout_test.go
@@ -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"