Skip to content

refactor: replace context.Background() with t.Context()/b.Context() in tests#7352

Merged
dmathieu merged 24 commits intoopen-telemetry:mainfrom
flc1125:replace-ctx
Sep 23, 2025
Merged

refactor: replace context.Background() with t.Context()/b.Context() in tests#7352
dmathieu merged 24 commits intoopen-telemetry:mainfrom
flc1125:replace-ctx

Conversation

@flc1125
Copy link
Member

@flc1125 flc1125 commented Sep 11, 2025

Based on the Go version we currently use, the dependency already supports 1.24+, which allows using t.Context() and b.Context() in unit tests and benchmarks respectively.

  • Enable context-background and context-todo in usetesting
  • Adjust the code to support linter detection

…ground

- Replace context.Background() with t.Context() in unit tests
- This change ensures that the test uses the context provided by the testing framework, which may contain useful information for the test
- Update multiple test files to use t.Context() instead of context.Background()
- This change improves testability by allowing the test framework to control the context
- Affected files:
  - bridge_grpc_test.go
  - bridge_test.go  - metric_test.go
  - mix_test.go
  - trace_test.go
  - tracer_test.go
@flc1125 flc1125 added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Sep 11, 2025
@codecov
Copy link

codecov bot commented Sep 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.5%. Comparing base (2389f44) to head (4df3368).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main   #7352     +/-   ##
=======================================
- Coverage   85.5%   85.5%   -0.1%     
=======================================
  Files        275     275             
  Lines      24614   24614             
=======================================
- Hits       21048   21045      -3     
- Misses      3187    3190      +3     
  Partials     379     379             

see 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@flc1125 flc1125 changed the title refactor: replace context.Background() with t.Context()/b.Context() in tests [WIP] refactor: replace context.Background() with t.Context()/b.Context() in tests Sep 11, 2025
Use context.Background() instead of b.Context() when asserting Shutdown in
benchmark cleanup to avoid unnecessary overhead and potential panic.

- Updated in trace/benchmark_test.go
…or Shutdown

- Use context.Background() instead of t.Context() when calling Shutdown- Add inline comment to explain the change and suppress linter warning
- Update multiple test functions to use the new approach
@flc1125 flc1125 marked this pull request as ready for review September 12, 2025 14:07
@flc1125 flc1125 changed the title [WIP] refactor: replace context.Background() with t.Context()/b.Context() in tests refactor: replace context.Background() with t.Context()/b.Context() in tests Sep 12, 2025
- Replace 'used to assert Shutdown' with 'required to avoid getting a canceled context at cleanup' in multiple test files
- Update several test files to use context.Background() in Shutdown calls with the new comment
- This change improves test cleanup and prevents potential issues with canceled contexts
…P client test

- Use context.Background() instead of t.Context() to prevent potential context cancellation at cleanup
- Add comment explaining the use of context.Background() and ignoring the usetesting linter
@flc1125 flc1125 marked this pull request as draft September 16, 2025 11:07
# Conflicts:
#	exporters/otlp/otlptrace/otlptracegrpc/client_test.go
Replace `context.Background()` with `t.Context()` in multiple test files to ensureproper context handling during test cleanup. This change prevents tests from using
a background context that may be canceled unexpectedly.Also remove unused import of `context` package in instrumentation_test.go and
add nolint comments where necessary to avoid linting errors related to context usage.
@flc1125 flc1125 marked this pull request as ready for review September 22, 2025 15:31
@dmathieu dmathieu merged commit 80cb909 into open-telemetry:main Sep 23, 2025
29 of 30 checks passed
@MrAlias MrAlias added this to the v1.39.0 milestone Oct 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Skip Changelog PRs that do not require a CHANGELOG.md entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants