File: text_map_propagator_test.go

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (61 lines) | stat: -rw-r--r-- 1,797 bytes parent folder | download | duplicates (8)
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
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/internaltest/text_map_propagator_test.go.tmpl

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internaltest

import (
	"context"
	"testing"
)

func TestTextMapPropagatorInjectExtract(t *testing.T) {
	name := "testing"
	ctx := context.Background()
	carrier := NewTextMapCarrier(map[string]string{name: value})
	propagator := NewTextMapPropagator(name)

	propagator.Inject(ctx, carrier)
	// Carrier value overridden with state.
	if carrier.SetKeyValue(t, name, "1,0") {
		// Ensure nothing has been extracted yet.
		propagator.ExtractedN(t, ctx, 0)
		// Test the injection was counted.
		propagator.InjectedN(t, carrier, 1)
	}

	ctx = propagator.Extract(ctx, carrier)
	v := ctx.Value(ctxKeyType(name))
	if v == nil {
		t.Error("TextMapPropagator.Extract failed to extract state")
	}
	if s, ok := v.(state); !ok {
		t.Error("TextMapPropagator.Extract did not extract proper state")
	} else if s.Extractions != 1 {
		t.Error("TextMapPropagator.Extract did not increment state.Extractions")
	}
	if carrier.GotKey(t, name) {
		// Test the extraction was counted.
		propagator.ExtractedN(t, ctx, 1)
		// Ensure no additional injection was recorded.
		propagator.InjectedN(t, carrier, 1)
	}
}

func TestTextMapPropagatorFields(t *testing.T) {
	name := "testing"
	propagator := NewTextMapPropagator(name)
	if got := propagator.Fields(); len(got) != 1 {
		t.Errorf("TextMapPropagator.Fields returned %d fields, want 1", len(got))
	} else if got[0] != name {
		t.Errorf("TextMapPropagator.Fields returned %q, want %q", got[0], name)
	}
}

func TestNewStateEmpty(t *testing.T) {
	if want, got := (state{}), newState(""); got != want {
		t.Errorf("newState(\"\") returned %v, want %v", got, want)
	}
}