15
15
#include < lightstep/catch/catch.hpp>
16
16
17
17
using namespace lightstep ;
18
- using namespace opentracing ;
19
18
20
19
// ------------------------------------------------------------------------------
21
20
// TextMapCarrier
22
21
// ------------------------------------------------------------------------------
23
- struct TextMapCarrier : TextMapReader, TextMapWriter {
22
+ struct TextMapCarrier : opentracing:: TextMapReader, opentracing:: TextMapWriter {
24
23
TextMapCarrier (std::unordered_map<std::string, std::string>& text_map_)
25
24
: text_map(text_map_) {}
26
25
27
- expected<void > Set (string_view key, string_view value) const override {
26
+ opentracing::expected<void > Set (
27
+ opentracing::string_view key,
28
+ opentracing::string_view value) const override {
28
29
text_map[key] = value;
29
30
return {};
30
31
}
31
32
32
- expected<void > ForeachKey (
33
- std::function<expected<void >(string_view key, string_view value)> f)
34
- const override {
33
+ opentracing::expected<opentracing::string_view> LookupKey (
34
+ opentracing::string_view key) const override {
35
+ if (!supports_lookup) {
36
+ return opentracing::make_unexpected (
37
+ opentracing::lookup_key_not_supported_error);
38
+ }
39
+ auto iter = text_map.find (key);
40
+ if (iter != text_map.end ()) {
41
+ return opentracing::string_view{iter->second };
42
+ } else {
43
+ return opentracing::make_unexpected (opentracing::key_not_found_error);
44
+ }
45
+ }
46
+
47
+ opentracing::expected<void > ForeachKey (
48
+ std::function<opentracing::expected<void >(opentracing::string_view key,
49
+ opentracing::string_view value)>
50
+ f) const override {
51
+ ++foreach_key_call_count;
35
52
for (const auto & key_value : text_map) {
36
53
auto result = f (key_value.first , key_value.second );
37
54
if (!result) return result;
38
55
}
39
56
return {};
40
57
}
41
58
59
+ bool supports_lookup = false ;
60
+ mutable int foreach_key_call_count = 0 ;
42
61
std::unordered_map<std::string, std::string>& text_map;
43
62
};
44
63
45
64
// ------------------------------------------------------------------------------
46
65
// HTTPHeadersCarrier
47
66
// ------------------------------------------------------------------------------
48
- struct HTTPHeadersCarrier : HTTPHeadersReader, HTTPHeadersWriter {
67
+ struct HTTPHeadersCarrier : opentracing::HTTPHeadersReader,
68
+ opentracing::HTTPHeadersWriter {
49
69
HTTPHeadersCarrier (std::unordered_map<std::string, std::string>& text_map_)
50
70
: text_map(text_map_) {}
51
71
52
- expected<void > Set (string_view key, string_view value) const override {
72
+ opentracing::expected<void > Set (
73
+ opentracing::string_view key,
74
+ opentracing::string_view value) const override {
53
75
text_map[key] = value;
54
76
return {};
55
77
}
56
78
57
- expected<void > ForeachKey (
58
- std::function<expected<void >(string_view key, string_view value)> f)
59
- const override {
79
+ opentracing::expected<void > ForeachKey (
80
+ std::function<opentracing::expected<void >(opentracing::string_view key,
81
+ opentracing::string_view value)>
82
+ f) const override {
60
83
for (const auto & key_value : text_map) {
61
84
auto result = f (key_value.first , key_value.second );
62
85
if (!result) return result;
@@ -132,13 +155,13 @@ TEST_CASE("propagation") {
132
155
133
156
SECTION (
134
157
" Injecting a non-LightStep span returns invalid_span_context_error." ) {
135
- auto noop_tracer = MakeNoopTracer ();
158
+ auto noop_tracer = opentracing:: MakeNoopTracer ();
136
159
CHECK (noop_tracer);
137
160
auto span = noop_tracer->StartSpan (" a" );
138
161
CHECK (span);
139
162
auto was_successful = tracer->Inject (span->context (), text_map_carrier);
140
163
CHECK (!was_successful);
141
- CHECK (was_successful.error () == invalid_span_context_error);
164
+ CHECK (was_successful.error () == opentracing:: invalid_span_context_error);
142
165
}
143
166
144
167
SECTION (
@@ -152,7 +175,8 @@ TEST_CASE("propagation") {
152
175
text_map.erase (std::begin (text_map));
153
176
auto span_context_maybe = tracer->Extract (text_map_carrier);
154
177
CHECK (!span_context_maybe);
155
- CHECK (span_context_maybe.error () == span_context_corrupted_error);
178
+ CHECK (span_context_maybe.error () ==
179
+ opentracing::span_context_corrupted_error);
156
180
}
157
181
158
182
SECTION (" Extract is insensitive to changes in case for http header fields" ) {
@@ -192,7 +216,8 @@ TEST_CASE("propagation") {
192
216
std::istringstream iss{invalid_context, std::ios::binary};
193
217
auto span_context_maybe = tracer->Extract (iss);
194
218
CHECK (!span_context_maybe);
195
- CHECK (span_context_maybe.error () == span_context_corrupted_error);
219
+ CHECK (span_context_maybe.error () ==
220
+ opentracing::span_context_corrupted_error);
196
221
}
197
222
198
223
SECTION (" Calling Extract on an empty stream yields a nullptr." ) {
@@ -218,8 +243,6 @@ TEST_CASE("propagation - single key") {
218
243
CHECK (tracer->Inject (span->context (), text_map_carrier));
219
244
CHECK (text_map.size () == 1 );
220
245
221
- for (auto & kv : text_map) std::cout << kv.first << " : " << kv.second << " \n " ;
222
-
223
246
SECTION (" Inject, extract, inject yields the same text_map." ) {
224
247
auto injection_map1 = text_map;
225
248
auto span_context_maybe = tracer->Extract (text_map_carrier);
@@ -229,8 +252,26 @@ TEST_CASE("propagation - single key") {
229
252
CHECK (injection_map1 == text_map);
230
253
}
231
254
255
+ SECTION (" If a carrier supports LookupKey, then ForeachKey won't be called" ) {
256
+ text_map_carrier.supports_lookup = true ;
257
+ auto span_context_maybe = tracer->Extract (text_map_carrier);
258
+ CHECK ((span_context_maybe && span_context_maybe->get ()));
259
+ CHECK (text_map_carrier.foreach_key_call_count == 0 );
260
+ }
261
+
262
+ SECTION (
263
+ " When LookupKey is used, a nullptr is returned if there is no "
264
+ " span_context" ) {
265
+ text_map.clear ();
266
+ text_map_carrier.supports_lookup = true ;
267
+ auto span_context_maybe = tracer->Extract (text_map_carrier);
268
+ CHECK ((span_context_maybe && span_context_maybe->get () == nullptr ));
269
+ CHECK (text_map_carrier.foreach_key_call_count == 0 );
270
+ }
271
+
232
272
SECTION (" Verify only valid base64 characters are used." ) {
233
- // Follows the guidelines given in RFC-4648. See
273
+ // Follows the guidelines given in RFC-4648 on what characters are
274
+ // permissible. See
234
275
// http://www.rfc-editor.org/rfc/rfc4648.txt
235
276
auto iter = text_map.begin ();
236
277
CHECK (iter != text_map.end ());
0 commit comments