blob: 9dea0169141094f1a668f6ba70383dffbd5dd9e9 [file] [log] [blame]
Patrick Williams73e278b2022-09-16 08:31:36 -05001#include <sdbusplus/async.hpp>
2
3#include <gtest/gtest.h>
4
Patrick Williams78e436f2022-09-21 10:06:20 -05005struct Context : public testing::Test
Patrick Williams73e278b2022-09-16 08:31:36 -05006{
Patrick Williams78e436f2022-09-21 10:06:20 -05007 ~Context() noexcept = default;
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 Williams1b7b54c2022-09-21 10:49:45 -050016 void runToStop()
17 {
18 ctx->run(std::execution::just() |
19 std::execution::then([this]() { ctx->request_stop(); }));
20 }
21
Patrick Williams78e436f2022-09-21 10:06:20 -050022 std::optional<sdbusplus::async::context> ctx{std::in_place};
23};
24
25TEST_F(Context, RunSimple)
26{
Patrick Williams1b7b54c2022-09-21 10:49:45 -050027 runToStop();
Patrick Williams73e278b2022-09-16 08:31:36 -050028}
Patrick Williams0139cac2022-09-20 17:04:17 -050029
Patrick Williams78e436f2022-09-21 10:06:20 -050030TEST_F(Context, SpawnedTask)
Patrick Williams0139cac2022-09-20 17:04:17 -050031{
Patrick Williams78e436f2022-09-21 10:06:20 -050032 ctx->spawn(std::execution::just());
Patrick Williams1b7b54c2022-09-21 10:49:45 -050033 runToStop();
Patrick Williams78e436f2022-09-21 10:06:20 -050034}
Patrick Williams0139cac2022-09-20 17:04:17 -050035
Patrick Williams78e436f2022-09-21 10:06:20 -050036TEST_F(Context, SpawnDelayedTask)
37{
38 using namespace std::literals;
39 static constexpr auto timeout = 500ms;
40
41 auto start = std::chrono::steady_clock::now();
42
43 bool ran = false;
44 ctx->spawn(sdbusplus::async::sleep_for(*ctx, timeout) |
45 std::execution::then([&ran]() { ran = true; }));
46
Patrick Williams1b7b54c2022-09-21 10:49:45 -050047 runToStop();
Patrick Williams78e436f2022-09-21 10:06:20 -050048
49 auto stop = std::chrono::steady_clock::now();
50
51 EXPECT_TRUE(ran);
52 EXPECT_GT(stop - start, timeout);
53 EXPECT_LT(stop - start, timeout * 2);
Patrick Williams0139cac2022-09-20 17:04:17 -050054}
Patrick Williams1b7b54c2022-09-21 10:49:45 -050055
56TEST_F(Context, DestructMatcherWithPendingAwait)
57{
58 using namespace std::literals;
59
60 bool ran = false;
61 auto m = std::make_optional<sdbusplus::async::match>(
62 *ctx, sdbusplus::bus::match::rules::interfacesAdded(
63 "/this/is/a/bogus/path/for/SpawnMatcher"));
64
65 // Await the match completion (which will never happen).
66 ctx->spawn(m->next() | std::execution::then([&ran](...) { ran = true; }));
67
68 // Destruct the match.
69 ctx->spawn(sdbusplus::async::sleep_for(*ctx, 1ms) |
70 std::execution::then([&m](...) { m.reset(); }));
71
Patrick Williams4cfc2842022-09-22 09:53:33 -050072 EXPECT_THROW(runToStop(), sdbusplus::exception::UnhandledStop);
Patrick Williams1b7b54c2022-09-21 10:49:45 -050073 EXPECT_FALSE(ran);
74}
75
76TEST_F(Context, DestructMatcherWithPendingAwaitAsTask)
77{
78 using namespace std::literals;
79
80 auto m = std::make_optional<sdbusplus::async::match>(
81 *ctx, sdbusplus::bus::match::rules::interfacesAdded(
82 "/this/is/a/bogus/path/for/SpawnMatcher"));
83
84 struct _
85 {
86 static auto fn(decltype(m->next()) snd, bool& ran)
87 -> sdbusplus::async::task<>
88 {
89 co_await std::move(snd);
90 ran = true;
91 co_return;
92 }
93 };
94
95 bool ran = false;
96 ctx->spawn(_::fn(m->next(), ran));
97 ctx->spawn(sdbusplus::async::sleep_for(*ctx, 1ms) |
98 std::execution::then([&]() { m.reset(); }));
99
Patrick Williams4cfc2842022-09-22 09:53:33 -0500100 EXPECT_THROW(runToStop(), sdbusplus::exception::UnhandledStop);
Patrick Williams1b7b54c2022-09-21 10:49:45 -0500101 EXPECT_FALSE(ran);
102}