Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions ext/test/http/curl_http_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<http_client::curl::HttpCurlClientFactory>()->Create();
std::static_pointer_cast<curl::HttpClient>(http_client)->MaybeSpawnBackgroundThread();
// start background first, then test it could wakeup
Expand All @@ -929,19 +933,22 @@ TEST_F(BasicCurlHttpTests, ElegantQuitQuick)
request->SetUri("get/");
auto handler = std::make_shared<GetEventHandler>();
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<std::chrono::milliseconds>(cost).count()
<< " libcurl version: 0x" << std::hex << LIBCURL_VERSION_NUM;
ASSERT_TRUE(handler->is_called_);
ASSERT_TRUE(handler->got_response_);
}

TEST_F(BasicCurlHttpTests, BackgroundThreadWaitMore)
Expand Down
Loading