Skip to content

Commit

Permalink
run ResetForTest during cleanup (#2754)
Browse files Browse the repository at this point in the history
Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
  • Loading branch information
dmathieu and hanyuancheung committed Mar 31, 2022
1 parent ceead4a commit 60041d2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 17 deletions.
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

0 comments on commit 60041d2

Please sign in to comment.