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