]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/jaeger-client-cpp/src/jaegertracing/testutils/MockAgentTest.cpp
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / jaeger-client-cpp / src / jaegertracing / testutils / MockAgentTest.cpp
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 <sstream>
18
19 #include <gtest/gtest.h>
20 #include <nlohmann/json.hpp>
21
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"
26
27 namespace jaegertracing {
28 namespace testutils {
29
30 TEST(MockAgent, testSpanServer)
31 {
32 std::shared_ptr<MockAgent> mockAgent = MockAgent::make();
33 mockAgent->start();
34
35 auto client = mockAgent->spanServerClient();
36
37 constexpr auto kBiggestBatch = 5;
38 for (auto i = 1; i < kBiggestBatch; ++i) {
39 thrift::Batch batch;
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);
45 }
46
47 client->emitBatch(batch);
48
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) {
55 break;
56 }
57 }
58
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);
66 }
67 mockAgent->resetBatches();
68 }
69 }
70
71 TEST(MockAgent, testSamplingManager)
72 {
73 auto mockAgent = MockAgent::make();
74 mockAgent->start();
75
76 {
77 std::ostringstream oss;
78 oss << "http://" << mockAgent->samplingServerAddress().authority()
79 << '/';
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());
84 }
85 {
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());
93 }
94 {
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);
105 }
106 {
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);
114
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);
124 }
125 }
126
127 } // namespace testutils
128 } // namespace jaegertracing