Make HTTP livness timout configurable for unittests

It's annoying to have to wait 10 seconds for the `liveness_disconnect`
test to complete, so make the timeout configurable and set it to a way
lower value to test the functionality.
This commit is contained in:
Yonas Habteab 2025-08-29 15:06:52 +02:00
parent cfbad92d9b
commit 97ad0fc552
3 changed files with 26 additions and 5 deletions

View File

@ -152,6 +152,11 @@ bool HttpServerConnection::Disconnected()
return m_ShuttingDown;
}
void HttpServerConnection::SetLivenessTimeout(double seconds)
{
m_LivenessTimeout = seconds;
}
static inline
bool EnsureValidHeaders(
boost::beast::flat_buffer& buf,
@ -542,14 +547,17 @@ void HttpServerConnection::CheckLiveness(boost::asio::yield_context yc)
boost::system::error_code ec;
for (;;) {
m_CheckLivenessTimer.expires_from_now(boost::posix_time::seconds(5));
// Wait for the half of the liveness timeout to give the connection some leeway to do other work.
// But never wait longer than 5 seconds to ensure timely shutdowns.
auto sleepTime = std::min(5ll*1000, static_cast<long long>(m_LivenessTimeout * 500));
m_CheckLivenessTimer.expires_from_now(boost::posix_time::milliseconds(sleepTime));
m_CheckLivenessTimer.async_wait(yc[ec]);
if (m_ShuttingDown) {
break;
}
if (m_Seen < Utility::GetTime() - 10) {
if (m_Seen < Utility::GetTime() - m_LivenessTimeout) {
Log(LogInformation, "HttpServerConnection")
<< "No messages for HTTP connection have been received in the last 10 seconds.";

View File

@ -33,11 +33,23 @@ public:
void StartDetectClientSideShutdown();
bool Disconnected();
/**
* Sets the liveness timeout in seconds.
*
* If we don't receive any data from the client in this time frame, we consider the connection
* dead and close it. The default is 10 seconds. This function should only be used for unit tests
* to speed them up.
*
* @param seconds The timeout in seconds.
*/
void SetLivenessTimeout(double seconds);
private:
WaitGroup::Ptr m_WaitGroup;
ApiUser::Ptr m_ApiUser;
Shared<AsioTlsStream>::Ptr m_Stream;
double m_Seen;
double m_LivenessTimeout{10.0}; // The liveness timeout in seconds. @see SetLivenessTimeout() for details.
String m_PeerAddress;
boost::asio::io_context::strand m_IoStrand;
bool m_ShuttingDown;

View File

@ -45,10 +45,11 @@ struct HttpServerConnectionFixture : TlsStreamFixture, ConfigurationCacheDirFixt
user->Register();
}
void SetupHttpServerConnection(bool authenticated)
void SetupHttpServerConnection(bool authenticated, double livenessTimeout = 10.0)
{
String identity = authenticated ? "client" : "invalid";
m_Connection = new HttpServerConnection(m_WaitGroup, identity, authenticated, server);
m_Connection->SetLivenessTimeout(livenessTimeout);
m_Connection->Start();
}
@ -552,9 +553,9 @@ BOOST_AUTO_TEST_CASE(handler_throw_streaming)
BOOST_AUTO_TEST_CASE(liveness_disconnect)
{
SetupHttpServerConnection(false);
SetupHttpServerConnection(false, .300); // 300ms liveness timeout is more than enough!
BOOST_REQUIRE(AssertServerDisconnected(std::chrono::seconds(11)));
BOOST_REQUIRE(AssertServerDisconnected(std::chrono::milliseconds(450))); // Give some leeway to Asio's timers
BOOST_REQUIRE(ExpectLogPattern("HTTP client disconnected .*"));
BOOST_REQUIRE(ExpectLogPattern("No messages for HTTP connection have been received in the last 10 seconds."));
BOOST_REQUIRE(Shutdown(client));