internal/xcontext: avoid assumptions about test timing in TestDetach
The test previously assumed that a 500ms sleep is sufficient for a
200ms timeout to trigger. On a heavily loaded CI machine, that
assumption may not always hold — but it doesn't seem necessary for the
properties under test anyway.
Fixes #61272.
Change-Id: Iaf88a39ec06d87731769acdee66847a5e9404dc0
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/517296
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
kokoro-CI: kokoro <noreply+kokoro@google.com>
diff --git a/internal/xcontext/xcontext_test.go b/internal/xcontext/xcontext_test.go
index ceb44bb..6d9b507 100644
--- a/internal/xcontext/xcontext_test.go
+++ b/internal/xcontext/xcontext_test.go
@@ -15,8 +15,9 @@
var key = ctxKey("key")
func TestDetach(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
+ ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()
+
ctx = context.WithValue(ctx, key, "value")
dctx := Detach(ctx)
// Detached context has the same values.
@@ -24,8 +25,12 @@
if !ok || got != "value" {
t.Errorf("Value: got (%v, %t), want 'value', true", got, ok)
}
- // Detached context doesn't time out.
- time.Sleep(500 * time.Millisecond)
+
+ // Wait for the parent context to time out, and then give it an arbitrary
+ // amount of extra time for the cancellation to propagate if it's going to.
+ <-ctx.Done()
+ time.Sleep(5 * time.Millisecond)
+
if err := ctx.Err(); err != context.DeadlineExceeded {
t.Fatalf("original context Err: got %v, want DeadlineExceeded", err)
}