1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package global
import (
"testing"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/metric"
metricnoop "go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
tracenoop "go.opentelemetry.io/otel/trace/noop"
)
type nonComparableErrorHandler struct {
ErrorHandler
nonComparable func() //nolint:structcheck,unused // This is not called.
}
type nonComparableTracerProvider struct {
trace.TracerProvider
nonComparable func() //nolint:structcheck,unused // This is not called.
}
type nonComparableMeterProvider struct {
metric.MeterProvider
nonComparable func() //nolint:structcheck,unused // This is not called.
}
type fnErrHandler func(error)
func (f fnErrHandler) Handle(err error) { f(err) }
var noopEH = fnErrHandler(func(error) {})
func TestSetErrorHandler(t *testing.T) {
t.Run("Set With default is a noop", func(t *testing.T) {
ResetForTest(t)
SetErrorHandler(GetErrorHandler())
eh, ok := GetErrorHandler().(*ErrDelegator)
if !ok {
t.Fatal("Global ErrorHandler should be the default ErrorHandler")
}
if eh.delegate.Load() != nil {
t.Fatal("ErrorHandler should not delegate when setting itself")
}
})
t.Run("First Set() should replace the delegate", func(t *testing.T) {
ResetForTest(t)
SetErrorHandler(noopEH)
_, ok := GetErrorHandler().(*ErrDelegator)
if ok {
t.Fatal("Global ErrorHandler was not changed")
}
})
t.Run("Set() should delegate existing ErrorHandlers", func(t *testing.T) {
ResetForTest(t)
eh := GetErrorHandler()
SetErrorHandler(noopEH)
errDel, ok := eh.(*ErrDelegator)
if !ok {
t.Fatal("Wrong ErrorHandler returned")
}
if errDel.delegate.Load() == nil {
t.Fatal("The ErrDelegator should have a delegate")
}
})
t.Run("non-comparable types should not panic", func(t *testing.T) {
ResetForTest(t)
eh := nonComparableErrorHandler{}
assert.NotPanics(t, func() { SetErrorHandler(eh) }, "delegate")
assert.NotPanics(t, func() { SetErrorHandler(eh) }, "replacement")
})
}
func TestSetTracerProvider(t *testing.T) {
t.Run("Set With default is a noop", func(t *testing.T) {
ResetForTest(t)
SetTracerProvider(TracerProvider())
tp, ok := TracerProvider().(*tracerProvider)
if !ok {
t.Fatal("Global TracerProvider should be the default tracer provider")
}
if tp.delegate != nil {
t.Fatal("tracer provider should not delegate when setting itself")
}
})
t.Run("First Set() should replace the delegate", func(t *testing.T) {
ResetForTest(t)
SetTracerProvider(tracenoop.NewTracerProvider())
_, ok := TracerProvider().(*tracerProvider)
if ok {
t.Fatal("Global TracerProvider was not changed")
}
})
t.Run("Set() should delegate existing TracerProviders", func(t *testing.T) {
ResetForTest(t)
tp := TracerProvider()
SetTracerProvider(tracenoop.NewTracerProvider())
ntp := tp.(*tracerProvider)
if ntp.delegate == nil {
t.Fatal("The delegated tracer providers should have a delegate")
}
})
t.Run("non-comparable types should not panic", func(t *testing.T) {
ResetForTest(t)
tp := nonComparableTracerProvider{}
SetTracerProvider(tp)
assert.NotPanics(t, func() { SetTracerProvider(tp) })
})
}
func TestSetTextMapPropagator(t *testing.T) {
t.Run("Set With default is a noop", func(t *testing.T) {
ResetForTest(t)
SetTextMapPropagator(TextMapPropagator())
tmp, ok := TextMapPropagator().(*textMapPropagator)
if !ok {
t.Fatal("Global TextMapPropagator should be the default propagator")
}
if tmp.delegate != nil {
t.Fatal("TextMapPropagator should not delegate when setting itself")
}
})
t.Run("First Set() should replace the delegate", func(t *testing.T) {
ResetForTest(t)
SetTextMapPropagator(propagation.TraceContext{})
_, ok := TextMapPropagator().(*textMapPropagator)
if ok {
t.Fatal("Global TextMapPropagator was not changed")
}
})
t.Run("Set() should delegate existing propagators", func(t *testing.T) {
ResetForTest(t)
p := TextMapPropagator()
SetTextMapPropagator(propagation.TraceContext{})
np := p.(*textMapPropagator)
if np.delegate == nil {
t.Fatal("The delegated TextMapPropagators should have a delegate")
}
})
t.Run("non-comparable types should not panic", func(t *testing.T) {
ResetForTest(t)
// A composite TextMapPropagator is not comparable.
prop := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{})
SetTextMapPropagator(prop)
assert.NotPanics(t, func() { SetTextMapPropagator(prop) })
})
}
func TestSetMeterProvider(t *testing.T) {
t.Run("Set With default is a noop", func(t *testing.T) {
ResetForTest(t)
SetMeterProvider(MeterProvider())
mp, ok := MeterProvider().(*meterProvider)
if !ok {
t.Fatal("Global MeterProvider should be the default meter provider")
}
if mp.delegate != nil {
t.Fatal("meter provider should not delegate when setting itself")
}
})
t.Run("First Set() should replace the delegate", func(t *testing.T) {
ResetForTest(t)
SetMeterProvider(metricnoop.NewMeterProvider())
_, ok := MeterProvider().(*meterProvider)
if ok {
t.Fatal("Global MeterProvider was not changed")
}
})
t.Run("Set() should delegate existing Meter Providers", func(t *testing.T) {
ResetForTest(t)
mp := MeterProvider()
SetMeterProvider(metricnoop.NewMeterProvider())
dmp := mp.(*meterProvider)
if dmp.delegate == nil {
t.Fatal("The delegated meter providers should have a delegate")
}
})
t.Run("non-comparable types should not panic", func(t *testing.T) {
ResetForTest(t)
mp := nonComparableMeterProvider{}
SetMeterProvider(mp)
assert.NotPanics(t, func() { SetMeterProvider(mp) })
})
}
|