Skip to content

Commit a3b12ea

Browse files
Bryan C. Millsgopherbot
authored andcommitted
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 <[email protected]> Run-TryBot: Bryan Mills <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> kokoro-CI: kokoro <[email protected]>
1 parent c3163ea commit a3b12ea

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

internal/xcontext/xcontext_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,22 @@ type ctxKey string
1515
var key = ctxKey("key")
1616

1717
func TestDetach(t *testing.T) {
18-
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
18+
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
1919
defer cancel()
20+
2021
ctx = context.WithValue(ctx, key, "value")
2122
dctx := Detach(ctx)
2223
// Detached context has the same values.
2324
got, ok := dctx.Value(key).(string)
2425
if !ok || got != "value" {
2526
t.Errorf("Value: got (%v, %t), want 'value', true", got, ok)
2627
}
27-
// Detached context doesn't time out.
28-
time.Sleep(500 * time.Millisecond)
28+
29+
// Wait for the parent context to time out, and then give it an arbitrary
30+
// amount of extra time for the cancellation to propagate if it's going to.
31+
<-ctx.Done()
32+
time.Sleep(5 * time.Millisecond)
33+
2934
if err := ctx.Err(); err != context.DeadlineExceeded {
3035
t.Fatalf("original context Err: got %v, want DeadlineExceeded", err)
3136
}

0 commit comments

Comments
 (0)