Skip to content

Commit

Permalink
feat: adds wrapper for asio::async_connect() for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
aritosteles committed Dec 19, 2024
1 parent 62fed9f commit 5508208
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
10 changes: 9 additions & 1 deletion src/agent/communicator/include/http_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,15 @@ namespace http_client
/// @brief Closes the socket
void Close() override;

private:
protected:
/// @brief Wrapper around boost::asio::async_connect to be overridden in unit tests
/// param endpoints The endpoints to connect to
/// param ec The error code, if any occurred
/// param connectionSuccess True if the connection was successful
virtual void CallAsyncConnect(const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& ec,
bool& connectionSuccess);

/// @brief The socket to use for the HTTP connection
boost::asio::ip::tcp::socket m_socket;
};
Expand Down
27 changes: 17 additions & 10 deletions src/agent/communicator/src/http_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ namespace http_client
{
}

void HttpSocket::CallAsyncConnect(const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& ec,
bool& connectionSuccess)
{
boost::asio::async_connect(m_socket,
endpoints,
[&](const boost::system::error_code& errorCode, const auto&)
{
if (!errorCode)
{
connectionSuccess = true;
ec = errorCode;
}
});
}

void HttpSocket::Connect(boost::asio::io_context& ioContext,
const boost::asio::ip::tcp::resolver::results_type& endpoints,
boost::system::error_code& ec,
Expand All @@ -28,16 +44,7 @@ namespace http_client
{
bool connectionSuccess = false;

boost::asio::async_connect(m_socket,
endpoints,
[&](const boost::system::error_code& errorCode, const auto&)
{
if (!errorCode)
{
connectionSuccess = true;
ec = errorCode;
}
});
CallAsyncConnect(endpoints, ec, connectionSuccess);

ioContext.run_for(timeOut);
if (!connectionSuccess)
Expand Down

0 comments on commit 5508208

Please sign in to comment.