Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 1 | #include <sdbusplus/async.hpp> |
| 2 | |
| 3 | #include <gtest/gtest.h> |
| 4 | |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 5 | struct Context : public testing::Test |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 6 | { |
Ed Tanous | 9688ed6 | 2023-01-06 13:17:25 -0800 | [diff] [blame] | 7 | ~Context() noexcept override = default; |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 8 | |
| 9 | void TearDown() override |
| 10 | { |
| 11 | // Destructing the context can throw, so we have to do it in |
| 12 | // the TearDown in order to make our destructor noexcept. |
| 13 | ctx.reset(); |
| 14 | } |
| 15 | |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 16 | void spawnStop() |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 17 | { |
Patrick Williams | 5e7ef08 | 2023-01-05 11:08:53 -0600 | [diff] [blame] | 18 | ctx->spawn(stdexec::just() | |
| 19 | stdexec::then([this]() { ctx->request_stop(); })); |
Patrick Williams | 115e9b3 | 2022-09-23 16:20:59 -0500 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | void runToStop() |
| 23 | { |
| 24 | spawnStop(); |
Patrick Williams | 3c242ba | 2022-09-23 09:51:55 -0500 | [diff] [blame] | 25 | ctx->run(); |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 26 | } |
| 27 | |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 28 | std::optional<sdbusplus::async::context> ctx{std::in_place}; |
| 29 | }; |
| 30 | |
| 31 | TEST_F(Context, RunSimple) |
| 32 | { |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 33 | runToStop(); |
Patrick Williams | 73e278b | 2022-09-16 08:31:36 -0500 | [diff] [blame] | 34 | } |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 35 | |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 36 | TEST_F(Context, SpawnedTask) |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 37 | { |
Patrick Williams | 5e7ef08 | 2023-01-05 11:08:53 -0600 | [diff] [blame] | 38 | ctx->spawn(stdexec::just()); |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 39 | runToStop(); |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 40 | } |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 41 | |
Patrick Williams | 10483c9 | 2022-09-23 11:33:39 -0500 | [diff] [blame] | 42 | TEST_F(Context, ReentrantRun) |
| 43 | { |
| 44 | runToStop(); |
| 45 | for (int i = 0; i < 100; ++i) |
| 46 | { |
| 47 | ctx->run(); |
| 48 | } |
| 49 | } |
| 50 | |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 51 | TEST_F(Context, SpawnDelayedTask) |
| 52 | { |
| 53 | using namespace std::literals; |
| 54 | static constexpr auto timeout = 500ms; |
| 55 | |
| 56 | auto start = std::chrono::steady_clock::now(); |
| 57 | |
| 58 | bool ran = false; |
| 59 | ctx->spawn(sdbusplus::async::sleep_for(*ctx, timeout) | |
Patrick Williams | 5e7ef08 | 2023-01-05 11:08:53 -0600 | [diff] [blame] | 60 | stdexec::then([&ran]() { ran = true; })); |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 61 | |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 62 | runToStop(); |
Patrick Williams | 78e436f | 2022-09-21 10:06:20 -0500 | [diff] [blame] | 63 | |
| 64 | auto stop = std::chrono::steady_clock::now(); |
| 65 | |
| 66 | EXPECT_TRUE(ran); |
| 67 | EXPECT_GT(stop - start, timeout); |
Patrick Williams | 6ea246a | 2022-10-14 07:58:15 -0500 | [diff] [blame] | 68 | EXPECT_LT(stop - start, timeout * 3); |
Patrick Williams | 0139cac | 2022-09-20 17:04:17 -0500 | [diff] [blame] | 69 | } |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 70 | |
Patrick Williams | d6b05cc | 2022-09-23 09:38:38 -0500 | [diff] [blame] | 71 | TEST_F(Context, SpawnRecursiveTask) |
| 72 | { |
| 73 | struct _ |
| 74 | { |
| 75 | static auto one(size_t count, size_t& executed) |
| 76 | -> sdbusplus::async::task<size_t> |
| 77 | { |
| 78 | if (count) |
| 79 | { |
| 80 | ++executed; |
| 81 | co_return (co_await one(count - 1, executed)) + 1; |
| 82 | } |
Patrick Williams | 5e7ef08 | 2023-01-05 11:08:53 -0600 | [diff] [blame] | 83 | co_return co_await stdexec::just(0); |
Patrick Williams | d6b05cc | 2022-09-23 09:38:38 -0500 | [diff] [blame] | 84 | } |
| 85 | }; |
| 86 | |
| 87 | static constexpr size_t count = 100; |
| 88 | size_t executed = 0; |
| 89 | |
Patrick Williams | 5e7ef08 | 2023-01-05 11:08:53 -0600 | [diff] [blame] | 90 | ctx->spawn(_::one(count, executed) | |
| 91 | stdexec::then([=](auto result) { EXPECT_EQ(result, count); })); |
Patrick Williams | d6b05cc | 2022-09-23 09:38:38 -0500 | [diff] [blame] | 92 | |
| 93 | runToStop(); |
| 94 | |
| 95 | EXPECT_EQ(executed, count); |
| 96 | } |
| 97 | |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 98 | TEST_F(Context, DestructMatcherWithPendingAwait) |
| 99 | { |
| 100 | using namespace std::literals; |
| 101 | |
| 102 | bool ran = false; |
| 103 | auto m = std::make_optional<sdbusplus::async::match>( |
| 104 | *ctx, sdbusplus::bus::match::rules::interfacesAdded( |
| 105 | "/this/is/a/bogus/path/for/SpawnMatcher")); |
| 106 | |
| 107 | // Await the match completion (which will never happen). |
Patrick Williams | 5e7ef08 | 2023-01-05 11:08:53 -0600 | [diff] [blame] | 108 | ctx->spawn(m->next() | stdexec::then([&ran](...) { ran = true; })); |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 109 | |
| 110 | // Destruct the match. |
| 111 | ctx->spawn(sdbusplus::async::sleep_for(*ctx, 1ms) | |
Patrick Williams | 5e7ef08 | 2023-01-05 11:08:53 -0600 | [diff] [blame] | 112 | stdexec::then([&m](...) { m.reset(); })); |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 113 | |
Patrick Williams | 97c31c8 | 2023-05-25 11:04:46 -0500 | [diff] [blame] | 114 | runToStop(); |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 115 | EXPECT_FALSE(ran); |
| 116 | } |
| 117 | |
| 118 | TEST_F(Context, DestructMatcherWithPendingAwaitAsTask) |
| 119 | { |
| 120 | using namespace std::literals; |
| 121 | |
| 122 | auto m = std::make_optional<sdbusplus::async::match>( |
| 123 | *ctx, sdbusplus::bus::match::rules::interfacesAdded( |
| 124 | "/this/is/a/bogus/path/for/SpawnMatcher")); |
| 125 | |
| 126 | struct _ |
| 127 | { |
| 128 | static auto fn(decltype(m->next()) snd, bool& ran) |
| 129 | -> sdbusplus::async::task<> |
| 130 | { |
| 131 | co_await std::move(snd); |
| 132 | ran = true; |
| 133 | co_return; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | bool ran = false; |
| 138 | ctx->spawn(_::fn(m->next(), ran)); |
| 139 | ctx->spawn(sdbusplus::async::sleep_for(*ctx, 1ms) | |
Patrick Williams | 5e7ef08 | 2023-01-05 11:08:53 -0600 | [diff] [blame] | 140 | stdexec::then([&]() { m.reset(); })); |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 141 | |
Patrick Williams | 97c31c8 | 2023-05-25 11:04:46 -0500 | [diff] [blame] | 142 | runToStop(); |
Patrick Williams | 1b7b54c | 2022-09-21 10:49:45 -0500 | [diff] [blame] | 143 | EXPECT_FALSE(ran); |
| 144 | } |