blob: 4092de5784996bb972a2ecb0e8ec9507982a0a81 [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
16 std::optional<sdbusplus::async::context> ctx{std::in_place};
17};
18
19TEST_F(Context, RunSimple)
20{
21 ctx->run(std::execution::just() |
22 std::execution::then([this]() { ctx->request_stop(); }));
Patrick Williams73e278b2022-09-16 08:31:36 -050023}
Patrick Williams0139cac2022-09-20 17:04:17 -050024
Patrick Williams78e436f2022-09-21 10:06:20 -050025TEST_F(Context, SpawnedTask)
Patrick Williams0139cac2022-09-20 17:04:17 -050026{
Patrick Williams78e436f2022-09-21 10:06:20 -050027 ctx->spawn(std::execution::just());
Patrick Williams0139cac2022-09-20 17:04:17 -050028
Patrick Williams78e436f2022-09-21 10:06:20 -050029 ctx->run(std::execution::just() |
30 std::execution::then([this]() { ctx->request_stop(); }));
31}
Patrick Williams0139cac2022-09-20 17:04:17 -050032
Patrick Williams78e436f2022-09-21 10:06:20 -050033TEST_F(Context, SpawnDelayedTask)
34{
35 using namespace std::literals;
36 static constexpr auto timeout = 500ms;
37
38 auto start = std::chrono::steady_clock::now();
39
40 bool ran = false;
41 ctx->spawn(sdbusplus::async::sleep_for(*ctx, timeout) |
42 std::execution::then([&ran]() { ran = true; }));
43
44 ctx->run(std::execution::just() |
45 std::execution::then([this]() { ctx->request_stop(); }));
46
47 auto stop = std::chrono::steady_clock::now();
48
49 EXPECT_TRUE(ran);
50 EXPECT_GT(stop - start, timeout);
51 EXPECT_LT(stop - start, timeout * 2);
Patrick Williams0139cac2022-09-20 17:04:17 -050052}