test: async: add a few recursive task tests
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I37b231e75b55c851c37600367da7ce1bc75e60af
diff --git a/test/async/context.cpp b/test/async/context.cpp
index 9dea016..39ee28d 100644
--- a/test/async/context.cpp
+++ b/test/async/context.cpp
@@ -53,6 +53,34 @@
EXPECT_LT(stop - start, timeout * 2);
}
+TEST_F(Context, SpawnRecursiveTask)
+{
+ struct _
+ {
+ static auto one(size_t count, size_t& executed)
+ -> sdbusplus::async::task<size_t>
+ {
+ if (count)
+ {
+ ++executed;
+ co_return (co_await one(count - 1, executed)) + 1;
+ }
+ co_return co_await std::execution::just(0);
+ }
+ };
+
+ static constexpr size_t count = 100;
+ size_t executed = 0;
+
+ ctx->spawn(_::one(count, executed) | std::execution::then([=](auto result) {
+ EXPECT_EQ(result, count);
+ }));
+
+ runToStop();
+
+ EXPECT_EQ(executed, count);
+}
+
TEST_F(Context, DestructMatcherWithPendingAwait)
{
using namespace std::literals;
diff --git a/test/async/task.cpp b/test/async/task.cpp
index 34d9e65..6ee90f8 100644
--- a/test/async/task.cpp
+++ b/test/async/task.cpp
@@ -76,3 +76,28 @@
std::this_thread::sync_wait(_::two(caught));
EXPECT_TRUE(caught);
}
+
+TEST(Task, RecursiveTask)
+{
+ struct _
+ {
+ static auto one(size_t count, size_t& executed) -> task<size_t>
+ {
+ if (count)
+ {
+ ++executed;
+ co_return (co_await one(count - 1, executed)) + 1;
+ }
+ co_return co_await std::execution::just(0);
+ }
+ };
+
+ static constexpr size_t count = 100;
+ size_t executed = 0;
+
+ std::this_thread::sync_wait(
+ _::one(count, executed) |
+ std::execution::then([=](auto result) { EXPECT_EQ(result, count); }));
+
+ EXPECT_EQ(executed, count);
+}