Patrick Williams | a8c11e3 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 1 | #include <systemd/sd-event.h> |
| 2 | |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 3 | #include <sdeventplus/event.hpp> |
| 4 | #include <sdeventplus/exception.hpp> |
| 5 | #include <sdeventplus/source/event.hpp> |
| 6 | #include <sdeventplus/test/sdevent.hpp> |
Patrick Williams | a8c11e3 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 7 | |
| 8 | #include <cerrno> |
| 9 | #include <functional> |
| 10 | #include <memory> |
William A. Kennington III | ebcb4f1 | 2018-07-22 16:51:36 -0700 | [diff] [blame] | 11 | #include <type_traits> |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 12 | #include <utility> |
| 13 | |
Patrick Williams | a8c11e3 | 2023-05-10 07:50:56 -0500 | [diff] [blame] | 14 | #include <gmock/gmock.h> |
| 15 | #include <gtest/gtest.h> |
| 16 | |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 17 | namespace sdeventplus |
| 18 | { |
| 19 | namespace source |
| 20 | { |
| 21 | namespace |
| 22 | { |
| 23 | |
| 24 | using testing::DoAll; |
| 25 | using testing::Return; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 26 | using testing::ReturnPointee; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 27 | using testing::SaveArg; |
| 28 | using testing::SetArgPointee; |
| 29 | |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 30 | using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 31 | |
| 32 | class EventTest : public testing::Test |
| 33 | { |
| 34 | protected: |
| 35 | testing::StrictMock<test::SdEventMock> mock; |
| 36 | sd_event_source* const expected_source = |
| 37 | reinterpret_cast<sd_event_source*>(1234); |
| 38 | sd_event* const expected_event = reinterpret_cast<sd_event*>(2345); |
| 39 | UniqueEvent event = make_event(expected_event); |
| 40 | |
| 41 | UniqueEvent make_event(sd_event* event) |
| 42 | { |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 43 | auto deleter = [this, event](Event* e) { |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 44 | EXPECT_CALL(this->mock, sd_event_unref(event)) |
| 45 | .WillOnce(Return(nullptr)); |
| 46 | delete e; |
| 47 | }; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 48 | return UniqueEvent(new Event(event, std::false_type(), &mock), deleter); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void expect_destruct() |
| 52 | { |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 53 | EXPECT_CALL(mock, sd_event_source_unref(expected_source)) |
| 54 | .WillOnce(Return(nullptr)); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 55 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 56 | .WillOnce(Return(nullptr)); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | TEST_F(EventTest, DeferConstruct) |
| 61 | { |
| 62 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 63 | .WillOnce(Return(expected_event)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 64 | sd_event_destroy_t destroy; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 65 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 66 | { |
| 67 | testing::InSequence seq; |
| 68 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 69 | testing::_)) |
| 70 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 71 | EXPECT_CALL(mock, |
| 72 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 73 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 74 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 75 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 76 | } |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 77 | sd_event_handler_t handler; |
| 78 | EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_, |
| 79 | nullptr)) |
| 80 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler), |
| 81 | Return(0))); |
| 82 | int completions = 0; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 83 | EventBase::Callback callback = [&completions](EventBase&) { |
| 84 | completions++; |
| 85 | }; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 86 | Defer defer(*event, std::move(callback)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 87 | EXPECT_NE(&defer, userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 88 | EXPECT_FALSE(callback); |
| 89 | EXPECT_EQ(0, completions); |
| 90 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 91 | EXPECT_EQ(0, handler(nullptr, userdata)); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 92 | EXPECT_EQ(1, completions); |
| 93 | |
William A. Kennington III | b53ef90 | 2018-10-29 20:06:45 -0700 | [diff] [blame] | 94 | defer.set_callback(std::bind([]() {})); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 95 | EXPECT_EQ(0, handler(nullptr, userdata)); |
William A. Kennington III | b53ef90 | 2018-10-29 20:06:45 -0700 | [diff] [blame] | 96 | EXPECT_EQ(1, completions); |
| 97 | |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 98 | expect_destruct(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 99 | destroy(userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST_F(EventTest, PostConstruct) |
| 103 | { |
| 104 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 105 | .WillOnce(Return(expected_event)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 106 | sd_event_destroy_t destroy; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 107 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 108 | { |
| 109 | testing::InSequence seq; |
| 110 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 111 | testing::_)) |
| 112 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 113 | EXPECT_CALL(mock, |
| 114 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 115 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 116 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 117 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 118 | } |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 119 | sd_event_handler_t handler; |
| 120 | EXPECT_CALL(mock, sd_event_add_post(expected_event, testing::_, testing::_, |
| 121 | nullptr)) |
| 122 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler), |
| 123 | Return(0))); |
| 124 | int completions = 0; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 125 | EventBase::Callback callback = [&completions](EventBase&) { |
| 126 | completions++; |
| 127 | }; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 128 | Post post(*event, std::move(callback)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 129 | EXPECT_NE(&post, userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 130 | EXPECT_FALSE(callback); |
| 131 | EXPECT_EQ(0, completions); |
| 132 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 133 | EXPECT_EQ(0, handler(nullptr, userdata)); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 134 | EXPECT_EQ(1, completions); |
| 135 | |
| 136 | expect_destruct(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 137 | destroy(userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | TEST_F(EventTest, ExitConstruct) |
| 141 | { |
| 142 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 143 | .WillOnce(Return(expected_event)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 144 | sd_event_destroy_t destroy; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 145 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 146 | { |
| 147 | testing::InSequence seq; |
| 148 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 149 | testing::_)) |
| 150 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 151 | EXPECT_CALL(mock, |
| 152 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 153 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 154 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 155 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 156 | } |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 157 | sd_event_handler_t handler; |
| 158 | EXPECT_CALL(mock, sd_event_add_exit(expected_event, testing::_, testing::_, |
| 159 | nullptr)) |
| 160 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler), |
| 161 | Return(0))); |
| 162 | int completions = 0; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 163 | EventBase::Callback callback = [&completions](EventBase&) { |
| 164 | completions++; |
| 165 | }; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 166 | Exit exit(*event, std::move(callback)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 167 | EXPECT_NE(&exit, userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 168 | EXPECT_FALSE(callback); |
| 169 | EXPECT_EQ(0, completions); |
| 170 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 171 | EXPECT_EQ(0, handler(nullptr, userdata)); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 172 | EXPECT_EQ(1, completions); |
| 173 | |
| 174 | expect_destruct(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 175 | destroy(userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | TEST_F(EventTest, ConstructFailure) |
| 179 | { |
| 180 | EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_, |
| 181 | nullptr)) |
| 182 | .WillOnce(Return(-EINVAL)); |
| 183 | int completions = 0; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 184 | EventBase::Callback callback = [&completions](EventBase&) { |
| 185 | completions++; |
| 186 | }; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 187 | EXPECT_THROW(Defer(*event, std::move(callback)), SdEventError); |
| 188 | EXPECT_TRUE(callback); |
| 189 | EXPECT_EQ(0, completions); |
| 190 | } |
| 191 | |
William A. Kennington III | 56dc78b | 2019-03-29 21:09:13 -0700 | [diff] [blame] | 192 | TEST_F(EventTest, CopyConstruct) |
| 193 | { |
| 194 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 195 | .WillOnce(Return(expected_event)); |
| 196 | sd_event_destroy_t destroy; |
| 197 | void* userdata; |
| 198 | { |
| 199 | testing::InSequence seq; |
| 200 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 201 | testing::_)) |
| 202 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 203 | EXPECT_CALL(mock, |
| 204 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 205 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 206 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 207 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 208 | } |
| 209 | sd_event_handler_t handler; |
| 210 | EXPECT_CALL(mock, sd_event_add_exit(expected_event, testing::_, testing::_, |
| 211 | nullptr)) |
| 212 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler), |
| 213 | Return(0))); |
| 214 | auto exit = std::make_unique<Exit>(*event, [](EventBase&) {}); |
| 215 | |
| 216 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 217 | .WillOnce(Return(expected_event)); |
| 218 | EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| 219 | .WillOnce(Return(expected_source)); |
| 220 | auto exit2 = std::make_unique<Exit>(*exit); |
| 221 | { |
| 222 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 223 | .WillOnce(Return(expected_event)); |
| 224 | EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| 225 | .WillOnce(Return(expected_source)); |
| 226 | Exit exit3(*exit); |
| 227 | |
| 228 | expect_destruct(); |
| 229 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 230 | .WillOnce(Return(expected_event)); |
| 231 | EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| 232 | .WillOnce(Return(expected_source)); |
| 233 | *exit2 = exit3; |
| 234 | |
| 235 | expect_destruct(); |
| 236 | } |
| 237 | |
| 238 | // Delete the original exit |
| 239 | expect_destruct(); |
| 240 | exit.reset(); |
| 241 | |
| 242 | // Make sure our new copy can still access data |
| 243 | exit2->set_callback(nullptr); |
| 244 | expect_destruct(); |
| 245 | exit2.reset(); |
| 246 | destroy(userdata); |
| 247 | } |
| 248 | |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 249 | } // namespace |
| 250 | } // namespace source |
| 251 | } // namespace sdeventplus |