Debian Codegraph @

internal/internaltest/text_map_carrier.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package internaltest // import "go.opentelemetry.io/otel/internal/internaltest"
5
6 import (
7 "maps"
8 "slices"
9 "sync"
10 "testing"
11
12 "go.opentelemetry.io/otel/propagation"
13 )
14
15 // TextMapCarrier is a storage medium for a TextMapPropagator used in testing.
16 // The methods of a TextMapCarrier are concurrent safe.
17 type TextMapCarrier struct {
18 mtx sync.Mutex
19
20 gets []string
21 sets [][2]string
22 data map[string]string
23 }
24
25 var _ propagation.TextMapCarrier = (*TextMapCarrier)(nil)
26
27 // NewTextMapCarrier returns a new *TextMapCarrier populated with data.
28 func NewTextMapCarrier(data map[string]string) *TextMapCarrier {
29 copied := make(map[string]string, len(data))
30 maps.Copy(copied, data)
31 return &TextMapCarrier{data: copied}
32 }
33
34 // Keys returns the keys for which this carrier has a value.
35 func (c *TextMapCarrier) Keys() []string {
36 c.mtx.Lock()
37 defer c.mtx.Unlock()
38
39 result := make([]string, 0, len(c.data))
40 for k := range c.data {
41 result = append(result, k)
42 }
43 return result
44 }
45
46 // Get returns the value associated with the passed key.
47 func (c *TextMapCarrier) Get(key string) string {
48 c.mtx.Lock()
49 defer c.mtx.Unlock()
50 c.gets = append(c.gets, key)
51 return c.data[key]
52 }
53
54 // GotKey tests if c.Get has been called for key.
55 func (c *TextMapCarrier) GotKey(t *testing.T, key string) bool {
56 c.mtx.Lock()
57 defer c.mtx.Unlock()
58 if slices.Contains(c.gets, key) {
59 return true
60 }
61 t.Errorf("TextMapCarrier.Get(%q) has not been called", key)
62 return false
63 }
64
65 // GotN tests if n calls to c.Get have been made.
66 func (c *TextMapCarrier) GotN(t *testing.T, n int) bool {
67 c.mtx.Lock()
68 defer c.mtx.Unlock()
69 if len(c.gets) != n {
70 t.Errorf("TextMapCarrier.Get was called %d times, not %d", len(c.gets), n)
71 return false
72 }
73 return true
74 }
75
76 // Set stores the key-value pair.
77 func (c *TextMapCarrier) Set(key, value string) {
78 c.mtx.Lock()
79 defer c.mtx.Unlock()
80 c.sets = append(c.sets, [2]string{key, value})
81 c.data[key] = value
82 }
83
84 // SetKeyValue tests if c.Set has been called for the key-value pair.
85 func (c *TextMapCarrier) SetKeyValue(t *testing.T, key, value string) bool {
86 c.mtx.Lock()
87 defer c.mtx.Unlock()
88 var vals []string
89 for _, pair := range c.sets {
90 if key == pair[0] {
91 if value == pair[1] {
92 return true
93 }
94 vals = append(vals, pair[1])
95 }
96 }
97 if len(vals) > 0 {
98 t.Errorf("TextMapCarrier.Set called with %q and %v values, but not %s", key, vals, value)
99 }
100 t.Errorf("TextMapCarrier.Set(%q,%q) has not been called", key, value)
101 return false
102 }
103
104 // SetN tests if n calls to c.Set have been made.
105 func (c *TextMapCarrier) SetN(t *testing.T, n int) bool {
106 c.mtx.Lock()
107 defer c.mtx.Unlock()
108 if len(c.sets) != n {
109 t.Errorf("TextMapCarrier.Set was called %d times, not %d", len(c.sets), n)
110 return false
111 }
112 return true
113 }
114
115 // Reset zeros out the recording state and sets the carried values to data.
116 func (c *TextMapCarrier) Reset(data map[string]string) {
117 copied := make(map[string]string, len(data))
118 maps.Copy(copied, data)
119
120 c.mtx.Lock()
121 defer c.mtx.Unlock()
122
123 c.gets = nil
124 c.sets = nil
125 c.data = copied
126 }