diff --git a/ext/test/http/curl_http_test.cc b/ext/test/http/curl_http_test.cc index 90142962d..9a5f58413 100644 --- a/ext/test/http/curl_http_test.cc +++ b/ext/test/http/curl_http_test.cc @@ -919,8 +919,12 @@ TEST_F(BasicCurlHttpTests, FinishInAsyncCallback) } } +// Destroying the client wakes the polling background thread instead of letting it sleep out +// scheduled_delay_milliseconds_. A missed wakeup is slow rather than wrong, so the bound is the +// assertion: measured, the quit is under a millisecond and a slept out poll is 256 ms. TEST_F(BasicCurlHttpTests, ElegantQuitQuick) { + received_requests_.clear(); auto http_client = std::make_shared()->Create(); std::static_pointer_cast(http_client)->MaybeSpawnBackgroundThread(); // start background first, then test it could wakeup @@ -929,19 +933,22 @@ TEST_F(BasicCurlHttpTests, ElegantQuitQuick) request->SetUri("get/"); auto handler = std::make_shared(); session->SendRequest(handler); - std::this_thread::sleep_for(std::chrono::milliseconds{10}); // let it enter poll state + + // Sending is not what is timed, so a slow request must not read as a slow quit. + ASSERT_TRUE(waitForRequests(30, 1)); + session->FinishSession(); + ASSERT_TRUE(handler->is_called_.load(std::memory_order_acquire)); + ASSERT_TRUE(handler->got_response_.load(std::memory_order_acquire)); + auto beg = std::chrono::system_clock::now(); http_client->FinishAllSessions(); http_client.reset(); - // when background_thread_wait_for_ is used, it should have no side effect on elegant quit - // wait should be less than scheduled_delay_milliseconds_ - // Due to load on CI hosts (some take 10ms), we assert it is less than 20ms auto cost = std::chrono::system_clock::now() - beg; - ASSERT_TRUE(cost < std::chrono::milliseconds{20}) + + // background_thread_wait_for_ keeps the thread alive here and must not delay the quit. + ASSERT_TRUE(cost < std::chrono::milliseconds{100}) << "cost ms: " << std::chrono::duration_cast(cost).count() << " libcurl version: 0x" << std::hex << LIBCURL_VERSION_NUM; - ASSERT_TRUE(handler->is_called_); - ASSERT_TRUE(handler->got_response_); } TEST_F(BasicCurlHttpTests, BackgroundThreadWaitMore)