...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package internaltest
19
20 import (
21 "sync"
22 "testing"
23
24 "go.opentelemetry.io/otel/propagation"
25 )
26
27
28
29 type TextMapCarrier struct {
30 mtx sync.Mutex
31
32 gets []string
33 sets [][2]string
34 data map[string]string
35 }
36
37 var _ propagation.TextMapCarrier = (*TextMapCarrier)(nil)
38
39
40 func NewTextMapCarrier(data map[string]string) *TextMapCarrier {
41 copied := make(map[string]string, len(data))
42 for k, v := range data {
43 copied[k] = v
44 }
45 return &TextMapCarrier{data: copied}
46 }
47
48
49 func (c *TextMapCarrier) Keys() []string {
50 c.mtx.Lock()
51 defer c.mtx.Unlock()
52
53 result := make([]string, 0, len(c.data))
54 for k := range c.data {
55 result = append(result, k)
56 }
57 return result
58 }
59
60
61 func (c *TextMapCarrier) Get(key string) string {
62 c.mtx.Lock()
63 defer c.mtx.Unlock()
64 c.gets = append(c.gets, key)
65 return c.data[key]
66 }
67
68
69 func (c *TextMapCarrier) GotKey(t *testing.T, key string) bool {
70 c.mtx.Lock()
71 defer c.mtx.Unlock()
72 for _, k := range c.gets {
73 if k == key {
74 return true
75 }
76 }
77 t.Errorf("TextMapCarrier.Get(%q) has not been called", key)
78 return false
79 }
80
81
82 func (c *TextMapCarrier) GotN(t *testing.T, n int) bool {
83 c.mtx.Lock()
84 defer c.mtx.Unlock()
85 if len(c.gets) != n {
86 t.Errorf("TextMapCarrier.Get was called %d times, not %d", len(c.gets), n)
87 return false
88 }
89 return true
90 }
91
92
93 func (c *TextMapCarrier) Set(key, value string) {
94 c.mtx.Lock()
95 defer c.mtx.Unlock()
96 c.sets = append(c.sets, [2]string{key, value})
97 c.data[key] = value
98 }
99
100
101 func (c *TextMapCarrier) SetKeyValue(t *testing.T, key, value string) bool {
102 c.mtx.Lock()
103 defer c.mtx.Unlock()
104 var vals []string
105 for _, pair := range c.sets {
106 if key == pair[0] {
107 if value == pair[1] {
108 return true
109 }
110 vals = append(vals, pair[1])
111 }
112 }
113 if len(vals) > 0 {
114 t.Errorf("TextMapCarrier.Set called with %q and %v values, but not %s", key, vals, value)
115 }
116 t.Errorf("TextMapCarrier.Set(%q,%q) has not been called", key, value)
117 return false
118 }
119
120
121 func (c *TextMapCarrier) SetN(t *testing.T, n int) bool {
122 c.mtx.Lock()
123 defer c.mtx.Unlock()
124 if len(c.sets) != n {
125 t.Errorf("TextMapCarrier.Set was called %d times, not %d", len(c.sets), n)
126 return false
127 }
128 return true
129 }
130
131
132 func (c *TextMapCarrier) Reset(data map[string]string) {
133 copied := make(map[string]string, len(data))
134 for k, v := range data {
135 copied[k] = v
136 }
137
138 c.mtx.Lock()
139 defer c.mtx.Unlock()
140
141 c.gets = nil
142 c.sets = nil
143 c.data = copied
144 }
145
View as plain text