William A. Kennington III | a4c76ea | 2022-11-21 16:20:41 -0800 | [diff] [blame] | 1 | #include <sdeventplus/source/event.hpp> |
| 2 | #include <sdeventplus/utility/sdbus.hpp> |
| 3 | |
Patrick Williams | a8c11e3 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 4 | #include <gtest/gtest.h> |
| 5 | |
William A. Kennington III | a4c76ea | 2022-11-21 16:20:41 -0800 | [diff] [blame] | 6 | namespace sdeventplus::utility |
| 7 | { |
| 8 | |
| 9 | struct LoopWithBus : testing::Test |
| 10 | { |
| 11 | Event event = Event::get_new(); |
| 12 | sdbusplus::bus_t bus = sdbusplus::bus::new_bus(); |
| 13 | }; |
| 14 | |
| 15 | TEST_F(LoopWithBus, ImmediateExit) |
| 16 | { |
| 17 | event.exit(0); |
| 18 | EXPECT_EQ(0, loopWithBus(event, bus)); |
| 19 | } |
| 20 | |
| 21 | TEST_F(LoopWithBus, DelayedExit) |
| 22 | { |
| 23 | source::Defer(event, [](source::EventBase& b) { |
| 24 | b.get_event().exit(1); |
| 25 | }).set_floating(true); |
| 26 | EXPECT_EQ(1, loopWithBus(event, bus)); |
| 27 | } |
| 28 | |
| 29 | TEST_F(LoopWithBus, ExitSources) |
| 30 | { |
| 31 | int d1 = 0, d2 = 0; |
| 32 | source::Exit(event, [&](source::EventBase&) { d1 = 1; }).set_floating(true); |
| 33 | source::Defer(event, [&](source::EventBase& b) { |
| 34 | source::Exit(event, [&](source::EventBase&) { |
| 35 | d2 = 2; |
| 36 | }).set_floating(true); |
| 37 | b.get_event().exit(3); |
| 38 | }).set_floating(true); |
| 39 | EXPECT_EQ(3, loopWithBus(event, bus)); |
| 40 | EXPECT_EQ(1, d1); |
| 41 | EXPECT_EQ(2, d2); |
| 42 | } |
| 43 | |
| 44 | } // namespace sdeventplus::utility |