From 2cefd12a1bf7ee1d1aad03e17c4680d4b611d6da Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 6 Apr 2016 19:02:27 +0000 Subject: net, runtime: skip flaky tests on OpenBSD Flaky tests are a distraction and cover up real problems. File bugs instead and mark them as flaky. This moves the net/http flaky test flagging mechanism to internal/testenv. Updates #15156 Updates #15157 Updates #15158 Change-Id: I0e561cd2a09c0dec369cd4ed93bc5a2b40233dfe Reviewed-on: https://go-review.googlesource.com/21614 Reviewed-by: Matthew Dempsky Run-TryBot: Brad Fitzpatrick --- src/context/context_test.go | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/context') diff --git a/src/context/context_test.go b/src/context/context_test.go index 05345fc5e5..60020303c7 100644 --- a/src/context/context_test.go +++ b/src/context/context_test.go @@ -6,6 +6,7 @@ package context import ( "fmt" + "internal/testenv" "math/rand" "runtime" "strings" @@ -258,6 +259,9 @@ func TestDeadline(t *testing.T) { } func TestTimeout(t *testing.T) { + if runtime.GOOS == "openbsd" { + testenv.SkipFlaky(t, 15158) + } c, _ := WithTimeout(Background(), 100*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) -- cgit v1.3 From e2c09749af8c50fc2c0b515f2adc990cb0cb3cf6 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 7 Apr 2016 09:00:32 -0700 Subject: context: mark more tests as flaky on OpenBSD Updates #15158 Change-Id: I53e9e68d36efbf52736822e6caa047cfff501283 Reviewed-on: https://go-review.googlesource.com/21653 Reviewed-by: Matthew Dempsky Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/context/context_test.go | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/context') diff --git a/src/context/context_test.go b/src/context/context_test.go index 60020303c7..74af9a301c 100644 --- a/src/context/context_test.go +++ b/src/context/context_test.go @@ -242,6 +242,9 @@ func testDeadline(c Context, wait time.Duration, t *testing.T) { } func TestDeadline(t *testing.T) { + if runtime.GOOS == "openbsd" { + testenv.SkipFlaky(t, 15158) + } c, _ := WithDeadline(Background(), time.Now().Add(100*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) @@ -279,6 +282,9 @@ func TestTimeout(t *testing.T) { } func TestCanceledTimeout(t *testing.T) { + if runtime.GOOS == "openbsd" { + testenv.SkipFlaky(t, 15158) + } c, _ := WithTimeout(Background(), 200*time.Millisecond) o := otherContext{c} c, cancel := WithTimeout(o, 400*time.Millisecond) -- cgit v1.3 From 59d186832b94349d683431e01e084d6ce460f476 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 8 Apr 2016 17:50:03 +0000 Subject: context: disable more flaky tests on openbsd Updates #15158 Change-Id: Icb3788152a7a5a9b0d56ea38da46d770ffdce413 Reviewed-on: https://go-review.googlesource.com/21763 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Matthew Dempsky --- src/context/context_test.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/context') diff --git a/src/context/context_test.go b/src/context/context_test.go index 74af9a301c..573470e084 100644 --- a/src/context/context_test.go +++ b/src/context/context_test.go @@ -494,6 +494,9 @@ 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...) -- cgit v1.3 From bd7249766617fda12d112c3ad3ae2857ff97c71e Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 10 Apr 2016 15:45:34 +0000 Subject: context: document that WithValue's key must be comparable Also, check it and explode earlier, rather than cryptic failures later. Change-Id: I319a425f60e2bc9d005a187fbdbd153faa96411c Reviewed-on: https://go-review.googlesource.com/21799 Reviewed-by: Andrew Gerrand Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot Reviewed-by: Minux Ma --- src/context/context.go | 8 +++++++- src/context/context_test.go | 13 +++++++++++++ src/go/build/deps_test.go | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) (limited to 'src/context') diff --git a/src/context/context.go b/src/context/context.go index 21dc8676bf..c332e1f443 100644 --- a/src/context/context.go +++ b/src/context/context.go @@ -39,6 +39,7 @@ package context import ( "errors" "fmt" + "reflect" "sync" "time" ) @@ -424,7 +425,12 @@ func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc) { // // Use context Values only for request-scoped data that transits processes and // APIs, not for passing optional parameters to functions. -func WithValue(parent Context, key interface{}, val interface{}) Context { +// +// The provided key must be comparable. +func WithValue(parent Context, key, val interface{}) Context { + if !reflect.TypeOf(key).Comparable() { + panic("key is not comparable") + } return &valueCtx{parent, key, val} } diff --git a/src/context/context_test.go b/src/context/context_test.go index 573470e084..0616704dd8 100644 --- a/src/context/context_test.go +++ b/src/context/context_test.go @@ -586,3 +586,16 @@ func TestCancelRemoves(t *testing.T) { cancel() checkChildren("after cancelling WithTimeout child", ctx, 0) } + +func TestWithValueChecksKey(t *testing.T) { + panicVal := recoveredValue(func() { WithValue(Background(), []byte("foo"), "bar") }) + if panicVal == nil { + t.Error("expected panic") + } +} + +func recoveredValue(fn func()) (v interface{}) { + defer func() { v = recover() }() + fn() + return +} diff --git a/src/go/build/deps_test.go b/src/go/build/deps_test.go index 8e2fd6e584..f1d19bb50c 100644 --- a/src/go/build/deps_test.go +++ b/src/go/build/deps_test.go @@ -215,7 +215,7 @@ var pkgDeps = map[string][]string{ "compress/gzip": {"L4", "compress/flate"}, "compress/lzw": {"L4"}, "compress/zlib": {"L4", "compress/flate"}, - "context": {"errors", "fmt", "sync", "time"}, + "context": {"errors", "fmt", "reflect", "sync", "time"}, "database/sql": {"L4", "container/list", "database/sql/driver"}, "database/sql/driver": {"L4", "time"}, "debug/dwarf": {"L4"}, -- cgit v1.3 From 501ddf7189cf97ef27eb870ad134a312f80ae585 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 11 Apr 2016 18:16:55 +0000 Subject: 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 TryBot-Result: Gobot Gobot Reviewed-by: Andrew Gerrand --- src/context/context_test.go | 65 ++++++++++++++++++----------------------- src/context/withtimeout_test.go | 4 +-- 2 files changed, 30 insertions(+), 39 deletions(-) (limited to 'src/context') 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" -- cgit v1.3 From 87bca88c703c1f14fe8473dc2f07dc521cf2b989 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 26 Apr 2016 18:54:12 -0700 Subject: context: fix doc typo Fixes #15449 Change-Id: I8d84d076a05c56694b48f7b84f572b1a6524f522 Reviewed-on: https://go-review.googlesource.com/22493 Reviewed-by: Andrew Gerrand --- src/context/context.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/context') diff --git a/src/context/context.go b/src/context/context.go index c332e1f443..da294b1292 100644 --- a/src/context/context.go +++ b/src/context/context.go @@ -67,7 +67,7 @@ type Context interface { // // // Stream generates values with DoSomething and sends them to out // // until DoSomething returns an error or ctx.Done is closed. - // func Stream(ctx context.Context, out <-chan Value) error { + // func Stream(ctx context.Context, out chan<- Value) error { // for { // v, err := DoSomething(ctx) // if err != nil { -- cgit v1.3