1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-29 23:07:45 +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

View File

@@ -22,6 +22,7 @@ import (
func basicTracerProvider(t *testing.T) *TracerProvider {
tp := NewTracerProvider(WithSampler(AlwaysSample()))
t.Cleanup(func() {
//nolint:usetesting // required to avoid getting a canceled context at cleanup.
assert.NoError(t, tp.Shutdown(context.Background()))
})
return tp
@@ -112,7 +113,7 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
ctxKey := testCtxKey{}
ctxValue := "ctx value"
ctx := context.WithValue(context.Background(), ctxKey, ctxValue)
ctx := context.WithValue(t.Context(), ctxKey, ctxValue)
ctx, _ = subject.Start(ctx, "test")
@@ -124,7 +125,7 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()
_, span := subject.Start(context.Background(), "test")
_, span := subject.Start(t.Context(), "test")
require.NotNil(t, span)
require.True(t, span.SpanContext().IsValid())
@@ -135,7 +136,7 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()
ctx, span := subject.Start(context.Background(), "test")
ctx, span := subject.Start(t.Context(), "test")
require.NotNil(t, span)
require.NotEqual(t, trace.SpanContext{}, span.SpanContext())
@@ -147,8 +148,8 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()
_, span1 := subject.Start(context.Background(), "span1")
_, span2 := subject.Start(context.Background(), "span2")
_, span1 := subject.Start(t.Context(), "span1")
_, span2 := subject.Start(t.Context(), "span2")
sc1 := span1.SpanContext()
sc2 := span2.SpanContext()
@@ -162,7 +163,7 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()
ctx, parent := subject.Start(context.Background(), "parent")
ctx, parent := subject.Start(t.Context(), "parent")
_, child := subject.Start(ctx, "child")
psc := parent.SpanContext()
@@ -177,7 +178,7 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()
ctx, parent := subject.Start(context.Background(), "parent")
ctx, parent := subject.Start(t.Context(), "parent")
_, child := subject.Start(ctx, "child", trace.WithNewRoot())
psc := parent.SpanContext()
@@ -192,8 +193,8 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()
_, remoteParent := subject.Start(context.Background(), "remote parent")
parentCtx := trace.ContextWithRemoteSpanContext(context.Background(), remoteParent.SpanContext())
_, remoteParent := subject.Start(t.Context(), "remote parent")
parentCtx := trace.ContextWithRemoteSpanContext(t.Context(), remoteParent.SpanContext())
_, child := subject.Start(parentCtx, "child")
psc := remoteParent.SpanContext()
@@ -208,8 +209,8 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
subject := subjectFactory()
_, remoteParent := subject.Start(context.Background(), "remote parent")
parentCtx := trace.ContextWithRemoteSpanContext(context.Background(), remoteParent.SpanContext())
_, remoteParent := subject.Start(t.Context(), "remote parent")
parentCtx := trace.ContextWithRemoteSpanContext(t.Context(), remoteParent.SpanContext())
_, child := subject.Start(parentCtx, "child", trace.WithNewRoot())
psc := remoteParent.SpanContext()
@@ -224,7 +225,7 @@ func (h *harness) testTracer(subjectFactory func() trace.Tracer) {
tracer := subjectFactory()
ctx, parent := tracer.Start(context.Background(), "span")
ctx, parent := tracer.Start(t.Context(), "span")
runner := func(tp trace.Tracer) <-chan struct{} {
done := make(chan struct{})
@@ -284,12 +285,12 @@ func (h *harness) testSpan(tracerFactory func() trace.Tracer) {
mechanisms := map[string]func() trace.Span{
"Span created via Tracer#Start": func() trace.Span {
tracer := tracerFactory()
_, subject := tracer.Start(context.Background(), "test")
_, subject := tracer.Start(h.t.Context(), "test")
return subject
},
"Span created via span.TracerProvider()": func() trace.Span {
ctx, spanA := tracerFactory().Start(context.Background(), "span1")
ctx, spanA := tracerFactory().Start(h.t.Context(), "span1")
_, spanB := spanA.TracerProvider().Tracer("second").Start(ctx, "span2")
return spanB