From 97ad0fc552e5b9b02c9f9e3d7bd46cddc08f5288 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 29 Aug 2025 15:06:52 +0200 Subject: [PATCH] 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. --- lib/remote/httpserverconnection.cpp | 12 ++++++++++-- lib/remote/httpserverconnection.hpp | 12 ++++++++++++ test/remote-httpserverconnection.cpp | 7 ++++--- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/lib/remote/httpserverconnection.cpp b/lib/remote/httpserverconnection.cpp index d8befd211..fde33fdcc 100644 --- a/lib/remote/httpserverconnection.cpp +++ b/lib/remote/httpserverconnection.cpp @@ -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(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."; diff --git a/lib/remote/httpserverconnection.hpp b/lib/remote/httpserverconnection.hpp index 1f3d5d7f9..63f1ad6f7 100644 --- a/lib/remote/httpserverconnection.hpp +++ b/lib/remote/httpserverconnection.hpp @@ -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::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; diff --git a/test/remote-httpserverconnection.cpp b/test/remote-httpserverconnection.cpp index f0b45125d..7596ae325 100644 --- a/test/remote-httpserverconnection.cpp +++ b/test/remote-httpserverconnection.cpp @@ -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));