Skip to content

use t.Context() instead of context.Background() in tests#1132

Merged
wolo-lab merged 3 commits into
mainfrom
wolo/tests-use-tcontext
Jul 8, 2026
Merged

use t.Context() instead of context.Background() in tests#1132
wolo-lab merged 3 commits into
mainfrom
wolo/tests-use-tcontext

Conversation

@wolo-lab

@wolo-lab wolo-lab commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Summary

Migrates test code from context.Background()/context.TODO() to
t.Context() (Go 1.24+) wherever a *testing.T is in scope.
t.Context() returns a context that is canceled when the test finishes, so
test-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

  • Converted 91 call sites (90 context.Background() + 1 context.TODO())
    across 33 _test.go files and one test-support helper
    (internal/telemetry/telemetrytest/scenario.go).
  • Dropped the now-unused "context" imports.
  • Test-only; no production code changes.

Deliberately left as context.Background()

  • session/vertexai/service_test.go deleteAllFromApp: runs from t.Cleanup,
    where t.Context() is already canceled (per testing.T.Context docs), which
    would fail its List/Delete calls. Added a comment explaining why.
  • Helpers with no *testing.T in scope (e.g. newUserHello, makeEvent,
    emitTestSignals, newMockCtx) are unchanged.

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.
@wolo-lab wolo-lab force-pushed the wolo/tests-use-tcontext branch from daf8272 to 8da2914 Compare July 8, 2026 07:06
@wolo-lab wolo-lab marked this pull request as ready for review July 8, 2026 07:14
@wolo-lab wolo-lab requested a review from hanorik July 8, 2026 07:14
@wolo-lab wolo-lab merged commit 8cd675d into main Jul 8, 2026
9 checks passed
@wolo-lab wolo-lab deleted the wolo/tests-use-tcontext branch July 8, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants