import (
"fmt"
- "internal/testenv"
"math/rand"
"runtime"
"strings"
}
}
-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 {
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()
},
// 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))
}
}
}
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...)
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")