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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/internaltest/text_map_carrier.go.tmpl
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internaltest // import "go.opentelemetry.io/otel/internal/internaltest"
import (
"sync"
"testing"
"go.opentelemetry.io/otel/propagation"
)
// TextMapCarrier is a storage medium for a TextMapPropagator used in testing.
// The methods of a TextMapCarrier are concurrent safe.
type TextMapCarrier struct {
mtx sync.Mutex
gets []string
sets [][2]string
data map[string]string
}
var _ propagation.TextMapCarrier = (*TextMapCarrier)(nil)
// NewTextMapCarrier returns a new *TextMapCarrier populated with data.
func NewTextMapCarrier(data map[string]string) *TextMapCarrier {
copied := make(map[string]string, len(data))
for k, v := range data {
copied[k] = v
}
return &TextMapCarrier{data: copied}
}
// Keys returns the keys for which this carrier has a value.
func (c *TextMapCarrier) Keys() []string {
c.mtx.Lock()
defer c.mtx.Unlock()
result := make([]string, 0, len(c.data))
for k := range c.data {
result = append(result, k)
}
return result
}
// Get returns the value associated with the passed key.
func (c *TextMapCarrier) Get(key string) string {
c.mtx.Lock()
defer c.mtx.Unlock()
c.gets = append(c.gets, key)
return c.data[key]
}
// GotKey tests if c.Get has been called for key.
func (c *TextMapCarrier) GotKey(t *testing.T, key string) bool {
c.mtx.Lock()
defer c.mtx.Unlock()
for _, k := range c.gets {
if k == key {
return true
}
}
t.Errorf("TextMapCarrier.Get(%q) has not been called", key)
return false
}
// GotN tests if n calls to c.Get have been made.
func (c *TextMapCarrier) GotN(t *testing.T, n int) bool {
c.mtx.Lock()
defer c.mtx.Unlock()
if len(c.gets) != n {
t.Errorf("TextMapCarrier.Get was called %d times, not %d", len(c.gets), n)
return false
}
return true
}
// Set stores the key-value pair.
func (c *TextMapCarrier) Set(key, value string) {
c.mtx.Lock()
defer c.mtx.Unlock()
c.sets = append(c.sets, [2]string{key, value})
c.data[key] = value
}
// SetKeyValue tests if c.Set has been called for the key-value pair.
func (c *TextMapCarrier) SetKeyValue(t *testing.T, key, value string) bool {
c.mtx.Lock()
defer c.mtx.Unlock()
var vals []string
for _, pair := range c.sets {
if key == pair[0] {
if value == pair[1] {
return true
}
vals = append(vals, pair[1])
}
}
if len(vals) > 0 {
t.Errorf("TextMapCarrier.Set called with %q and %v values, but not %s", key, vals, value)
}
t.Errorf("TextMapCarrier.Set(%q,%q) has not been called", key, value)
return false
}
// SetN tests if n calls to c.Set have been made.
func (c *TextMapCarrier) SetN(t *testing.T, n int) bool {
c.mtx.Lock()
defer c.mtx.Unlock()
if len(c.sets) != n {
t.Errorf("TextMapCarrier.Set was called %d times, not %d", len(c.sets), n)
return false
}
return true
}
// Reset zeros out the recording state and sets the carried values to data.
func (c *TextMapCarrier) Reset(data map[string]string) {
copied := make(map[string]string, len(data))
for k, v := range data {
copied[k] = v
}
c.mtx.Lock()
defer c.mtx.Unlock()
c.gets = nil
c.sets = nil
c.data = copied
}
|