|
40 | 40 | # include <grpcpp/grpcpp.h> |
41 | 41 | # include <gtest/gtest.h> |
42 | 42 | # include <algorithm> |
| 43 | +# include <chrono> |
| 44 | +# include <cstddef> |
| 45 | +# include <functional> |
43 | 46 | # include <future> |
| 47 | +# include <memory> |
| 48 | +# include <mutex> |
| 49 | +# include <thread> |
44 | 50 | # include <utility> |
45 | 51 | # include <vector> |
46 | 52 |
|
@@ -86,6 +92,13 @@ class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceSe |
86 | 92 | ::opentelemetry::proto::collector::trace::v1::ExportTraceServiceResponse *response, |
87 | 93 | std::function<void(::grpc::Status)> callback) override |
88 | 94 | { |
| 95 | + if (stub_->defer_completions_) |
| 96 | + { |
| 97 | + std::lock_guard<std::mutex> guard(stub_->deferred_lock_); |
| 98 | + stub_->deferred_.push_back(std::move(callback)); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
89 | 102 | stub_->last_async_status_ = stub_->Export(context, *request, response); |
90 | 103 | callback(stub_->last_async_status_); |
91 | 104 | } |
@@ -118,11 +131,68 @@ class OtlpMockTraceServiceStub : public proto::collector::trace::v1::MockTraceSe |
118 | 131 |
|
119 | 132 | ::grpc::Status GetLastAsyncStatus() const noexcept { return last_async_status_; } |
120 | 133 |
|
| 134 | + // Keep the completion callbacks instead of running them inline, so a test can call ForceFlush() |
| 135 | + // with requests still in flight. Call before the first export. |
| 136 | + void DeferCompletions() noexcept { defer_completions_ = true; } |
| 137 | + |
| 138 | + std::size_t DeferredCount() |
| 139 | + { |
| 140 | + std::lock_guard<std::mutex> guard(deferred_lock_); |
| 141 | + return deferred_.size(); |
| 142 | + } |
| 143 | + |
| 144 | + // Completes whichever deferred request has been waiting longest, false when there is none. |
| 145 | + bool CompleteOneDeferred() |
| 146 | + { |
| 147 | + std::function<void(::grpc::Status)> callback; |
| 148 | + { |
| 149 | + std::lock_guard<std::mutex> guard(deferred_lock_); |
| 150 | + if (deferred_.empty()) |
| 151 | + { |
| 152 | + return false; |
| 153 | + } |
| 154 | + callback = std::move(deferred_.front()); |
| 155 | + deferred_.erase(deferred_.begin()); |
| 156 | + } |
| 157 | + |
| 158 | + callback(::grpc::Status::OK); |
| 159 | + return true; |
| 160 | + } |
| 161 | + |
| 162 | + void CompleteAllDeferred() |
| 163 | + { |
| 164 | + while (CompleteOneDeferred()) |
| 165 | + { |
| 166 | + } |
| 167 | + } |
| 168 | + |
121 | 169 | private: |
122 | 170 | ::grpc::Status last_async_status_; |
| 171 | + bool defer_completions_{false}; |
| 172 | + std::mutex deferred_lock_; |
| 173 | + std::vector<std::function<void(::grpc::Status)>> deferred_; |
123 | 174 | async_interface async_interface_; |
124 | 175 | }; |
125 | 176 |
|
| 177 | +// Completes whatever is still deferred on the way out. ~OtlpGrpcClient waits for |
| 178 | +// running_requests to reach zero with no deadline, and TryCancel() cannot move it for a request |
| 179 | +// the stub is holding, so an assertion that ends a case early would hang the binary instead of |
| 180 | +// failing it. Declare after the exporter, so this runs first. |
| 181 | +class DrainDeferredCompletions |
| 182 | +{ |
| 183 | +public: |
| 184 | + explicit DrainDeferredCompletions(OtlpMockTraceServiceStub *stub) noexcept : stub_(stub) {} |
| 185 | + ~DrainDeferredCompletions() { stub_->CompleteAllDeferred(); } |
| 186 | + |
| 187 | + DrainDeferredCompletions(const DrainDeferredCompletions &) = delete; |
| 188 | + DrainDeferredCompletions &operator=(const DrainDeferredCompletions &) = delete; |
| 189 | + DrainDeferredCompletions(DrainDeferredCompletions &&) = delete; |
| 190 | + DrainDeferredCompletions &operator=(DrainDeferredCompletions &&) = delete; |
| 191 | + |
| 192 | +private: |
| 193 | + OtlpMockTraceServiceStub *stub_; |
| 194 | +}; |
| 195 | + |
126 | 196 | using opentelemetry::test_common::ScopedTestLogHandler; |
127 | 197 | } // namespace |
128 | 198 |
|
@@ -244,6 +314,92 @@ TEST_F(OtlpGrpcExporterTestPeer, ExportPartialSuccess) |
244 | 314 | EXPECT_TRUE(contains("too many spans!!")); |
245 | 315 | } |
246 | 316 |
|
| 317 | +namespace |
| 318 | +{ |
| 319 | +// ForceFlush() only has requests to wait for when the client tracks them, which it does under |
| 320 | +// ENABLE_ASYNC_EXPORT. gtest_add_tests registers by source text, so the cases below have to exist |
| 321 | +// in every build and opt out here rather than be compiled away. |
| 322 | +class OtlpGrpcExporterFlushTestPeer : public OtlpGrpcExporterTestPeer |
| 323 | +{ |
| 324 | +protected: |
| 325 | + void SetUp() override |
| 326 | + { |
| 327 | +# if !defined(ENABLE_ASYNC_EXPORT) |
| 328 | + GTEST_SKIP() << "ForceFlush has no in-flight requests to wait for without async export"; |
| 329 | +# endif |
| 330 | + } |
| 331 | +}; |
| 332 | + |
| 333 | +// The wait is bounded by the exporter's timeout, so a caller asking for less than that used to |
| 334 | +// wait for the larger of the two. |
| 335 | +TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushReturnsWithinTheCallerDeadline) |
| 336 | +{ |
| 337 | + auto *mock_stub = new OtlpMockTraceServiceStub(); |
| 338 | + mock_stub->DeferCompletions(); |
| 339 | + std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub_interface( |
| 340 | + mock_stub); |
| 341 | + auto exporter = GetExporter(stub_interface); |
| 342 | + DrainDeferredCompletions drain{mock_stub}; |
| 343 | + |
| 344 | + auto recordable = exporter->MakeRecordable(); |
| 345 | + recordable->SetName("Test span"); |
| 346 | + nostd::span<std::unique_ptr<sdk::trace::Recordable>> batch(&recordable, 1); |
| 347 | + exporter->Export(batch); |
| 348 | + ASSERT_EQ(1u, mock_stub->DeferredCount()); |
| 349 | + |
| 350 | + const auto started = std::chrono::steady_clock::now(); |
| 351 | + const bool flushed = exporter->ForceFlush(std::chrono::milliseconds{50}); |
| 352 | + const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 353 | + std::chrono::steady_clock::now() - started); |
| 354 | + |
| 355 | + EXPECT_FALSE(flushed); |
| 356 | + EXPECT_LT(elapsed.count(), 1000) |
| 357 | + << "waited on the export timeout rather than the one it was given"; |
| 358 | +} |
| 359 | + |
| 360 | +// A completion for one request used to end the wait for every other request, and the leftover |
| 361 | +// deadline was then reported as success. |
| 362 | +TEST_F(OtlpGrpcExporterFlushTestPeer, ForceFlushKeepsWaitingWhenAnotherRequestCompletes) |
| 363 | +{ |
| 364 | + auto *mock_stub = new OtlpMockTraceServiceStub(); |
| 365 | + mock_stub->DeferCompletions(); |
| 366 | + std::unique_ptr<proto::collector::trace::v1::TraceService::StubInterface> stub_interface( |
| 367 | + mock_stub); |
| 368 | + auto exporter = GetExporter(stub_interface); |
| 369 | + DrainDeferredCompletions drain{mock_stub}; |
| 370 | + |
| 371 | + auto first = exporter->MakeRecordable(); |
| 372 | + first->SetName("Test span 1"); |
| 373 | + nostd::span<std::unique_ptr<sdk::trace::Recordable>> first_batch(&first, 1); |
| 374 | + exporter->Export(first_batch); |
| 375 | + |
| 376 | + auto second = exporter->MakeRecordable(); |
| 377 | + second->SetName("Test span 2"); |
| 378 | + nostd::span<std::unique_ptr<sdk::trace::Recordable>> second_batch(&second, 1); |
| 379 | + exporter->Export(second_batch); |
| 380 | + |
| 381 | + ASSERT_EQ(2u, mock_stub->DeferredCount()); |
| 382 | + |
| 383 | + // Completed once the flush below is parked, so it arrives as a notification rather than as a |
| 384 | + // counter the loop condition reads on its way in. |
| 385 | + std::thread completer([mock_stub] { |
| 386 | + std::this_thread::sleep_for(std::chrono::milliseconds{100}); |
| 387 | + mock_stub->CompleteOneDeferred(); |
| 388 | + }); |
| 389 | + |
| 390 | + const auto started = std::chrono::steady_clock::now(); |
| 391 | + const bool flushed = exporter->ForceFlush(std::chrono::milliseconds{1000}); |
| 392 | + const auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 393 | + std::chrono::steady_clock::now() - started); |
| 394 | + completer.join(); |
| 395 | + |
| 396 | + EXPECT_FALSE(flushed); |
| 397 | + // Ran its own deadline down rather than returning on the notification. |
| 398 | + EXPECT_GE(elapsed.count(), 500); |
| 399 | + EXPECT_EQ(1u, mock_stub->DeferredCount()); |
| 400 | +} |
| 401 | +} // namespace |
| 402 | + |
247 | 403 | // Create spans, let processor call Export() |
248 | 404 | TEST_F(OtlpGrpcExporterTestPeer, ExportIntegrationTest) |
249 | 405 | { |
|
0 commit comments