MCPcopy
hub / github.com/grpc/grpc-go / TestOutgoingCarrier

Method TestOutgoingCarrier

stats/opentelemetry/internal/tracing/carrier_test.go:133–190  ·  view source on GitHub ↗

TestOutgoingCarrier verifies that a key-value pair is set in carrier's context metadata using `OutgoingCarrier.Set()`. If key is not present, it verifies that key-value pair is insterted. If key is already present, it verifies that new value is appended at the end of list for the existing key. If k

(t *testing.T)

Source from the content-addressed store, hash-verified

131// It also verifies that both existing and newly inserted keys are present in
132// the carrier's context using `Carrier.Keys()`.
133func (s) TestOutgoingCarrier(t *testing.T) {
134 tests := []struct {
135 name string
136 initialMD metadata.MD
137 setKey string
138 setValue string
139 wantValue string // expected value of the set key
140 wantKeys []string
141 }{
142 {
143 name: "new key",
144 initialMD: metadata.MD{},
145 setKey: "key1",
146 setValue: "value1",
147 wantValue: "value1",
148 wantKeys: []string{"key1"},
149 },
150 {
151 name: "add to existing key",
152 initialMD: metadata.MD{"key1": []string{"oldvalue"}},
153 setKey: "key1",
154 setValue: "newvalue",
155 wantValue: "newvalue",
156 wantKeys: []string{"key1"},
157 },
158 {
159 name: "new key with different existing key",
160 initialMD: metadata.MD{"key2": []string{"value2"}},
161 setKey: "key1",
162 setValue: "value1",
163 wantValue: "value1",
164 wantKeys: []string{"key2", "key1"},
165 },
166 {
167 name: "grpc-trace-bin binary key",
168 initialMD: metadata.MD{"key1": []string{"value1"}},
169 setKey: "grpc-trace-bin",
170 setValue: string([]byte{0x01, 0x02, 0x03}),
171 wantValue: string([]byte{0x01, 0x02, 0x03}),
172 wantKeys: []string{"key1", "grpc-trace-bin"},
173 },
174 }
175
176 for _, test := range tests {
177 t.Run(test.name, func(t *testing.T) {
178 ctx, cancel := context.WithCancel(context.Background())
179 defer cancel()
180 c := NewOutgoingCarrier(metadata.NewOutgoingContext(ctx, test.initialMD))
181 c.Set(test.setKey, test.setValue)
182 if gotKeys := c.Keys(); !cmp.Equal(test.wantKeys, gotKeys, cmpopts.SortSlices(func(a, b string) bool { return a < b })) {
183 t.Fatalf("c.Keys() = keys %v, want %v", gotKeys, test.wantKeys)
184 }
185 if md, ok := metadata.FromOutgoingContext(c.Context()); ok && md.Get(test.setKey)[len(md.Get(test.setKey))-1] != test.wantValue {
186 t.Fatalf("got value %s, want %s, for key %s", md.Get(test.setKey)[len(md.Get(test.setKey))-1], test.wantValue, test.setKey)
187 }
188 })
189 }
190}

Callers

nothing calls this directly

Calls 9

SetMethod · 0.95
KeysMethod · 0.95
ContextMethod · 0.95
NewOutgoingContextFunction · 0.92
FromOutgoingContextFunction · 0.92
NewOutgoingCarrierFunction · 0.85
EqualMethod · 0.65
FatalfMethod · 0.65
GetMethod · 0.65

Tested by

no test coverage detected