mirror of
https://github.com/XRPLF/clio.git
synced 2025-11-27 23:25:53 +00:00
refactor: Use expected<void, error> instead of optional<error> (#2565)
Co-authored-by: Ayaz Salikhov <mathbunnyru@users.noreply.github.com>
This commit is contained in:
@@ -80,8 +80,9 @@ protected:
|
||||
TEST_F(HttpConnectionTests, wasUpgraded)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
|
||||
runSpawn([this](boost::asio::yield_context yield) {
|
||||
@@ -95,11 +96,12 @@ TEST_F(HttpConnectionTests, Receive)
|
||||
request_.set(boost::beast::http::field::user_agent, "test_client");
|
||||
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
|
||||
maybeError = httpClient_.send(request_, yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
expectedSuccess = httpClient_.send(request_, yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
|
||||
runSpawn([this](boost::asio::yield_context yield) {
|
||||
@@ -123,8 +125,9 @@ TEST_F(HttpConnectionTests, Receive)
|
||||
TEST_F(HttpConnectionTests, ReceiveTimeout)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{1});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{1});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
|
||||
runSpawn([this](boost::asio::yield_context yield) {
|
||||
@@ -138,8 +141,9 @@ TEST_F(HttpConnectionTests, ReceiveTimeout)
|
||||
TEST_F(HttpConnectionTests, ReceiveClientDisconnected)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{1});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{1});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
httpClient_.disconnect();
|
||||
});
|
||||
|
||||
@@ -157,11 +161,12 @@ TEST_F(HttpConnectionTests, Send)
|
||||
Response const response{http::status::ok, "some response data", request};
|
||||
|
||||
util::spawn(ctx_, [this, response = response](boost::asio::yield_context yield) mutable {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
|
||||
auto const expectedResponse = httpClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedResponse.has_value()) << maybeError->message(); }();
|
||||
[&]() { ASSERT_TRUE(expectedResponse.has_value()) << expectedResponse.error().message(); }();
|
||||
|
||||
auto const receivedResponse = expectedResponse.value();
|
||||
auto const sentResponse = std::move(response).intoHttpResponse();
|
||||
@@ -173,8 +178,8 @@ TEST_F(HttpConnectionTests, Send)
|
||||
|
||||
runSpawn([this, &response](boost::asio::yield_context yield) {
|
||||
auto connection = acceptConnection(yield);
|
||||
auto maybeError = connection->send(response, yield);
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess = connection->send(response, yield);
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -184,8 +189,9 @@ TEST_F(HttpConnectionTests, SendMultipleTimes)
|
||||
Response const response{http::status::ok, "some response data", request};
|
||||
|
||||
util::spawn(ctx_, [this, response = response](boost::asio::yield_context yield) mutable {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
|
||||
for ([[maybe_unused]] auto i : std::ranges::iota_view{0, 3}) {
|
||||
auto const expectedResponse = httpClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
@@ -204,8 +210,8 @@ TEST_F(HttpConnectionTests, SendMultipleTimes)
|
||||
auto connection = acceptConnection(yield);
|
||||
|
||||
for ([[maybe_unused]] auto i : std::ranges::iota_view{0, 3}) {
|
||||
auto maybeError = connection->send(response, yield);
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess = connection->send(response, yield);
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -216,9 +222,9 @@ TEST_F(HttpConnectionTests, SendMultipleTimesFromMultipleCoroutines)
|
||||
Response const response{http::status::ok, "some response data", request};
|
||||
|
||||
util::spawn(ctx_, [this, response = response](boost::asio::yield_context yield) mutable {
|
||||
auto const maybeError =
|
||||
auto const expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
|
||||
for ([[maybe_unused]] auto i : std::ranges::iota_view{0, 3}) {
|
||||
auto const expectedResponse = httpClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
@@ -239,8 +245,8 @@ TEST_F(HttpConnectionTests, SendMultipleTimesFromMultipleCoroutines)
|
||||
util::CoroutineGroup group{yield};
|
||||
for ([[maybe_unused]] auto i : std::ranges::iota_view{0, 3}) {
|
||||
group.spawn(yield, [&response, &connection](boost::asio::yield_context innerYield) {
|
||||
auto const maybeError = connection->send(response, innerYield);
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto const expectedSuccess = connection->send(response, innerYield);
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
}
|
||||
group.asyncWait(yield);
|
||||
@@ -251,9 +257,9 @@ TEST_F(HttpConnectionTests, SendMultipleTimesClientDisconnected)
|
||||
{
|
||||
Response const response{http::status::ok, "some response data", Request{request_}};
|
||||
util::spawn(ctx_, [this, response = response](boost::asio::yield_context yield) mutable {
|
||||
auto const maybeError =
|
||||
auto const expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{1});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
auto const expectedResponse = httpClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedResponse.has_value()) << expectedResponse.error().message(); }();
|
||||
httpClient_.disconnect();
|
||||
@@ -262,15 +268,15 @@ TEST_F(HttpConnectionTests, SendMultipleTimesClientDisconnected)
|
||||
runSpawn([this, &response](boost::asio::yield_context yield) {
|
||||
auto connection = acceptConnection(yield);
|
||||
connection->setTimeout(std::chrono::milliseconds{1});
|
||||
auto maybeError = connection->send(response, yield);
|
||||
auto expectedSuccess = connection->send(response, yield);
|
||||
size_t counter{1};
|
||||
while (not maybeError.has_value() and counter < 100) {
|
||||
while (expectedSuccess.has_value() and counter < 100) {
|
||||
++counter;
|
||||
maybeError = connection->send(response, yield);
|
||||
expectedSuccess = connection->send(response, yield);
|
||||
}
|
||||
// Sending after getting an error should be safe
|
||||
maybeError = connection->send(response, yield);
|
||||
EXPECT_TRUE(maybeError.has_value());
|
||||
expectedSuccess = connection->send(response, yield);
|
||||
EXPECT_FALSE(expectedSuccess.has_value());
|
||||
EXPECT_LT(counter, 100);
|
||||
});
|
||||
}
|
||||
@@ -279,20 +285,21 @@ TEST_F(HttpConnectionTests, SendClientDisconnected)
|
||||
{
|
||||
Response const response{http::status::ok, "some response data", Request{request_}};
|
||||
util::spawn(ctx_, [this, response = response](boost::asio::yield_context yield) mutable {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{1});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{1});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
httpClient_.disconnect();
|
||||
});
|
||||
runSpawn([this, &response](boost::asio::yield_context yield) {
|
||||
auto connection = acceptConnection(yield);
|
||||
connection->setTimeout(std::chrono::milliseconds{1});
|
||||
auto maybeError = connection->send(response, yield);
|
||||
auto expectedSuccess = connection->send(response, yield);
|
||||
size_t counter{1};
|
||||
while (not maybeError.has_value() and counter < 100) {
|
||||
while (expectedSuccess.has_value() and counter < 100) {
|
||||
++counter;
|
||||
maybeError = connection->send(response, yield);
|
||||
expectedSuccess = connection->send(response, yield);
|
||||
}
|
||||
EXPECT_TRUE(maybeError.has_value());
|
||||
EXPECT_FALSE(expectedSuccess.has_value());
|
||||
EXPECT_LT(counter, 100);
|
||||
});
|
||||
}
|
||||
@@ -300,15 +307,16 @@ TEST_F(HttpConnectionTests, SendClientDisconnected)
|
||||
TEST_F(HttpConnectionTests, Close)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
|
||||
size_t counter{0};
|
||||
while (not maybeError.has_value() and counter < 100) {
|
||||
while (expectedSuccess.has_value() and counter < 100) {
|
||||
++counter;
|
||||
maybeError = httpClient_.send(request_, yield, std::chrono::milliseconds{1});
|
||||
expectedSuccess = httpClient_.send(request_, yield, std::chrono::milliseconds{1});
|
||||
}
|
||||
EXPECT_TRUE(maybeError.has_value());
|
||||
EXPECT_FALSE(expectedSuccess.has_value());
|
||||
EXPECT_LT(counter, 100);
|
||||
});
|
||||
|
||||
@@ -322,11 +330,12 @@ TEST_F(HttpConnectionTests, Close)
|
||||
TEST_F(HttpConnectionTests, IsUpgradeRequested_GotHttpRequest)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
|
||||
maybeError = httpClient_.send(request_, yield, std::chrono::milliseconds{1});
|
||||
EXPECT_FALSE(maybeError.has_value()) << maybeError->message();
|
||||
expectedSuccess = httpClient_.send(request_, yield, std::chrono::milliseconds{1});
|
||||
EXPECT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message();
|
||||
});
|
||||
|
||||
runSpawn([this](boost::asio::yield_context yield) {
|
||||
@@ -340,8 +349,9 @@ TEST_F(HttpConnectionTests, IsUpgradeRequested_GotHttpRequest)
|
||||
TEST_F(HttpConnectionTests, IsUpgradeRequested_FailedToFetch)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
|
||||
runSpawn([this](boost::asio::yield_context yield) {
|
||||
@@ -357,8 +367,8 @@ TEST_F(HttpConnectionTests, Upgrade)
|
||||
WebSocketAsyncClient wsClient{ctx_};
|
||||
|
||||
util::spawn(ctx_, [this, &wsClient](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess = wsClient.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
|
||||
runSpawn([this](boost::asio::yield_context yield) {
|
||||
@@ -375,8 +385,9 @@ TEST_F(HttpConnectionTests, Upgrade)
|
||||
TEST_F(HttpConnectionTests, Ip)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) mutable {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
|
||||
runSpawn([this](boost::asio::yield_context yield) {
|
||||
@@ -391,8 +402,9 @@ TEST_F(HttpConnectionTests, isAdminSetAdmin)
|
||||
EXPECT_CALL(adminSetter, Call).WillOnce(testing::Return(true));
|
||||
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) mutable {
|
||||
auto maybeError = httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError->message(); }();
|
||||
auto expectedSuccess =
|
||||
httpClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
|
||||
runSpawn([&](boost::asio::yield_context yield) {
|
||||
|
||||
Reference in New Issue
Block a user