Create generate free port class to avoid conflicting ports (#1439)

Fixes #1317
This commit is contained in:
Peter Chen
2024-06-18 06:29:05 -04:00
committed by GitHub
parent 5ba08b1d26
commit e135aa49d5
14 changed files with 313 additions and 149 deletions

View File

@@ -105,9 +105,9 @@ doSession(
} // namespace
TestHttpServer::TestHttpServer(boost::asio::io_context& context, std::string host, int const port) : acceptor_(context)
TestHttpServer::TestHttpServer(boost::asio::io_context& context, std::string host) : acceptor_(context)
{
boost::asio::ip::tcp::endpoint const endpoint(boost::asio::ip::make_address(host), port);
boost::asio::ip::tcp::endpoint const endpoint(boost::asio::ip::make_address(host), 0);
acceptor_.open(endpoint.protocol());
acceptor_.set_option(asio::socket_base::reuse_address(true));
acceptor_.bind(endpoint);
@@ -134,3 +134,9 @@ TestHttpServer::handleRequest(TestHttpServer::RequestHandler handler, bool const
boost::asio::detached
);
}
std::string
TestHttpServer::port() const
{
return std::to_string(acceptor_.local_endpoint().port());
}