Debian Codegraph @

internal/internaltest/text_map_propagator_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internaltest
5
6 import (
7 "testing"
8 )
9
10 func TestTextMapPropagatorInjectExtract(t *testing.T) {
11 name := "testing"
12 ctx := t.Context()
13 carrier := NewTextMapCarrier(map[string]string{name: value})
14 propagator := NewTextMapPropagator(name)
15
16 propagator.Inject(ctx, carrier)
17 // Carrier value overridden with state.
18 if carrier.SetKeyValue(t, name, "1,0") {
19 // Ensure nothing has been extracted yet.
20 propagator.ExtractedN(t, ctx, 0)
21 // Test the injection was counted.
22 propagator.InjectedN(t, carrier, 1)
23 }
24
25 ctx = propagator.Extract(ctx, carrier)
26 v := ctx.Value(ctxKeyType(name))
27 if v == nil {
28 t.Error("TextMapPropagator.Extract failed to extract state")
29 }
30 if s, ok := v.(state); !ok {
31 t.Error("TextMapPropagator.Extract did not extract proper state")
32 } else if s.Extractions != 1 {
33 t.Error("TextMapPropagator.Extract did not increment state.Extractions")
34 }
35 if carrier.GotKey(t, name) {
36 // Test the extraction was counted.
37 propagator.ExtractedN(t, ctx, 1)
38 // Ensure no additional injection was recorded.
39 propagator.InjectedN(t, carrier, 1)
40 }
41 }
42
43 func TestTextMapPropagatorFields(t *testing.T) {
44 name := "testing"
45 propagator := NewTextMapPropagator(name)
46 if got := propagator.Fields(); len(got) != 1 {
47 t.Errorf("TextMapPropagator.Fields returned %d fields, want 1", len(got))
48 } else if got[0] != name {
49 t.Errorf("TextMapPropagator.Fields returned %q, want %q", got[0], name)
50 }
51 }
52
53 func TestNewStateEmpty(t *testing.T) {
54 if want, got := (state{}), newState(""); got != want {
55 t.Errorf("newState(\"\") returned %v, want %v", got, want)
56 }
57 }