Patrick Williams | d2b0044 | 2022-09-16 08:28:07 -0500 | [diff] [blame] | 1 | #include <sdbusplus/event.hpp> |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <thread> |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | using namespace std::literals::chrono_literals; |
| 9 | |
| 10 | struct Event : public testing::Test |
| 11 | { |
| 12 | sdbusplus::event_t ev{}; |
| 13 | }; |
| 14 | |
| 15 | TEST_F(Event, TimeoutWorks) |
| 16 | { |
| 17 | static constexpr auto timeout = 250ms; |
| 18 | |
| 19 | auto start = std::chrono::steady_clock::now(); |
| 20 | ev.run_one(timeout); |
| 21 | auto stop = std::chrono::steady_clock::now(); |
| 22 | |
| 23 | EXPECT_TRUE(stop - start > timeout); |
| 24 | EXPECT_TRUE(stop - start < timeout * 2); |
| 25 | } |
| 26 | |
| 27 | TEST_F(Event, Runnable) |
| 28 | { |
| 29 | static constexpr auto timeout = 10s; |
| 30 | |
| 31 | std::jthread j{[&]() { ev.break_run(); }}; |
| 32 | |
| 33 | auto start = std::chrono::steady_clock::now(); |
| 34 | ev.run_one(timeout); |
| 35 | auto stop = std::chrono::steady_clock::now(); |
| 36 | |
| 37 | EXPECT_TRUE(stop - start < timeout); |
| 38 | } |
| 39 | |
| 40 | TEST_F(Event, ConditionSignals) |
| 41 | { |
| 42 | struct run |
| 43 | { |
| 44 | static int _(sd_event_source*, int, uint32_t, void* data) |
| 45 | { |
| 46 | *static_cast<bool*>(data) = true; |
| 47 | return 0; |
| 48 | } |
| 49 | }; |
| 50 | bool ran = false; |
| 51 | |
| 52 | auto c = ev.add_condition(run::_, &ran); |
| 53 | std::jthread j{[&]() { c.signal(); }}; |
| 54 | |
| 55 | ev.run_one(); |
| 56 | EXPECT_TRUE(ran); |
| 57 | c.ack(); |
| 58 | } |