]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Copyright (c) 2017-2018 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 <gtest/gtest.h> | |
18 | ||
19 | #include "jaegertracing/Tracer.h" | |
20 | #include "jaegertracing/ThriftSender.h" | |
21 | #include "jaegertracing/testutils/TracerUtil.h" | |
22 | #include "jaegertracing/utils/ErrorUtil.h" | |
23 | ||
24 | namespace jaegertracing { | |
25 | namespace { | |
26 | ||
27 | class MockUDPSender : public utils::UDPTransporter { | |
28 | public: | |
29 | enum class ExceptionType { kSystemError, kException, kString }; | |
30 | ||
31 | MockUDPSender(const net::IPAddress& serverAddr, | |
32 | int maxPacketSize, | |
33 | ExceptionType type) | |
34 | : UDPTransporter(serverAddr, maxPacketSize) | |
35 | , _type(type) | |
36 | { | |
37 | } | |
38 | ||
39 | private: | |
40 | void emitBatch(const thrift::Batch& batch) override | |
41 | { | |
42 | switch (_type) { | |
43 | case ExceptionType::kSystemError: | |
44 | throw std::system_error( | |
45 | std::make_error_code(std::errc::invalid_argument)); | |
46 | case ExceptionType::kException: | |
47 | throw std::exception(); | |
48 | default: | |
49 | assert(_type == ExceptionType::kString); | |
50 | throw "error"; | |
51 | } | |
52 | } | |
53 | ||
54 | ExceptionType _type; | |
55 | }; | |
56 | ||
57 | class MockThriftSender : public ThriftSender { | |
58 | public: | |
59 | MockThriftSender(const net::IPAddress& ip, | |
60 | int maxPacketSize, | |
61 | MockUDPSender::ExceptionType type) | |
62 | : ThriftSender(std::unique_ptr<utils::Transport>(new MockUDPSender(ip, maxPacketSize, type))) | |
63 | { | |
64 | } | |
65 | }; | |
66 | ||
67 | } // anonymous namespace | |
68 | ||
69 | TEST(ThriftSender, testManyMessages) | |
70 | { | |
71 | const auto handle = testutils::TracerUtil::installGlobalTracer(); | |
72 | const auto tracer = | |
73 | std::static_pointer_cast<const Tracer>(opentracing::Tracer::Global()); | |
74 | ||
75 | std::unique_ptr<utils::Transport> transporter( | |
76 | new utils::UDPTransporter(handle->_mockAgent->spanServerAddress(), 0)); | |
77 | ThriftSender sender( | |
78 | std::forward<std::unique_ptr<utils::Transport>>(transporter)); | |
79 | constexpr auto kNumMessages = 2000; | |
80 | const auto logger = logging::consoleLogger(); | |
81 | for (auto i = 0; i < kNumMessages; ++i) { | |
82 | Span span(tracer); | |
83 | span.SetOperationName("test" + std::to_string(i)); | |
84 | ASSERT_NO_THROW(sender.append(span)); | |
85 | } | |
86 | } | |
87 | ||
88 | TEST(ThriftSender, testExceptions) | |
89 | { | |
90 | const auto handle = testutils::TracerUtil::installGlobalTracer(); | |
91 | const auto tracer = | |
92 | std::static_pointer_cast<const Tracer>(opentracing::Tracer::Global()); | |
93 | ||
94 | Span span(tracer); | |
95 | span.SetOperationName("test"); | |
96 | ||
97 | const MockUDPSender::ExceptionType exceptionTypes[] = { | |
98 | MockUDPSender::ExceptionType::kSystemError, | |
99 | MockUDPSender::ExceptionType::kException, | |
100 | MockUDPSender::ExceptionType::kString | |
101 | }; | |
102 | for (auto type : exceptionTypes) { | |
103 | MockThriftSender mockSender(net::IPAddress::v4("localhost", 0), 0, type); | |
104 | mockSender.append(span); | |
105 | ASSERT_THROW(mockSender.flush(), Sender::Exception); | |
106 | } | |
107 | } | |
108 | ||
109 | } // namespace jaegertracing |