| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package global |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"go.opentelemetry.io/otel/internal/internaltest" |
| 10 |
) |
| 11 |
|
| 12 |
func TestTextMapPropagatorDelegation(t *testing.T) { |
| 13 |
ResetForTest(t) |
| 14 |
ctx := t.Context() |
| 15 |
carrier := internaltest.NewTextMapCarrier(nil) |
| 16 |
|
| 17 |
// The default should be a noop. |
| 18 |
initial := TextMapPropagator() |
| 19 |
initial.Inject(ctx, carrier) |
| 20 |
ctx = initial.Extract(ctx, carrier) |
| 21 |
if !carrier.GotN(t, 0) || !carrier.SetN(t, 0) { |
| 22 |
return |
| 23 |
} |
| 24 |
|
| 25 |
// Make sure the delegate woks as expected. |
| 26 |
delegate := internaltest.NewTextMapPropagator("test") |
| 27 |
delegate.Inject(ctx, carrier) |
| 28 |
ctx = delegate.Extract(ctx, carrier) |
| 29 |
if !delegate.InjectedN(t, carrier, 1) || !delegate.ExtractedN(t, ctx, 1) { |
| 30 |
return |
| 31 |
} |
| 32 |
|
| 33 |
// The initial propagator should use the delegate after it is set as the |
| 34 |
// global. |
| 35 |
SetTextMapPropagator(delegate) |
| 36 |
initial.Inject(ctx, carrier) |
| 37 |
ctx = initial.Extract(ctx, carrier) |
| 38 |
delegate.InjectedN(t, carrier, 2) |
| 39 |
delegate.ExtractedN(t, ctx, 2) |
| 40 |
} |
| 41 |
|
| 42 |
func TestTextMapPropagatorDelegationNil(t *testing.T) { |
| 43 |
ResetForTest(t) |
| 44 |
ctx := t.Context() |
| 45 |
carrier := internaltest.NewTextMapCarrier(nil) |
| 46 |
|
| 47 |
// The default should be a noop. |
| 48 |
initial := TextMapPropagator() |
| 49 |
initial.Inject(ctx, carrier) |
| 50 |
ctx = initial.Extract(ctx, carrier) |
| 51 |
if !carrier.GotN(t, 0) || !carrier.SetN(t, 0) { |
| 52 |
return |
| 53 |
} |
| 54 |
|
| 55 |
// Delegation to nil should not make a change. |
| 56 |
SetTextMapPropagator(nil) |
| 57 |
initial.Inject(ctx, carrier) |
| 58 |
initial.Extract(ctx, carrier) |
| 59 |
if !carrier.GotN(t, 0) || !carrier.SetN(t, 0) { |
| 60 |
return |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestTextMapPropagatorFields(t *testing.T) { |
| 65 |
ResetForTest(t) |
| 66 |
initial := TextMapPropagator() |
| 67 |
delegate := internaltest.NewTextMapPropagator("test") |
| 68 |
delegateFields := delegate.Fields() |
| 69 |
|
| 70 |
// Sanity check on the initial Fields. |
| 71 |
if got := initial.Fields(); fieldsEqual(got, delegateFields) { |
| 72 |
t.Fatalf("testing fields (%v) matched Noop fields (%v)", delegateFields, got) |
| 73 |
} |
| 74 |
SetTextMapPropagator(delegate) |
| 75 |
// Check previous returns from global not correctly delegate. |
| 76 |
if got := initial.Fields(); !fieldsEqual(got, delegateFields) { |
| 77 |
t.Errorf("global TextMapPropagator.Fields returned %v instead of delegating, want (%v)", got, delegateFields) |
| 78 |
} |
| 79 |
// Check new calls to global. |
| 80 |
if got := TextMapPropagator().Fields(); !fieldsEqual(got, delegateFields) { |
| 81 |
t.Errorf("global TextMapPropagator.Fields returned %v, want (%v)", got, delegateFields) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func fieldsEqual(f1, f2 []string) bool { |
| 86 |
if len(f1) != len(f2) { |
| 87 |
return false |
| 88 |
} |
| 89 |
for i := range f1 { |
| 90 |
if f1[i] != f2[i] { |
| 91 |
return false |
| 92 |
} |
| 93 |
} |
| 94 |
return true |
| 95 |
} |