feat: Move/copy support in async framework (#1609)

Fixes #1608
This commit is contained in:
Alex Kremer
2024-08-20 13:24:51 +01:00
committed by GitHub
parent fb473f6d28
commit 9a9de501e4
18 changed files with 290 additions and 54 deletions

View File

@@ -50,32 +50,32 @@ struct MockExecutionContext {
template <typename T>
using ScheduledOperation = MockScheduledOperation<T>;
MOCK_METHOD(Operation<std::any> const&, execute, (std::function<std::any()>), (const));
MOCK_METHOD(Operation<std::any> const&, execute, (std::function<std::any()>), ());
MOCK_METHOD(
Operation<std::any> const&,
execute,
(std::function<std::any()>, std::optional<std::chrono::milliseconds>),
(const)
()
);
MOCK_METHOD(
StoppableOperation<std::any> const&,
execute,
(std::function<std::any(util::async::AnyStopToken)>, std::optional<std::chrono::milliseconds>),
(const)
()
);
MOCK_METHOD(
ScheduledOperation<std::any> const&,
scheduleAfter,
(std::chrono::milliseconds, std::function<std::any(util::async::AnyStopToken)>),
(const)
()
);
MOCK_METHOD(
ScheduledOperation<std::any> const&,
scheduleAfter,
(std::chrono::milliseconds, std::function<std::any(util::async::AnyStopToken, bool)>),
(const)
()
);
MOCK_METHOD(MockStrand const&, makeStrand, (), (const));
MOCK_METHOD(MockStrand const&, makeStrand, (), ());
MOCK_METHOD(void, stop, (), (const));
MOCK_METHOD(void, join, (), (const));
MOCK_METHOD(void, join, (), ());
};

View File

@@ -24,6 +24,12 @@
#include "util/async/AnyOperation.hpp"
#include "util/async/AnyStopToken.hpp"
#include "util/async/AnyStrand.hpp"
#include "util/async/Concepts.hpp"
#include "util/async/Operation.hpp"
#include "util/async/Outcome.hpp"
#include "util/async/context/SyncExecutionContext.hpp"
#include "util/async/context/impl/Cancellation.hpp"
#include "util/async/impl/ErasedOperation.hpp"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -31,11 +37,72 @@
#include <any>
#include <chrono>
#include <functional>
#include <optional>
#include <type_traits>
#include <utility>
using namespace util::async;
using namespace ::testing;
static_assert(SomeStoppable<StoppableOperation<int, impl::BasicStopSource>>);
static_assert(not SomeStoppable<Operation<int>>);
static_assert(SomeCancellable<ScheduledOperation<SyncExecutionContext, int>>);
static_assert(not SomeCancellable<int>);
static_assert(SomeOperation<Operation<int>>);
static_assert(not SomeOperation<int>);
static_assert(SomeStoppableOperation<MockStoppableOperation<int>>);
static_assert(not SomeStoppableOperation<MockOperation<int>>);
static_assert(SomeCancellableOperation<MockScheduledOperation<int>>);
static_assert(not SomeCancellableOperation<MockOperation<int>>);
static_assert(SomeOutcome<Outcome<int>>);
static_assert(not SomeOutcome<int>);
static_assert(SomeStopToken<impl::BasicStopSource::Token>);
static_assert(not SomeStopToken<int>);
static_assert(SomeYieldStopSource<impl::YieldContextStopSource>);
static_assert(not SomeYieldStopSource<int>);
static_assert(not SomeYieldStopSource<impl::BasicStopSource>);
static_assert(SomeSimpleStopSource<impl::BasicStopSource>);
static_assert(not SomeSimpleStopSource<int>);
static_assert(SomeStopSource<impl::BasicStopSource>);
static_assert(not SomeStopSource<int>);
static_assert(SomeStopSourceProvider<StoppableOutcome<int, impl::BasicStopSource>>);
static_assert(not SomeStopSourceProvider<Outcome<int>>);
static_assert(SomeStoppableOutcome<StoppableOutcome<int, impl::BasicStopSource>>);
static_assert(not SomeStoppableOutcome<Outcome<int>>);
static_assert(SomeHandlerWithoutStopToken<decltype([]() {})>);
static_assert(not SomeHandlerWithoutStopToken<decltype([](int) {})>);
static_assert(SomeHandlerWith<decltype([](int) {}), int>);
static_assert(not SomeHandlerWith<decltype([](int) {}), std::optional<float>>);
static_assert(SomeStdDuration<std::chrono::steady_clock::duration>);
static_assert(not SomeStdDuration<std::chrono::steady_clock::time_point>);
static_assert(SomeStdOptional<std::optional<int>>);
static_assert(not SomeStdOptional<int>);
static_assert(SomeOptStdDuration<std::optional<std::chrono::steady_clock::duration>>);
static_assert(not SomeOptStdDuration<std::chrono::duration<double>>);
static_assert(not SomeOptStdDuration<std::optional<int>>);
static_assert(NotSameAs<int, impl::ErasedOperation>);
static_assert(not NotSameAs<impl::ErasedOperation, impl::ErasedOperation>);
static_assert(RValueNotSameAs<int&&, impl::ErasedOperation>);
static_assert(not RValueNotSameAs<int&, impl::ErasedOperation>);
struct AnyExecutionContextTests : Test {
using StrandType = NiceMock<MockStrand>;
@@ -52,6 +119,26 @@ struct AnyExecutionContextTests : Test {
AnyExecutionContext ctx{static_cast<MockExecutionContext&>(mockExecutionContext)};
};
TEST_F(AnyExecutionContextTests, Move)
{
auto mockOp = OperationType<std::any>{};
EXPECT_CALL(mockExecutionContext, execute(An<std::function<std::any()>>())).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockOp, get());
auto mineNow = std::move(ctx);
ASSERT_TRUE(mineNow.execute([] { throw 0; }).get());
}
TEST_F(AnyExecutionContextTests, CopyIsRefCounted)
{
auto mockOp = OperationType<std::any>{};
EXPECT_CALL(mockExecutionContext, execute(An<std::function<std::any()>>())).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockOp, get());
auto yoink = ctx;
ASSERT_TRUE(yoink.execute([] { throw 0; }).get());
}
TEST_F(AnyExecutionContextTests, ExecuteWithoutTokenAndVoid)
{
auto mockOp = OperationType<std::any>{};

View File

@@ -27,6 +27,7 @@
#include <any>
#include <expected>
#include <string>
#include <utility>
using namespace util::async;
using namespace ::testing;
@@ -47,6 +48,14 @@ struct AnyOperationTests : Test {
};
using AnyOperationDeathTest = AnyOperationTests;
TEST_F(AnyOperationTests, Move)
{
EXPECT_CALL(mockOp, get()).WillOnce(Return(std::any{}));
auto yoink = std::move(voidOp);
auto res = yoink.get();
ASSERT_TRUE(res);
}
TEST_F(AnyOperationTests, VoidDataYieldsNoError)
{
EXPECT_CALL(mockOp, get()).WillOnce(Return(std::any{}));

View File

@@ -31,6 +31,7 @@
#include <expected>
#include <functional>
#include <type_traits>
#include <utility>
using namespace util::async;
using namespace ::testing;
@@ -46,6 +47,25 @@ struct AnyStrandTests : ::testing::Test {
AnyStrand strand{static_cast<MockStrand&>(mockStrand)};
};
TEST_F(AnyStrandTests, Move)
{
auto mockOp = OperationType<std::any>{};
EXPECT_CALL(mockStrand, execute(An<std::function<std::any()>>())).WillOnce(ReturnRef(mockOp));
EXPECT_CALL(mockOp, get());
auto mineNow = std::move(strand);
ASSERT_TRUE(mineNow.execute([] { throw 0; }).get());
}
TEST_F(AnyStrandTests, CopyIsRefCounted)
{
auto mockOp = OperationType<std::any>{};
EXPECT_CALL(mockStrand, execute(An<std::function<std::any()>>())).WillOnce(ReturnRef(mockOp));
auto yoink = strand;
ASSERT_TRUE(yoink.execute([] { throw 0; }).get());
}
TEST_F(AnyStrandTests, ExecuteWithoutTokenAndVoid)
{
auto mockOp = OperationType<std::any>{};

View File

@@ -40,6 +40,12 @@ struct ExecutionContextTests : public ::testing::Test {
TYPED_TEST_CASE(ExecutionContextTests, ExecutionContextTypes);
TYPED_TEST(ExecutionContextTests, move)
{
auto mineNow = std::move(this->ctx);
EXPECT_TRUE(mineNow.execute([] { return true; }).get().value());
}
TYPED_TEST(ExecutionContextTests, execute)
{
auto res = this->ctx.execute([]() { return 42; });
@@ -161,6 +167,15 @@ TYPED_TEST(ExecutionContextTests, timerUnknownException)
EXPECT_TRUE(std::string{err}.ends_with("unknown"));
}
TYPED_TEST(ExecutionContextTests, strandMove)
{
auto strand = this->ctx.makeStrand();
auto yoink = std::move(strand);
auto res = yoink.execute([] { return 42; });
EXPECT_EQ(res.get().value(), 42);
}
TYPED_TEST(ExecutionContextTests, strand)
{
auto strand = this->ctx.makeStrand();