use t.Context() instead of context.Background() in tests#1132
Merged
Conversation
t.Context() (Go 1.24+) returns a context that is canceled when the test finishes, tying test-scoped work to the test lifecycle: it prevents goroutine leaks and honors the test timeout. This matches Google Go style and the repo's already-dominant convention. Converts the 84 call sites (83 context.Background() + 1 context.TODO()) that have a *testing.T in scope, as flagged by the usetesting linter, and drops the now-unused context imports. Deliberately keeps session/vertexai deleteAllFromApp on context.Background(): it runs from t.Cleanup, where t.Context() is already canceled and would fail its List/Delete calls. Helpers without a *testing.T in scope are unchanged. Test-only; no production code changes.
daf8272 to
8da2914
Compare
hanorik
approved these changes
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Migrates test code from
context.Background()/context.TODO()tot.Context()(Go 1.24+) wherever a*testing.Tis in scope.t.Context()returns a context that is canceled when the test finishes, sotest-scoped work is tied to the test lifecycle: it prevents goroutine leaks and
honors the test timeout. This matches Google Go style and the repo's already
dominant convention (305 existing
t.Context()uses before this change).What changed
context.Background()+ 1context.TODO())across 33
_test.gofiles and one test-support helper(
internal/telemetry/telemetrytest/scenario.go)."context"imports.Deliberately left as
context.Background()session/vertexai/service_test.godeleteAllFromApp: runs fromt.Cleanup,where
t.Context()is already canceled (pertesting.T.Contextdocs), whichwould fail its
List/Deletecalls. Added a comment explaining why.*testing.Tin scope (e.g.newUserHello,makeEvent,emitTestSignals,newMockCtx) are unchanged.