]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017 Uber Technologies, Inc. | |
3 | * | |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at | |
7 | * | |
8 | * http://www.apache.org/licenses/LICENSE-2.0 | |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
15 | */ | |
16 | ||
17 | #include "jaegertracing/SpanContext.h" | |
18 | #include "jaegertracing/TraceID.h" | |
19 | #include <algorithm> | |
20 | #include <gtest/gtest.h> | |
21 | #include <sstream> | |
22 | #include <string> | |
23 | ||
24 | namespace jaegertracing { | |
25 | namespace { | |
26 | ||
27 | struct FromStreamTestCase { | |
28 | std::string _input; | |
29 | bool _success; | |
30 | }; | |
31 | ||
32 | } // anonymous namespace | |
33 | ||
34 | TEST(SpanContext, testFromStream) | |
35 | { | |
36 | const FromStreamTestCase testCases[] = { | |
37 | { "", false }, | |
38 | { "abcd", false }, | |
39 | { "ABCD", false }, | |
40 | { "x:1:1:1", false }, | |
41 | { "1:x:1:1", false }, | |
42 | { "1:1:x:1", false }, | |
43 | { "1:1:1:x", false }, | |
44 | { "01234567890123456789012345678901234:1:1:1", false }, | |
45 | { "01234567890123456789012345678901:1:1:1", true }, | |
46 | { "01234_67890123456789012345678901:1:1:1", false }, | |
47 | { "0123456789012345678901_345678901:1:1:1", false }, | |
48 | { "1:0123456789012345:1:1", true }, | |
49 | { "1:01234567890123456:1:1", false }, | |
50 | { "10000000000000001:1:1:1", true }, | |
51 | { "10000000000000001:1:1", false }, | |
52 | { "1:1:1:1", true } | |
53 | }; | |
54 | ||
55 | for (auto&& testCase : testCases) { | |
56 | SpanContext spanContext; | |
57 | { | |
58 | std::stringstream ss; | |
59 | ss << testCase._input; | |
60 | spanContext = SpanContext::fromStream(ss); | |
61 | ASSERT_EQ(testCase._success, spanContext.isValid()) | |
62 | << "input=" << testCase._input; | |
63 | } | |
64 | ||
65 | SpanContext spanContextFromStreamOp; | |
66 | { | |
67 | std::stringstream ss; | |
68 | ss << testCase._input; | |
69 | ss >> spanContextFromStreamOp; | |
70 | } | |
71 | ||
72 | ASSERT_EQ(spanContext, spanContextFromStreamOp); | |
73 | } | |
74 | } | |
75 | ||
76 | TEST(SpanContext, testFormatting) | |
77 | { | |
78 | SpanContext spanContext(TraceID(255, 255), 0, 0, 0, SpanContext::StrMap()); | |
79 | std::ostringstream oss; | |
80 | oss << spanContext; | |
81 | ASSERT_EQ("ff00000000000000ff:0:0:0", oss.str()); | |
82 | } | |
83 | ||
84 | TEST(SpanContext, testBaggage) | |
85 | { | |
86 | const SpanContext spanContext( | |
87 | TraceID(0, 0), | |
88 | 0, | |
89 | 0, | |
90 | 0, | |
91 | SpanContext::StrMap({ { "key1", "value1" }, { "key2", "value2" } })); | |
92 | std::string keyCopy; | |
93 | std::string valueCopy; | |
94 | spanContext.ForeachBaggageItem( | |
95 | [&keyCopy, &valueCopy](const std::string& key, | |
96 | const std::string& value) { | |
97 | keyCopy = key; | |
98 | valueCopy = value; | |
99 | return false; | |
100 | }); | |
101 | ASSERT_TRUE(keyCopy == "key1" || keyCopy == "key2"); | |
102 | if (keyCopy == "key1") { | |
103 | ASSERT_EQ("value1", valueCopy); | |
104 | } | |
105 | else { | |
106 | ASSERT_EQ("value2", valueCopy); | |
107 | } | |
108 | } | |
109 | ||
110 | TEST(SpanContext, testDebug) | |
111 | { | |
112 | const SpanContext spanContext; | |
113 | ASSERT_FALSE(spanContext.isDebug()); | |
114 | ASSERT_FALSE(spanContext.isDebugIDContainerOnly()); | |
115 | } | |
116 | ||
117 | } // namespace jaegertracing |