|
3 | 3 |
|
4 | 4 | #ifndef OPENTELEMETRY_STL_VERSION |
5 | 5 |
|
| 6 | +# include <atomic> |
6 | 7 | # include <chrono> |
7 | 8 | # include <memory> |
8 | 9 | # include <utility> |
@@ -91,6 +92,32 @@ class OtlpHttpExporterCustomClientTestPeer : public ::testing::Test |
91 | 92 | return {new OtlpHttpClient(MakeOtlpHttpClientOptions(), http_client), http_client}; |
92 | 93 | } |
93 | 94 |
|
| 95 | + // The same, with the outcome recorded. A request that never settles calls this no times, and one |
| 96 | + // that settles twice calls it twice, so both failures the terminal states can produce are |
| 97 | + // visible in the count rather than only in a timeout. |
| 98 | + static void ExportOneRequest(OtlpHttpClient &otlp_client, |
| 99 | + std::shared_ptr<std::atomic<int>> calls, |
| 100 | + std::shared_ptr<sdk::common::ExportResult> result) |
| 101 | + { |
| 102 | + auto arena = std::make_unique<google::protobuf::Arena>(); |
| 103 | + auto *request = |
| 104 | + google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceRequest>( |
| 105 | + arena.get()); |
| 106 | + auto *response = |
| 107 | + google::protobuf::Arena::Create<proto::collector::trace::v1::ExportTraceServiceResponse>( |
| 108 | + arena.get()); |
| 109 | + |
| 110 | + otlp_client.Export( |
| 111 | + *request, std::move(arena), response, |
| 112 | + [calls, result](opentelemetry::sdk::common::ExportResult outcome, |
| 113 | + google::protobuf::Message *) { |
| 114 | + calls->fetch_add(1, std::memory_order_release); |
| 115 | + *result = outcome; |
| 116 | + return true; |
| 117 | + }, |
| 118 | + 1); |
| 119 | + } |
| 120 | + |
94 | 121 | // A non-zero request budget keeps the export asynchronous, so it returns while the session runs. |
95 | 122 | static void ExportOneRequest(OtlpHttpClient &otlp_client) |
96 | 123 | { |
@@ -235,6 +262,89 @@ TEST_F(OtlpHttpExporterCustomClientTestPeer, ForceFlushReportsSuccessOnceTheSess |
235 | 262 | EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); |
236 | 263 | } |
237 | 264 |
|
| 265 | +// Every state the client can finish a request on has to end that request. These three used to be |
| 266 | +// logged and then dropped: the switch that decides whether the request is over did not name them, |
| 267 | +// so a client that ended a transfer this way left the caller waiting for a callback that was not |
| 268 | +// coming, and ForceFlush and Shutdown could only give up on their own deadlines. |
| 269 | +class OtlpHttpTerminalStateTest : public OtlpHttpExporterCustomClientTestPeer, |
| 270 | + public ::testing::WithParamInterface<http_client::SessionState> |
| 271 | +{}; |
| 272 | + |
| 273 | +TEST_P(OtlpHttpTerminalStateTest, ATerminalClientEventEndsTheRequest) |
| 274 | +{ |
| 275 | + auto client = http_client::HttpClientTestFactory::Create(); |
| 276 | + auto no_send_client = std::static_pointer_cast<http_client::nosend::HttpClient>(client); |
| 277 | + auto session = std::static_pointer_cast<http_client::nosend::Session>(no_send_client->session_); |
| 278 | + |
| 279 | + std::shared_ptr<opentelemetry::ext::http::client::EventHandler> pending; |
| 280 | + EXPECT_CALL(*session, SendRequest) |
| 281 | + .WillRepeatedly( |
| 282 | + [&pending](std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) { |
| 283 | + pending = std::move(callback); |
| 284 | + }); |
| 285 | + |
| 286 | + OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); |
| 287 | + |
| 288 | + auto calls = std::make_shared<std::atomic<int>>(0); |
| 289 | + auto result = std::make_shared<sdk::common::ExportResult>(sdk::common::ExportResult::kSuccess); |
| 290 | + ExportOneRequest(otlp_client, calls, result); |
| 291 | + ASSERT_NE(pending, nullptr); |
| 292 | + |
| 293 | + pending->OnEvent(GetParam(), ""); |
| 294 | + |
| 295 | + EXPECT_EQ(1, calls->load(std::memory_order_acquire)) |
| 296 | + << "the request was not ended by the state the client finished on"; |
| 297 | + EXPECT_EQ(sdk::common::ExportResult::kFailure, *result); |
| 298 | + |
| 299 | + // The deadline is short on purpose. Without the request being ended above this waits it out. |
| 300 | + EXPECT_TRUE(otlp_client.ForceFlush(std::chrono::milliseconds{50})); |
| 301 | +} |
| 302 | + |
| 303 | +INSTANTIATE_TEST_SUITE_P(TerminalStates, |
| 304 | + OtlpHttpTerminalStateTest, |
| 305 | + ::testing::Values(http_client::SessionState::ReadError, |
| 306 | + http_client::SessionState::WriteError, |
| 307 | + http_client::SessionState::Destroyed)); |
| 308 | + |
| 309 | +// The other direction. A client is free to report one of those states after it has already |
| 310 | +// delivered a response, and the outcome the caller was given must not be replaced or repeated. |
| 311 | +TEST_F(OtlpHttpExporterCustomClientTestPeer, ALateTerminalEventDoesNotReportASecondTime) |
| 312 | +{ |
| 313 | + for (const auto state : |
| 314 | + {http_client::SessionState::ReadError, http_client::SessionState::WriteError, |
| 315 | + http_client::SessionState::Destroyed}) |
| 316 | + { |
| 317 | + SCOPED_TRACE(static_cast<int>(state)); |
| 318 | + |
| 319 | + auto client = http_client::HttpClientTestFactory::Create(); |
| 320 | + auto no_send_client = std::static_pointer_cast<http_client::nosend::HttpClient>(client); |
| 321 | + auto session = std::static_pointer_cast<http_client::nosend::Session>(no_send_client->session_); |
| 322 | + |
| 323 | + std::shared_ptr<opentelemetry::ext::http::client::EventHandler> pending; |
| 324 | + EXPECT_CALL(*session, SendRequest) |
| 325 | + .WillRepeatedly( |
| 326 | + [&pending](std::shared_ptr<opentelemetry::ext::http::client::EventHandler> callback) { |
| 327 | + pending = std::move(callback); |
| 328 | + }); |
| 329 | + |
| 330 | + OtlpHttpClient otlp_client(MakeOtlpHttpClientOptions(std::chrono::seconds{30}), client); |
| 331 | + |
| 332 | + auto calls = std::make_shared<std::atomic<int>>(0); |
| 333 | + auto result = std::make_shared<sdk::common::ExportResult>(sdk::common::ExportResult::kFailure); |
| 334 | + ExportOneRequest(otlp_client, calls, result); |
| 335 | + ASSERT_NE(pending, nullptr); |
| 336 | + |
| 337 | + http_client::nosend::Response sent; |
| 338 | + sent.Finish(*pending); |
| 339 | + ASSERT_EQ(1, calls->load(std::memory_order_acquire)); |
| 340 | + |
| 341 | + pending->OnEvent(state, ""); |
| 342 | + |
| 343 | + EXPECT_EQ(1, calls->load(std::memory_order_acquire)) |
| 344 | + << "a state arriving after the response reported the request a second time"; |
| 345 | + } |
| 346 | +} |
| 347 | + |
238 | 348 | } // namespace otlp |
239 | 349 | } // namespace exporter |
240 | 350 | OPENTELEMETRY_END_NAMESPACE |
|
0 commit comments