]>
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 <gtest/gtest.h> | |
18 | ||
19 | #include "jaegertracing/Logging.h" | |
20 | #include "jaegertracing/Tracer.h" | |
21 | #include "jaegertracing/Sender.h" | |
22 | #include "jaegertracing/reporters/CompositeReporter.h" | |
23 | #include "jaegertracing/reporters/InMemoryReporter.h" | |
24 | #include "jaegertracing/reporters/LoggingReporter.h" | |
25 | #include "jaegertracing/reporters/NullReporter.h" | |
26 | #include "jaegertracing/reporters/RemoteReporter.h" | |
27 | #include "jaegertracing/samplers/ConstSampler.h" | |
28 | ||
29 | namespace jaegertracing { | |
30 | namespace reporters { | |
31 | namespace { | |
32 | ||
33 | class FakeTransport : public Sender { | |
34 | public: | |
35 | FakeTransport(std::vector<Span>& spans, std::mutex& mutex) | |
36 | : _spans(spans) | |
37 | , _mutex(mutex) | |
38 | { | |
39 | } | |
40 | ||
41 | int append(const Span& span) override | |
42 | { | |
43 | std::lock_guard<std::mutex> lock(_mutex); | |
44 | _spans.push_back(span); | |
45 | return 1; | |
46 | } | |
47 | ||
48 | int flush() override { return 0; } | |
49 | ||
50 | void close() override {} | |
51 | ||
52 | private: | |
53 | std::vector<Span>& _spans; | |
54 | std::mutex& _mutex; | |
55 | }; | |
56 | ||
57 | const Span span; | |
58 | ||
59 | } // anonymous namespace | |
60 | ||
61 | TEST(Reporter, testRemoteReporter) | |
62 | { | |
63 | std::vector<Span> spans; | |
64 | std::mutex mutex; | |
65 | auto logger = logging::nullLogger(); | |
66 | auto metrics = metrics::Metrics::makeNullMetrics(); | |
67 | constexpr auto kFixedQueueSize = 10; | |
68 | RemoteReporter reporter( | |
69 | std::chrono::milliseconds(1), | |
70 | kFixedQueueSize, | |
71 | std::unique_ptr<Sender>(new FakeTransport(spans, mutex)), | |
72 | *logger, | |
73 | *metrics); | |
74 | constexpr auto kNumReports = 100; | |
75 | for (auto i = 0; i < kNumReports; ++i) { | |
76 | reporter.report(span); | |
77 | // TODO(isaachier): Find a way to make this test more rigorous. | |
78 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | |
79 | } | |
80 | reporter.close(); | |
81 | ASSERT_EQ(spans.size(), kNumReports); | |
82 | } | |
83 | ||
84 | TEST(Reporter, testNullReporter) | |
85 | { | |
86 | NullReporter reporter; | |
87 | constexpr auto kNumReports = 100; | |
88 | for (auto i = 0; i < kNumReports; ++i) { | |
89 | reporter.report(span); | |
90 | } | |
91 | reporter.close(); | |
92 | } | |
93 | ||
94 | TEST(Reporter, testLoggingReporter) | |
95 | { | |
96 | const auto logger = logging::nullLogger(); | |
97 | LoggingReporter reporter(*logger); | |
98 | constexpr auto kNumReports = 100; | |
99 | for (auto i = 0; i < kNumReports; ++i) { | |
100 | reporter.report(span); | |
101 | } | |
102 | reporter.close(); | |
103 | } | |
104 | ||
105 | TEST(Reporter, testInMemoryReporter) | |
106 | { | |
107 | InMemoryReporter reporter; | |
108 | constexpr auto kNumReports = 100; | |
109 | for (auto i = 0; i < kNumReports; ++i) { | |
110 | reporter.report(span); | |
111 | } | |
112 | ASSERT_EQ(kNumReports, reporter.spansSubmitted()); | |
113 | reporter.reset(); | |
114 | ASSERT_EQ(0, reporter.spansSubmitted()); | |
115 | reporter.close(); | |
116 | } | |
117 | ||
118 | TEST(Reporter, testCompositeReporter) | |
119 | { | |
120 | std::vector<std::shared_ptr<Reporter>> reporters; | |
121 | reporters.push_back(std::make_shared<InMemoryReporter>()); | |
122 | reporters.push_back(std::make_shared<InMemoryReporter>()); | |
123 | ||
124 | CompositeReporter reporter(reporters); | |
125 | reporter.report(span); | |
126 | ASSERT_EQ(1, | |
127 | std::static_pointer_cast<InMemoryReporter>(reporters[0]) | |
128 | ->spansSubmitted()); | |
129 | ASSERT_EQ(1, | |
130 | std::static_pointer_cast<InMemoryReporter>(reporters[1]) | |
131 | ->spansSubmitted()); | |
132 | } | |
133 | ||
134 | } // namespace reporters | |
135 | } // namespace jaegertracing |