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) {
// Compare with BenchmarkStartEndSpan() in
// ../../sdk/trace/benchmark_test.go.
global.ResetForTest()
global.ResetForTest(b)
t := otel.Tracer("Benchmark StartEndSpan")
ctx := context.Background()
b.ResetTimer()

View File

@ -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()

View File

@ -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{}
})
}

View File

@ -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())

View File

@ -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},