diff --git a/internal/global/benchmark_test.go b/internal/global/benchmark_test.go index ef37194f4..f00184464 100644 --- a/internal/global/benchmark_test.go +++ b/internal/global/benchmark_test.go @@ -25,7 +25,7 @@ import ( func BenchmarkStartEndSpanNoSDK(b *testing.B) { // Compare with BenchmarkStartEndSpan() in // ../../sdk/trace/benchmark_test.go. - global.ResetForTest() + global.ResetForTest(b) t := otel.Tracer("Benchmark StartEndSpan") ctx := context.Background() b.ResetTimer() diff --git a/internal/global/propagator_test.go b/internal/global/propagator_test.go index afaff13e4..b58ae4456 100644 --- a/internal/global/propagator_test.go +++ b/internal/global/propagator_test.go @@ -23,7 +23,7 @@ import ( ) func TestTextMapPropagatorDelegation(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) ctx := context.Background() carrier := internaltest.NewTextMapCarrier(nil) @@ -53,7 +53,7 @@ func TestTextMapPropagatorDelegation(t *testing.T) { } func TestTextMapPropagatorDelegationNil(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) ctx := context.Background() carrier := internaltest.NewTextMapCarrier(nil) @@ -75,7 +75,7 @@ func TestTextMapPropagatorDelegationNil(t *testing.T) { } func TestTextMapPropagatorFields(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) initial := global.TextMapPropagator() delegate := internaltest.NewTextMapPropagator("test") delegateFields := delegate.Fields() diff --git a/internal/global/state.go b/internal/global/state.go index d6b3e900c..a0e669183 100644 --- a/internal/global/state.go +++ b/internal/global/state.go @@ -17,6 +17,7 @@ package global // import "go.opentelemetry.io/otel/internal/global" import ( "sync" "sync/atomic" + "testing" "go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/trace" @@ -97,10 +98,13 @@ func defaultPropagatorsValue() *atomic.Value { return v } -// ResetForTest restores the initial global state, for testing purposes. -func ResetForTest() { - globalTracer = defaultTracerValue() - globalPropagators = defaultPropagatorsValue() - delegateTraceOnce = sync.Once{} - delegateTextMapPropagatorOnce = sync.Once{} +// ResetForTest configures the test to restores the initial global state during +// its Cleanup step +func ResetForTest(t testing.TB) { + t.Cleanup(func() { + globalTracer = defaultTracerValue() + globalPropagators = defaultPropagatorsValue() + delegateTraceOnce = sync.Once{} + delegateTextMapPropagatorOnce = sync.Once{} + }) } diff --git a/internal/global/state_test.go b/internal/global/state_test.go index 9e812083a..3f497cce6 100644 --- a/internal/global/state_test.go +++ b/internal/global/state_test.go @@ -21,7 +21,7 @@ import ( ) func TestResetsOfGlobalsPanic(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) tests := map[string]func(){ "SetTextMapPropagator": func() { global.SetTextMapPropagator(global.TextMapPropagator()) diff --git a/internal/global/trace_test.go b/internal/global/trace_test.go index 827dad3c8..fc16ff0e7 100644 --- a/internal/global/trace_test.go +++ b/internal/global/trace_test.go @@ -44,7 +44,7 @@ func (fn fnTracer) Start(ctx context.Context, spanName string, opts ...trace.Spa } func TestTraceProviderDelegation(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) // Map of tracers to expected span names. expected := map[string][]string{ @@ -98,7 +98,7 @@ func TestTraceProviderDelegation(t *testing.T) { } func TestTraceProviderDelegates(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) // Retrieve the placeholder TracerProvider. gtp := otel.GetTracerProvider() @@ -118,7 +118,7 @@ func TestTraceProviderDelegates(t *testing.T) { } func TestTraceProviderDelegatesConcurrentSafe(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) // Retrieve the placeholder TracerProvider. gtp := otel.GetTracerProvider() @@ -161,7 +161,7 @@ func TestTraceProviderDelegatesConcurrentSafe(t *testing.T) { } func TestTracerDelegatesConcurrentSafe(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) // Retrieve the placeholder TracerProvider. gtp := otel.GetTracerProvider() @@ -210,7 +210,7 @@ func TestTracerDelegatesConcurrentSafe(t *testing.T) { } func TestTraceProviderDelegatesSameInstance(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) // Retrieve the placeholder TracerProvider. gtp := otel.GetTracerProvider() @@ -228,7 +228,7 @@ func TestTraceProviderDelegatesSameInstance(t *testing.T) { } func TestSpanContextPropagatedWithNonRecordingSpan(t *testing.T) { - global.ResetForTest() + global.ResetForTest(t) sc := trace.NewSpanContext(trace.SpanContextConfig{ TraceID: [16]byte{0x01},