Skip to content

Commit

Permalink
Create generate free port class to avoid conflicting ports (#1439)
Browse files Browse the repository at this point in the history
Fixes #1317
  • Loading branch information
PeterChen13579 authored Jun 18, 2024
1 parent 5ba08b1 commit e135aa4
Show file tree
Hide file tree
Showing 14 changed files with 313 additions and 149 deletions.
1 change: 1 addition & 0 deletions tests/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(clio_testing_common)

target_sources(
clio_testing_common PRIVATE util/StringUtils.cpp util/TestHttpServer.cpp util/TestWsServer.cpp util/TestObject.cpp
util/AssignRandomPort.cpp
)

include(deps/gtest)
Expand Down
45 changes: 45 additions & 0 deletions tests/common/util/AssignRandomPort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#include "util/AssignRandomPort.hpp"

#include <boost/asio/io_context.hpp>
#include <boost/asio/ip/tcp.hpp>

#include <cstdint>

using tcp = boost::asio::ip::tcp;

namespace tests::util {

uint32_t
generateFreePort()
{
boost::asio::io_context io_context;
tcp::acceptor acceptor(io_context);
tcp::endpoint const endpoint(tcp::v4(), 0);

acceptor.open(endpoint.protocol());
acceptor.set_option(tcp::acceptor::reuse_address(true));
acceptor.bind(endpoint);

return acceptor.local_endpoint().port();
}

} // namespace tests::util
29 changes: 29 additions & 0 deletions tests/common/util/AssignRandomPort.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2024, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#pragma once

#include <cstdint>

namespace tests::util {

uint32_t
generateFreePort();

} // namespace tests::util
8 changes: 7 additions & 1 deletion tests/common/util/MockXrpLedgerAPIService.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ struct WithMockXrpLedgerAPIService : virtual ::testing::Test {
WithMockXrpLedgerAPIService(std::string serverAddress)
{
grpc::ServerBuilder builder;
builder.AddListeningPort(serverAddress, grpc::InsecureServerCredentials());
builder.AddListeningPort(serverAddress, grpc::InsecureServerCredentials(), &port_);
builder.RegisterService(&mockXrpLedgerAPIService);
server_ = builder.BuildAndStart();
serverThread_ = std::thread([this] { server_->Wait(); });
Expand All @@ -94,11 +94,17 @@ struct WithMockXrpLedgerAPIService : virtual ::testing::Test {
serverThread_.join();
}

int
getXRPLMockPort() const
{
return port_;
}
MockXrpLedgerAPIService mockXrpLedgerAPIService;

private:
std::unique_ptr<grpc::Server> server_;
std::thread serverThread_;
int port_{};
};

} // namespace tests::util
10 changes: 8 additions & 2 deletions tests/common/util/TestHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
}
11 changes: 9 additions & 2 deletions tests/common/util/TestHttpServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ class TestHttpServer {
*
* @param context boost::asio::io_context to use for networking
* @param host host to bind to
* @param port port to bind to
*/
TestHttpServer(boost::asio::io_context& context, std::string host, int port);
TestHttpServer(boost::asio::io_context& context, std::string host);

/**
* @brief Start the server
Expand All @@ -56,6 +55,14 @@ class TestHttpServer {
void
handleRequest(RequestHandler handler, bool allowToFail = false);

/**
* @brief Return the port HTTP server is connected to
*
* @return string port number
*/
std::string
port() const;

private:
boost::asio::ip::tcp::acceptor acceptor_;
};
10 changes: 8 additions & 2 deletions tests/common/util/TestWsServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,20 @@ TestWsConnection::headers() const
return headers_;
}

TestWsServer::TestWsServer(asio::io_context& context, std::string const& host, int port) : acceptor_(context)
TestWsServer::TestWsServer(asio::io_context& context, std::string const& host) : acceptor_(context)
{
auto endpoint = asio::ip::tcp::endpoint(boost::asio::ip::make_address(host), port);
auto endpoint = asio::ip::tcp::endpoint(boost::asio::ip::make_address(host), 0);
acceptor_.open(endpoint.protocol());
acceptor_.set_option(asio::socket_base::reuse_address(true));
acceptor_.bind(endpoint);
}

std::string
TestWsServer::port() const
{
return std::to_string(this->acceptor_.local_endpoint().port());
}

std::expected<TestWsConnection, util::requests::RequestError>
TestWsServer::acceptConnection(asio::yield_context yield)
{
Expand Down
5 changes: 4 additions & 1 deletion tests/common/util/TestWsServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ class TestWsServer {
boost::asio::ip::tcp::acceptor acceptor_;

public:
TestWsServer(boost::asio::io_context& context, std::string const& host, int port);
TestWsServer(boost::asio::io_context& context, std::string const& host);

std::string
port() const;

std::expected<TestWsConnection, util::requests::RequestError>
acceptConnection(boost::asio::yield_context yield);
Expand Down
9 changes: 7 additions & 2 deletions tests/unit/etl/ForwardingSourceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,13 @@
using namespace etl::impl;

struct ForwardingSourceTests : SyncAsioContextTest {
TestWsServer server_{ctx, "0.0.0.0", 11114};
ForwardingSource forwardingSource{"127.0.0.1", "11114", std::chrono::milliseconds{1}, std::chrono::milliseconds{1}};
TestWsServer server_{ctx, "0.0.0.0"};
ForwardingSource forwardingSource{
"127.0.0.1",
server_.port(),
std::chrono::milliseconds{1},
std::chrono::milliseconds{1}
};
};

TEST_F(ForwardingSourceTests, ConnectionFailed)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/etl/GrpcSourceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ using namespace etl::impl;

struct GrpcSourceTests : NoLoggerFixture, util::prometheus::WithPrometheus, tests::util::WithMockXrpLedgerAPIService {
GrpcSourceTests()
: WithMockXrpLedgerAPIService("localhost:55051")
: WithMockXrpLedgerAPIService("localhost:0")
, mockBackend_(std::make_shared<testing::StrictMock<MockBackend>>(util::Config{}))
, grpcSource_("127.0.0.1", "55051", mockBackend_)
, grpcSource_("127.0.0.1", std::to_string(getXRPLMockPort()), mockBackend_)
{
}

Expand Down
7 changes: 4 additions & 3 deletions tests/unit/etl/SubscriptionSourceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include "etl/impl/SubscriptionSource.hpp"
#include "util/AssignRandomPort.hpp"
#include "util/Fixtures.hpp"
#include "util/MockNetworkValidatedLedgers.hpp"
#include "util/MockSubscriptionManager.hpp"
Expand All @@ -31,6 +32,7 @@
#include <gtest/gtest.h>

#include <chrono>
#include <cstdint>
#include <optional>
#include <string>
#include <utility>
Expand All @@ -46,8 +48,7 @@ struct SubscriptionSourceConnectionTests : public NoLoggerFixture {
}

boost::asio::io_context ioContext_;

TestWsServer wsServer_{ioContext_, "0.0.0.0", 11113};
TestWsServer wsServer_{ioContext_, "0.0.0.0"};

StrictMockNetworkValidatedLedgersPtr networkValidatedLedgers_;
StrictMockSubscriptionManagerSharedPtr subscriptionManager_;
Expand All @@ -59,7 +60,7 @@ struct SubscriptionSourceConnectionTests : public NoLoggerFixture {
SubscriptionSource subscriptionSource_{
ioContext_,
"127.0.0.1",
"11113",
wsServer_.port(),
networkValidatedLedgers_,
subscriptionManager_,
onConnectHook_.AsStdFunction(),
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/util/requests/RequestBuilderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
//==============================================================================

#include "util/AssignRandomPort.hpp"
#include "util/Fixtures.hpp"
#include "util/TestHttpServer.hpp"
#include "util/requests/RequestBuilder.hpp"
Expand All @@ -31,6 +32,7 @@
#include <gtest/gtest.h>

#include <chrono>
#include <cstdint>
#include <expected>
#include <optional>
#include <string>
Expand All @@ -50,8 +52,8 @@ struct RequestBuilderTestBundle {
};

struct RequestBuilderTestBase : SyncAsioContextTest {
TestHttpServer server{ctx, "0.0.0.0", 11111};
RequestBuilder builder{"localhost", "11111"};
TestHttpServer server{ctx, "0.0.0.0"};
RequestBuilder builder{"localhost", server.port()};
};

struct RequestBuilderTest : RequestBuilderTestBase, testing::WithParamInterface<RequestBuilderTestBundle> {};
Expand Down Expand Up @@ -182,7 +184,7 @@ TEST_F(RequestBuilderTest, ResolveError)

TEST_F(RequestBuilderTest, ConnectionError)
{
builder = RequestBuilder{"localhost", "11112"};
builder = RequestBuilder{"localhost", std::to_string(tests::util::generateFreePort())};
builder.setTimeout(std::chrono::milliseconds{1});
runSpawn([this](asio::yield_context yield) {
auto const response = builder.getPlain(yield);
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/util/requests/WsConnectionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
//==============================================================================

#include "util/AssignRandomPort.hpp"
#include "util/Fixtures.hpp"
#include "util/TestWsServer.hpp"
#include "util/requests/Types.hpp"
Expand All @@ -29,6 +30,7 @@

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <expected>
#include <memory>
#include <optional>
Expand All @@ -41,8 +43,8 @@ namespace asio = boost::asio;
namespace http = boost::beast::http;

struct WsConnectionTestsBase : SyncAsioContextTest {
WsConnectionBuilder builder{"localhost", "11112"};
TestWsServer server{ctx, "0.0.0.0", 11112};
TestWsServer server{ctx, "0.0.0.0"};
WsConnectionBuilder builder{"localhost", server.port()};

template <typename T, typename E>
T
Expand Down
Loading

0 comments on commit e135aa4

Please sign in to comment.