1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-10 09:50:58 +02:00

run ResetForTest during cleanup (#2754)

Co-authored-by: Chester Cheung <cheung.zhy.csu@gmail.com>
This commit is contained in:
Damien Mathieu 2022-03-31 19:21:14 +02:00 committed by GitHub
parent ceead4a2cd
commit 60041d2992
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 17 deletions

View File

@ -25,7 +25,7 @@ import (
func BenchmarkStartEndSpanNoSDK(b *testing.B) { func BenchmarkStartEndSpanNoSDK(b *testing.B) {
// Compare with BenchmarkStartEndSpan() in // Compare with BenchmarkStartEndSpan() in
// ../../sdk/trace/benchmark_test.go. // ../../sdk/trace/benchmark_test.go.
global.ResetForTest() global.ResetForTest(b)
t := otel.Tracer("Benchmark StartEndSpan") t := otel.Tracer("Benchmark StartEndSpan")
ctx := context.Background() ctx := context.Background()
b.ResetTimer() b.ResetTimer()

View File

@ -23,7 +23,7 @@ import (
) )
func TestTextMapPropagatorDelegation(t *testing.T) { func TestTextMapPropagatorDelegation(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
ctx := context.Background() ctx := context.Background()
carrier := internaltest.NewTextMapCarrier(nil) carrier := internaltest.NewTextMapCarrier(nil)
@ -53,7 +53,7 @@ func TestTextMapPropagatorDelegation(t *testing.T) {
} }
func TestTextMapPropagatorDelegationNil(t *testing.T) { func TestTextMapPropagatorDelegationNil(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
ctx := context.Background() ctx := context.Background()
carrier := internaltest.NewTextMapCarrier(nil) carrier := internaltest.NewTextMapCarrier(nil)
@ -75,7 +75,7 @@ func TestTextMapPropagatorDelegationNil(t *testing.T) {
} }
func TestTextMapPropagatorFields(t *testing.T) { func TestTextMapPropagatorFields(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
initial := global.TextMapPropagator() initial := global.TextMapPropagator()
delegate := internaltest.NewTextMapPropagator("test") delegate := internaltest.NewTextMapPropagator("test")
delegateFields := delegate.Fields() delegateFields := delegate.Fields()

View File

@ -17,6 +17,7 @@ package global // import "go.opentelemetry.io/otel/internal/global"
import ( import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing"
"go.opentelemetry.io/otel/propagation" "go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace" "go.opentelemetry.io/otel/trace"
@ -97,10 +98,13 @@ func defaultPropagatorsValue() *atomic.Value {
return v return v
} }
// ResetForTest restores the initial global state, for testing purposes. // ResetForTest configures the test to restores the initial global state during
func ResetForTest() { // its Cleanup step
globalTracer = defaultTracerValue() func ResetForTest(t testing.TB) {
globalPropagators = defaultPropagatorsValue() t.Cleanup(func() {
delegateTraceOnce = sync.Once{} globalTracer = defaultTracerValue()
delegateTextMapPropagatorOnce = sync.Once{} globalPropagators = defaultPropagatorsValue()
delegateTraceOnce = sync.Once{}
delegateTextMapPropagatorOnce = sync.Once{}
})
} }

View File

@ -21,7 +21,7 @@ import (
) )
func TestResetsOfGlobalsPanic(t *testing.T) { func TestResetsOfGlobalsPanic(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
tests := map[string]func(){ tests := map[string]func(){
"SetTextMapPropagator": func() { "SetTextMapPropagator": func() {
global.SetTextMapPropagator(global.TextMapPropagator()) global.SetTextMapPropagator(global.TextMapPropagator())

View File

@ -44,7 +44,7 @@ func (fn fnTracer) Start(ctx context.Context, spanName string, opts ...trace.Spa
} }
func TestTraceProviderDelegation(t *testing.T) { func TestTraceProviderDelegation(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
// Map of tracers to expected span names. // Map of tracers to expected span names.
expected := map[string][]string{ expected := map[string][]string{
@ -98,7 +98,7 @@ func TestTraceProviderDelegation(t *testing.T) {
} }
func TestTraceProviderDelegates(t *testing.T) { func TestTraceProviderDelegates(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
// Retrieve the placeholder TracerProvider. // Retrieve the placeholder TracerProvider.
gtp := otel.GetTracerProvider() gtp := otel.GetTracerProvider()
@ -118,7 +118,7 @@ func TestTraceProviderDelegates(t *testing.T) {
} }
func TestTraceProviderDelegatesConcurrentSafe(t *testing.T) { func TestTraceProviderDelegatesConcurrentSafe(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
// Retrieve the placeholder TracerProvider. // Retrieve the placeholder TracerProvider.
gtp := otel.GetTracerProvider() gtp := otel.GetTracerProvider()
@ -161,7 +161,7 @@ func TestTraceProviderDelegatesConcurrentSafe(t *testing.T) {
} }
func TestTracerDelegatesConcurrentSafe(t *testing.T) { func TestTracerDelegatesConcurrentSafe(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
// Retrieve the placeholder TracerProvider. // Retrieve the placeholder TracerProvider.
gtp := otel.GetTracerProvider() gtp := otel.GetTracerProvider()
@ -210,7 +210,7 @@ func TestTracerDelegatesConcurrentSafe(t *testing.T) {
} }
func TestTraceProviderDelegatesSameInstance(t *testing.T) { func TestTraceProviderDelegatesSameInstance(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
// Retrieve the placeholder TracerProvider. // Retrieve the placeholder TracerProvider.
gtp := otel.GetTracerProvider() gtp := otel.GetTracerProvider()
@ -228,7 +228,7 @@ func TestTraceProviderDelegatesSameInstance(t *testing.T) {
} }
func TestSpanContextPropagatedWithNonRecordingSpan(t *testing.T) { func TestSpanContextPropagatedWithNonRecordingSpan(t *testing.T) {
global.ResetForTest() global.ResetForTest(t)
sc := trace.NewSpanContext(trace.SpanContextConfig{ sc := trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{0x01}, TraceID: [16]byte{0x01},