1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

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

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`](https://golangci-lint.run/docs/linters/configuration/#usetesting)
- Adjust the code to support linter detection

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
Co-authored-by: Damien Mathieu <42@dmathieu.com>
This commit is contained in:
Flc゛
2025-09-23 15:52:45 +08:00
committed by GitHub
parent 2389f4488f
commit 80cb909774
106 changed files with 763 additions and 778 deletions
+6 -6
View File
@@ -171,7 +171,7 @@ func TestTracerStartPropagatesOrigCtx(t *testing.T) {
var key ctxKey
val := "value"
ctx := context.WithValue(context.Background(), key, val)
ctx := context.WithValue(t.Context(), key, val)
ctx, _ = newAutoTracerProvider().Tracer(tName).Start(ctx, "span.name")
assert.Equal(t, val, ctx.Value(key))
@@ -181,7 +181,7 @@ func TestTracerStartReturnsNonNilSpan(t *testing.T) {
t.Parallel()
tr := newAutoTracerProvider().Tracer(tName)
_, s := tr.Start(context.Background(), "span.name")
_, s := tr.Start(t.Context(), "span.name")
assert.NotNil(t, s)
}
@@ -189,7 +189,7 @@ func TestTracerStartAddsSpanToCtx(t *testing.T) {
t.Parallel()
tr := newAutoTracerProvider().Tracer(tName)
ctx, s := tr.Start(context.Background(), "span.name")
ctx, s := tr.Start(t.Context(), "span.name")
assert.Same(t, s, SpanFromContext(ctx))
}
@@ -199,7 +199,7 @@ func TestTracerConcurrentSafe(t *testing.T) {
const goroutines = 10
ctx := context.Background()
ctx := t.Context()
run := func(tracer Tracer) <-chan struct{} {
done := make(chan struct{})
@@ -377,7 +377,7 @@ func TestSpanCreation(t *testing.T) {
},
}
ctx := context.Background()
ctx := t.Context()
for _, tc := range testcases {
t.Run(tc.TestName, func(t *testing.T) {
if tc.Setup != nil {
@@ -1071,7 +1071,7 @@ func TestSpanConcurrentSafe(t *testing.T) {
go func(tracer Tracer) {
defer close(done)
ctx := context.Background()
ctx := t.Context()
var wg sync.WaitGroup
for i := range nSpans {