File: registry_test.go

package info (click to toggle)
golang-opentelemetry-contrib 0.56.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,884 kB
  • sloc: makefile: 278; sh: 211; sed: 1
file content (85 lines) | stat: -rw-r--r-- 1,935 bytes parent folder | download
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package autoprop

import (
	"fmt"
	"sync"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"go.opentelemetry.io/otel/propagation"
)

var noop = propagation.NewCompositeTextMapPropagator()

func TestRegistryEmptyStore(t *testing.T) {
	r := registry{}
	assert.NotPanics(t, func() {
		require.NoError(t, r.store("first", noop))
	})
}

func TestRegistryEmptyLoad(t *testing.T) {
	r := registry{}
	assert.NotPanics(t, func() {
		v, ok := r.load("non-existent")
		assert.False(t, ok, "empty registry should hold nothing")
		assert.Nil(t, v, "non-nil propagator returned")
	})
}

func TestRegistryConcurrentSafe(t *testing.T) {
	const propName = "prop"

	r := registry{}
	assert.NotPanics(t, func() {
		require.NoError(t, r.store(propName, noop))
	})

	var wg sync.WaitGroup

	wg.Add(1)
	go func() {
		defer wg.Done()
		assert.NotPanics(t, func() {
			require.ErrorIs(t, r.store(propName, noop), errDupReg)
		})
	}()

	wg.Add(1)
	go func() {
		defer wg.Done()
		assert.NotPanics(t, func() {
			v, ok := r.load(propName)
			assert.True(t, ok, "missing propagator in registry")
			assert.Equal(t, noop, v, "wrong propagator returned")
		})
	}()

	wg.Wait()
}

func TestRegisterTextMapPropagator(t *testing.T) {
	const propName = "custom"
	RegisterTextMapPropagator(propName, noop)
	t.Cleanup(func() { propagators.drop(propName) })

	v, ok := propagators.load(propName)
	assert.True(t, ok, "missing propagator in envRegistry")
	assert.Equal(t, noop, v, "wrong propagator stored")
}

func TestDuplicateRegisterTextMapPropagatorPanics(t *testing.T) {
	const propName = "custom"
	RegisterTextMapPropagator(propName, noop)
	t.Cleanup(func() { propagators.drop(propName) })

	errString := fmt.Sprintf("%s: %q", errDupReg, propName)
	assert.PanicsWithError(t, errString, func() {
		RegisterTextMapPropagator(propName, noop)
	})
}