2 * Copyright (c) 2017 Uber Technologies, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <gtest/gtest.h>
20 #include <nlohmann/json.hpp>
22 #include "jaegertracing/baggage/RemoteRestrictionJSON.h"
23 #include "jaegertracing/net/http/Response.h"
24 #include "jaegertracing/samplers/RemoteSamplingJSON.h"
25 #include "jaegertracing/testutils/MockAgent.h"
27 namespace jaegertracing
{
30 TEST(MockAgent
, testSpanServer
)
32 std::shared_ptr
<MockAgent
> mockAgent
= MockAgent::make();
35 auto client
= mockAgent
->spanServerClient();
37 constexpr auto kBiggestBatch
= 5;
38 for (auto i
= 1; i
< kBiggestBatch
; ++i
) {
40 batch
.spans
.resize(i
);
41 for (auto j
= 0; j
< i
; ++j
) {
42 std::string
operationName("span-");
43 operationName
+= std::to_string(j
);
44 batch
.spans
[j
].__set_operationName(operationName
);
47 client
->emitBatch(batch
);
49 constexpr auto kNumTries
= 100;
50 for (auto k
= 0; k
< kNumTries
; ++k
) {
51 std::this_thread::sleep_for(std::chrono::milliseconds(1));
52 const auto batches
= mockAgent
->batches();
53 if (!batches
.empty() &&
54 static_cast<int>(batches
[0].spans
.size()) == i
) {
59 const auto batches
= mockAgent
->batches();
60 ASSERT_FALSE(batches
.empty());
61 ASSERT_EQ(i
, static_cast<int>(batches
[0].spans
.size()));
62 for (auto j
= 0; j
< i
; ++j
) {
63 std::string
operationName("span-");
64 operationName
+= std::to_string(j
);
65 ASSERT_EQ(operationName
, batches
[0].spans
[j
].operationName
);
67 mockAgent
->resetBatches();
71 TEST(MockAgent
, testSamplingManager
)
73 auto mockAgent
= MockAgent::make();
77 std::ostringstream oss
;
78 oss
<< "http://" << mockAgent
->samplingServerAddress().authority()
80 const auto uriStr
= oss
.str();
81 const auto uri
= net::URI::parse(uriStr
);
82 const auto response
= net::http::get(uri
);
83 ASSERT_EQ("no 'service' parameter", response
.body());
86 std::ostringstream oss
;
87 oss
<< "http://" << mockAgent
->samplingServerAddress().authority()
88 << "/?service=a&service=b";
89 const auto uriStr
= oss
.str();
90 const auto uri
= net::URI::parse(uriStr
);
91 const auto response
= net::http::get(uri
);
92 ASSERT_EQ("'service' parameter must occur only once", response
.body());
95 std::ostringstream oss
;
96 oss
<< "http://" << mockAgent
->samplingServerAddress().authority()
97 << "/?service=something";
98 const auto uriStr
= oss
.str();
99 const auto uri
= net::URI::parse(uriStr
);
100 const auto responseHTTP
= net::http::get(uri
);
101 sampling_manager::thrift::SamplingStrategyResponse response
;
102 response
= nlohmann::json::parse(responseHTTP
.body());
103 ASSERT_EQ(sampling_manager::thrift::SamplingStrategyType::PROBABILISTIC
,
104 response
.strategyType
);
107 sampling_manager::thrift::SamplingStrategyResponse config
;
108 config
.__set_strategyType(
109 sampling_manager::thrift::SamplingStrategyType::RATE_LIMITING
);
110 sampling_manager::thrift::RateLimitingSamplingStrategy rateLimiting
;
111 rateLimiting
.__set_maxTracesPerSecond(123);
112 config
.__set_rateLimitingSampling(rateLimiting
);
113 mockAgent
->addSamplingStrategy("service123", config
);
115 std::ostringstream oss
;
116 oss
<< "http://" << mockAgent
->samplingServerAddress().authority()
117 << "/?service=service123";
118 const auto uriStr
= oss
.str();
119 const auto uri
= net::URI::parse(uriStr
);
120 const auto responseHTTP
= net::http::get(uri
);
121 sampling_manager::thrift::SamplingStrategyResponse response
;
122 response
= nlohmann::json::parse(responseHTTP
.body());
123 ASSERT_EQ(config
, response
);
127 } // namespace testutils
128 } // namespace jaegertracing