test: replace context.Background() with t.Context()/b.Context() in tests#2613
Conversation
PR SummaryLow Risk Overview Reviewed by Cursor Bugbot for commit 633e474. Bugbot is set up for automated code reviews on this repo. Configure here. |
❌ 10 Tests Failed:
View the full list of 10 ❄️ flaky test(s)
To view more test analytics, go to the Test Analytics Dashboard |
There was a problem hiding this comment.
Code Review
Cleanup operations and finish callbacks in autoresume_test.go and state_change_test.go must use context.Background() instead of t.Context(). The test context is cancelled as soon as the test function returns, which will cause any subsequent context-aware I/O in these callbacks to fail, potentially leaving the system in an inconsistent state or causing flaky tests.
Tests now use the testing.T/B/F context binding introduced in Go 1.24
instead of context.Background(). The bound context is cancelled when the
test finishes, so any goroutines or in-flight operations spawned by a
test get stopped automatically when the test exits or times out — this
avoids goroutine leaks and surfaces lifetime bugs that context.Background()
silently masks.
Mechanical replacement across 31 test files:
- ctx := context.Background() -> ctx := t.Context()
- context.WithCancel(context.Background()) -> context.WithCancel(t.Context())
- context.WithTimeout(context.Background(), …) -> context.WithTimeout(t.Context(), …)
- Inside *testing.B benchmarks -> b.Context()
- Inside helpers taking testing.TB -> tb.Context()
- ratelimit doRequest helper now takes *testing.T so it can use t.Context()
- Removed now-unused "context" imports where no other context.* usage remained
Intentionally left as context.Background():
- t.Cleanup(...) callbacks: t.Context() is cancelled BEFORE cleanups run,
so passing it to Close()/Terminate() would abort the cleanup operation.
- Helpers without a *testing.T/B in scope (e.g. proxy_test.go newTestBackend,
uffd/userfaultfd/rpc_services_test.go subprocess harness).
- Unreachable defensive fallbacks of the form
`if parentCtx == nil { parentCtx = context.Background() }`
in placement_benchmark_test.go after b.Context() (which never returns nil).
Verified with `go vet` and `go test -run=NONE` (compile-only) across all
affected packages, including GOOS=linux for build-tagged orchestrator tests.
Tests now use the testing.T/B/F context binding introduced in Go 1.24 instead of context.Background(). The bound context is cancelled when the test finishes, so any goroutines or in-flight operations spawned by a test get stopped automatically when the test exits or times out — this avoids goroutine leaks and surfaces lifetime bugs that context.Background() silently masks.
Mechanical replacement across 31 test files:
Intentionally left as context.Background():
if parentCtx == nil { parentCtx = context.Background() }in placement_benchmark_test.go after b.Context() (which never returns nil).Verified with
go vetandgo test -run=NONE(compile-only) across all affected packages, including GOOS=linux for build-tagged orchestrator tests.