//------------------------------------------------------------------------------ /* 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/MockOperation.hpp" #include "util/MockStrand.hpp" #include "util/async/AnyOperation.hpp" #include "util/async/AnyStopToken.hpp" #include "util/async/AnyStrand.hpp" #include #include #include #include #include #include #include using namespace util::async; using namespace ::testing; struct AnyStrandTests : ::testing::Test { template using OperationType = ::testing::NiceMock>; template using StoppableOperationType = ::testing::NiceMock>; ::testing::NaggyMock mockStrand; AnyStrand strand{static_cast(mockStrand)}; }; TEST_F(AnyStrandTests, ExecuteWithoutTokenAndVoid) { auto mockOp = OperationType{}; EXPECT_CALL(mockStrand, execute(An>())).WillOnce(ReturnRef(mockOp)); auto op = strand.execute([] {}); static_assert(std::is_same_v>); ASSERT_TRUE(op.get()); } TEST_F(AnyStrandTests, ExecuteWithoutTokenAndVoidThrowsException) { auto mockOp = OperationType{}; EXPECT_CALL(mockStrand, execute(An>())) .WillOnce([](auto&&) -> OperationType const& { throw 0; }); EXPECT_ANY_THROW([[maybe_unused]] auto unused = strand.execute([] {})); } TEST_F(AnyStrandTests, ExecuteWithStopTokenAndVoid) { auto mockOp = StoppableOperationType{}; EXPECT_CALL(mockStrand, execute(An>(), _)).WillOnce(ReturnRef(mockOp)); auto op = strand.execute([](auto) {}); static_assert(std::is_same_v>); ASSERT_TRUE(op.get()); } TEST_F(AnyStrandTests, ExecuteWithStopTokenAndVoidThrowsException) { EXPECT_CALL(mockStrand, execute(An>(), _)) .WillOnce([](auto&&, auto) -> StoppableOperationType const& { throw 0; }); EXPECT_ANY_THROW([[maybe_unused]] auto unused = strand.execute([](auto) {})); } TEST_F(AnyStrandTests, ExecuteWithStopTokenAndReturnValue) { auto mockOp = StoppableOperationType{}; EXPECT_CALL(mockOp, get()).WillOnce(Return(std::make_any(42))); EXPECT_CALL(mockStrand, execute(An>(), _)).WillOnce(ReturnRef(mockOp)); auto op = strand.execute([](auto) { return 42; }); static_assert(std::is_same_v>); ASSERT_EQ(op.get().value(), 42); } TEST_F(AnyStrandTests, ExecuteWithStopTokenAndReturnValueThrowsException) { EXPECT_CALL(mockStrand, execute(An>(), _)) .WillOnce([](auto&&, auto) -> StoppableOperationType const& { throw 0; }); EXPECT_ANY_THROW([[maybe_unused]] auto unused = strand.execute([](auto) { return 42; })); } TEST_F(AnyStrandTests, ExecuteWithTimeoutAndStopTokenAndReturnValue) { auto mockOp = StoppableOperationType{}; EXPECT_CALL(mockOp, get()).WillOnce(Return(std::make_any(42))); EXPECT_CALL(mockStrand, execute(An>(), _)).WillOnce(ReturnRef(mockOp)); auto op = strand.execute([](auto) { return 42; }, std::chrono::milliseconds{1}); static_assert(std::is_same_v>); ASSERT_EQ(op.get().value(), 42); } TEST_F(AnyStrandTests, ExecuteWithTimoutAndStopTokenAndReturnValueThrowsException) { EXPECT_CALL(mockStrand, execute(An>(), _)) .WillOnce([](auto&&, auto) -> StoppableOperationType const& { throw 0; }); EXPECT_ANY_THROW( [[maybe_unused]] auto unused = strand.execute([](auto) { return 42; }, std::chrono::milliseconds{1}) ); }