Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

run ResetForTest during cleanup #2754

Merged
merged 2 commits into from Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/global/benchmark_test.go
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions internal/global/propagator_test.go
Expand Up @@ -23,7 +23,7 @@ import (
)

func TestTextMapPropagatorDelegation(t *testing.T) {
global.ResetForTest()
global.ResetForTest(t)
ctx := context.Background()
carrier := internaltest.NewTextMapCarrier(nil)

Expand Down Expand Up @@ -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)

Expand All @@ -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()
Expand Down
16 changes: 10 additions & 6 deletions internal/global/state.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
})
}
2 changes: 1 addition & 1 deletion internal/global/state_test.go
Expand Up @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions internal/global/trace_test.go
Expand Up @@ -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{
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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},
Expand Down