1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-25 22:41:46 +02:00
Files
opentelemetry-go/internal/internaltest/text_map_propagator_test.go
Flc゛ 80cb909774 refactor: replace context.Background() with t.Context()/b.Context() in tests (#7352)
Based on the Go version we currently use, the dependency already
supports 1.24+, which allows using `t.Context()` and `b.Context()` in
unit tests and benchmarks respectively.

- Enable `context-background` and `context-todo` in
[`usetesting`](https://golangci-lint.run/docs/linters/configuration/#usetesting)
- Adjust the code to support linter detection

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
Co-authored-by: Damien Mathieu <42@dmathieu.com>
2025-09-23 09:52:45 +02:00

58 lines
1.6 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package internaltest
import (
"testing"
)
func TestTextMapPropagatorInjectExtract(t *testing.T) {
name := "testing"
ctx := t.Context()
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)
}
}