mirror of
https://github.com/XRPLF/clio.git
synced 2026-08-22 21:20:52 +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:
@@ -99,8 +99,9 @@ protected:
|
||||
TEST_F(WebWsConnectionTests, WasUpgraded)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().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) {
|
||||
auto wsConnection = acceptConnection(yield);
|
||||
@@ -115,8 +116,9 @@ TEST_F(WebWsConnectionTests, DisconnectClientOnInactivity)
|
||||
std::thread clientThread{[&clientCtx]() { clientCtx.run(); }};
|
||||
|
||||
util::spawn(clientCtx, [&work, this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
boost::asio::steady_timer timer{yield.get_executor(), std::chrono::milliseconds{5}};
|
||||
timer.async_wait(yield);
|
||||
work.reset();
|
||||
@@ -143,8 +145,9 @@ TEST_F(WebWsConnectionTests, Send)
|
||||
Response const response{boost::beast::http::status::ok, "some response", request_};
|
||||
|
||||
util::spawn(ctx_, [this, &response](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
auto const expectedMessage = wsClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedMessage.has_value()) << expectedMessage.error().message(); }();
|
||||
EXPECT_EQ(expectedMessage.value(), response.message());
|
||||
@@ -152,8 +155,8 @@ TEST_F(WebWsConnectionTests, Send)
|
||||
|
||||
runSpawn([this, &response](boost::asio::yield_context yield) {
|
||||
auto wsConnection = acceptConnection(yield);
|
||||
auto maybeError = wsConnection->send(response, yield);
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess = wsConnection->send(response, yield);
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -162,8 +165,9 @@ TEST_F(WebWsConnectionTests, SendShared)
|
||||
auto const response = std::make_shared<std::string>("some response");
|
||||
|
||||
util::spawn(ctx_, [this, &response](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
auto const expectedMessage = wsClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedMessage.has_value()) << expectedMessage.error().message(); }();
|
||||
EXPECT_EQ(expectedMessage.value(), *response);
|
||||
@@ -171,8 +175,8 @@ TEST_F(WebWsConnectionTests, SendShared)
|
||||
|
||||
runSpawn([this, &response](boost::asio::yield_context yield) {
|
||||
auto wsConnection = acceptConnection(yield);
|
||||
auto maybeError = wsConnection->sendShared(response, yield);
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess = wsConnection->sendShared(response, yield);
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -181,8 +185,9 @@ TEST_F(WebWsConnectionTests, MultipleSend)
|
||||
Response const response{boost::beast::http::status::ok, "some response", request_};
|
||||
|
||||
util::spawn(ctx_, [this, &response](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.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 expectedMessage = wsClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
@@ -195,8 +200,8 @@ TEST_F(WebWsConnectionTests, MultipleSend)
|
||||
auto wsConnection = acceptConnection(yield);
|
||||
|
||||
for ([[maybe_unused]] auto i : std::ranges::iota_view{0, 3}) {
|
||||
auto maybeError = wsConnection->send(response, yield);
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess = wsConnection->send(response, yield);
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -206,8 +211,9 @@ TEST_F(WebWsConnectionTests, MultipleSendFromMultipleCoroutines)
|
||||
Response const response{boost::beast::http::status::ok, "some response", request_};
|
||||
|
||||
util::spawn(ctx_, [this, &response](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.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 expectedMessage = wsClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
@@ -222,8 +228,8 @@ TEST_F(WebWsConnectionTests, MultipleSendFromMultipleCoroutines)
|
||||
util::CoroutineGroup group{yield};
|
||||
for ([[maybe_unused]] auto i : std::ranges::iota_view{0, 3}) {
|
||||
group.spawn(yield, [&wsConnection, &response](boost::asio::yield_context innerYield) {
|
||||
auto maybeError = wsConnection->send(response, innerYield);
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess = wsConnection->send(response, innerYield);
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
}
|
||||
group.asyncWait(yield);
|
||||
@@ -235,21 +241,22 @@ TEST_F(WebWsConnectionTests, SendFailed)
|
||||
Response const response{boost::beast::http::status::ok, "some response", request_};
|
||||
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
wsClient_.close();
|
||||
});
|
||||
|
||||
runSpawn([this, &response](boost::asio::yield_context yield) {
|
||||
auto wsConnection = acceptConnection(yield);
|
||||
wsConnection->setTimeout(std::chrono::milliseconds{1});
|
||||
std::optional<Error> maybeError;
|
||||
std::expected<void, Error> expectedSuccess;
|
||||
size_t counter = 0;
|
||||
while (not maybeError.has_value() and counter < 100) {
|
||||
maybeError = wsConnection->send(response, yield);
|
||||
while (expectedSuccess.has_value() and counter < 100) {
|
||||
expectedSuccess = wsConnection->send(response, yield);
|
||||
++counter;
|
||||
}
|
||||
EXPECT_TRUE(maybeError.has_value());
|
||||
EXPECT_FALSE(expectedSuccess.has_value());
|
||||
EXPECT_LT(counter, 100);
|
||||
});
|
||||
}
|
||||
@@ -259,8 +266,9 @@ TEST_F(WebWsConnectionTests, SendFailedSendingFromMultipleCoroutines)
|
||||
Response const response{boost::beast::http::status::ok, "some response", request_};
|
||||
|
||||
util::spawn(ctx_, [this, &response](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
|
||||
auto const expectedMessage = wsClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedMessage.has_value()) << expectedMessage.error().message(); }();
|
||||
@@ -271,15 +279,15 @@ TEST_F(WebWsConnectionTests, SendFailedSendingFromMultipleCoroutines)
|
||||
runSpawn([this, &response](boost::asio::yield_context yield) {
|
||||
auto wsConnection = acceptConnection(yield);
|
||||
wsConnection->setTimeout(std::chrono::milliseconds{1});
|
||||
std::optional<Error> maybeError;
|
||||
std::expected<void, Error> expectedSuccess;
|
||||
size_t counter = 0;
|
||||
while (not maybeError.has_value() and counter < 100) {
|
||||
maybeError = wsConnection->send(response, yield);
|
||||
while (expectedSuccess.has_value() and counter < 100) {
|
||||
expectedSuccess = wsConnection->send(response, yield);
|
||||
++counter;
|
||||
}
|
||||
// Sending after getting an error should be safe
|
||||
maybeError = wsConnection->send(response, yield);
|
||||
EXPECT_TRUE(maybeError.has_value());
|
||||
expectedSuccess = wsConnection->send(response, yield);
|
||||
EXPECT_FALSE(expectedSuccess.has_value());
|
||||
EXPECT_LT(counter, 100);
|
||||
});
|
||||
}
|
||||
@@ -287,11 +295,12 @@ TEST_F(WebWsConnectionTests, SendFailedSendingFromMultipleCoroutines)
|
||||
TEST_F(WebWsConnectionTests, Receive)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
|
||||
maybeError = wsClient_.send(yield, request_.message(), std::chrono::milliseconds{100});
|
||||
EXPECT_FALSE(maybeError.has_value()) << maybeError->message();
|
||||
expectedSuccess = wsClient_.send(yield, request_.message(), std::chrono::milliseconds{100});
|
||||
EXPECT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message();
|
||||
});
|
||||
|
||||
runSpawn([this](boost::asio::yield_context yield) {
|
||||
@@ -306,12 +315,13 @@ TEST_F(WebWsConnectionTests, Receive)
|
||||
TEST_F(WebWsConnectionTests, MultipleReceive)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.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}) {
|
||||
maybeError = wsClient_.send(yield, request_.message(), std::chrono::milliseconds{100});
|
||||
EXPECT_FALSE(maybeError.has_value()) << maybeError->message();
|
||||
expectedSuccess = wsClient_.send(yield, request_.message(), std::chrono::milliseconds{100});
|
||||
EXPECT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -329,8 +339,9 @@ TEST_F(WebWsConnectionTests, MultipleReceive)
|
||||
TEST_F(WebWsConnectionTests, ReceiveTimeout)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().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) {
|
||||
@@ -345,8 +356,9 @@ TEST_F(WebWsConnectionTests, ReceiveTimeout)
|
||||
TEST_F(WebWsConnectionTests, ReceiveFailed)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
wsClient_.close();
|
||||
});
|
||||
|
||||
@@ -361,8 +373,9 @@ TEST_F(WebWsConnectionTests, ReceiveFailed)
|
||||
TEST_F(WebWsConnectionTests, Close)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
auto const maybeMessage = wsClient_.receive(yield, std::chrono::milliseconds{100});
|
||||
EXPECT_FALSE(maybeMessage.has_value());
|
||||
EXPECT_THAT(maybeMessage.error().message(), testing::HasSubstr("was gracefully closed"));
|
||||
@@ -377,8 +390,9 @@ TEST_F(WebWsConnectionTests, Close)
|
||||
TEST_F(WebWsConnectionTests, CloseWhenConnectionIsAlreadyClosed)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
wsClient_.close();
|
||||
});
|
||||
|
||||
@@ -393,8 +407,9 @@ TEST_F(WebWsConnectionTests, CloseWhenConnectionIsAlreadyClosed)
|
||||
TEST_F(WebWsConnectionTests, CloseCalledFromMultipleSubCoroutines)
|
||||
{
|
||||
util::spawn(ctx_, [this](boost::asio::yield_context yield) {
|
||||
auto maybeError = wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_FALSE(maybeError.has_value()) << maybeError.value().message(); }();
|
||||
auto expectedSuccess =
|
||||
wsClient_.connect("localhost", httpServer_.port(), yield, std::chrono::milliseconds{100});
|
||||
[&]() { ASSERT_TRUE(expectedSuccess.has_value()) << expectedSuccess.error().message(); }();
|
||||
});
|
||||
|
||||
testing::StrictMock<testing::MockFunction<void()>> closeCalled;
|
||||
|
||||
Reference in New Issue
Block a user