William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 1 | #include <cerrno> |
| 2 | #include <functional> |
| 3 | #include <gmock/gmock.h> |
| 4 | #include <gtest/gtest.h> |
| 5 | #include <memory> |
| 6 | #include <sdeventplus/event.hpp> |
| 7 | #include <sdeventplus/exception.hpp> |
| 8 | #include <sdeventplus/source/event.hpp> |
| 9 | #include <sdeventplus/test/sdevent.hpp> |
| 10 | #include <systemd/sd-event.h> |
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 | |
| 14 | namespace sdeventplus |
| 15 | { |
| 16 | namespace source |
| 17 | { |
| 18 | namespace |
| 19 | { |
| 20 | |
| 21 | using testing::DoAll; |
| 22 | using testing::Return; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 23 | using testing::ReturnPointee; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 24 | using testing::SaveArg; |
| 25 | using testing::SetArgPointee; |
| 26 | |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 27 | using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 28 | |
| 29 | class EventTest : public testing::Test |
| 30 | { |
| 31 | protected: |
| 32 | testing::StrictMock<test::SdEventMock> mock; |
| 33 | sd_event_source* const expected_source = |
| 34 | reinterpret_cast<sd_event_source*>(1234); |
| 35 | sd_event* const expected_event = reinterpret_cast<sd_event*>(2345); |
| 36 | UniqueEvent event = make_event(expected_event); |
| 37 | |
| 38 | UniqueEvent make_event(sd_event* event) |
| 39 | { |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 40 | auto deleter = [this, event](Event* e) { |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 41 | EXPECT_CALL(this->mock, sd_event_unref(event)) |
| 42 | .WillOnce(Return(nullptr)); |
| 43 | delete e; |
| 44 | }; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 45 | return UniqueEvent(new Event(event, std::false_type(), &mock), deleter); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void expect_destruct() |
| 49 | { |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 50 | EXPECT_CALL(mock, sd_event_source_unref(expected_source)) |
| 51 | .WillOnce(Return(nullptr)); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 52 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 53 | .WillOnce(Return(nullptr)); |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | TEST_F(EventTest, DeferConstruct) |
| 58 | { |
| 59 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 60 | .WillOnce(Return(expected_event)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 61 | sd_event_destroy_t destroy; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 62 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 63 | { |
| 64 | testing::InSequence seq; |
| 65 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 66 | testing::_)) |
| 67 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 68 | EXPECT_CALL(mock, |
| 69 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 70 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 71 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 72 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 73 | } |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 74 | sd_event_handler_t handler; |
| 75 | EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_, |
| 76 | nullptr)) |
| 77 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler), |
| 78 | Return(0))); |
| 79 | int completions = 0; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 80 | EventBase::Callback callback = [&completions](EventBase&) { |
| 81 | completions++; |
| 82 | }; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 83 | Defer defer(*event, std::move(callback)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 84 | EXPECT_NE(&defer, userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 85 | EXPECT_FALSE(callback); |
| 86 | EXPECT_EQ(0, completions); |
| 87 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 88 | EXPECT_EQ(0, handler(nullptr, userdata)); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 89 | EXPECT_EQ(1, completions); |
| 90 | |
William A. Kennington III | b53ef90 | 2018-10-29 20:06:45 -0700 | [diff] [blame] | 91 | defer.set_callback(std::bind([]() {})); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 92 | EXPECT_EQ(0, handler(nullptr, userdata)); |
William A. Kennington III | b53ef90 | 2018-10-29 20:06:45 -0700 | [diff] [blame] | 93 | EXPECT_EQ(1, completions); |
| 94 | |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 95 | expect_destruct(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 96 | destroy(userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | TEST_F(EventTest, PostConstruct) |
| 100 | { |
| 101 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 102 | .WillOnce(Return(expected_event)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 103 | sd_event_destroy_t destroy; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 104 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 105 | { |
| 106 | testing::InSequence seq; |
| 107 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 108 | testing::_)) |
| 109 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 110 | EXPECT_CALL(mock, |
| 111 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 112 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 113 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 114 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 115 | } |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 116 | sd_event_handler_t handler; |
| 117 | EXPECT_CALL(mock, sd_event_add_post(expected_event, testing::_, testing::_, |
| 118 | nullptr)) |
| 119 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler), |
| 120 | Return(0))); |
| 121 | int completions = 0; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 122 | EventBase::Callback callback = [&completions](EventBase&) { |
| 123 | completions++; |
| 124 | }; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 125 | Post post(*event, std::move(callback)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 126 | EXPECT_NE(&post, userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 127 | EXPECT_FALSE(callback); |
| 128 | EXPECT_EQ(0, completions); |
| 129 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 130 | EXPECT_EQ(0, handler(nullptr, userdata)); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 131 | EXPECT_EQ(1, completions); |
| 132 | |
| 133 | expect_destruct(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 134 | destroy(userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | TEST_F(EventTest, ExitConstruct) |
| 138 | { |
| 139 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 140 | .WillOnce(Return(expected_event)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 141 | sd_event_destroy_t destroy; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 142 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 143 | { |
| 144 | testing::InSequence seq; |
| 145 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 146 | testing::_)) |
| 147 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 148 | EXPECT_CALL(mock, |
| 149 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 150 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 151 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 152 | .WillRepeatedly(ReturnPointee(&userdata)); |
| 153 | } |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 154 | sd_event_handler_t handler; |
| 155 | EXPECT_CALL(mock, sd_event_add_exit(expected_event, testing::_, testing::_, |
| 156 | nullptr)) |
| 157 | .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler), |
| 158 | Return(0))); |
| 159 | int completions = 0; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 160 | EventBase::Callback callback = [&completions](EventBase&) { |
| 161 | completions++; |
| 162 | }; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 163 | Exit exit(*event, std::move(callback)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 164 | EXPECT_NE(&exit, userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 165 | EXPECT_FALSE(callback); |
| 166 | EXPECT_EQ(0, completions); |
| 167 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 168 | EXPECT_EQ(0, handler(nullptr, userdata)); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 169 | EXPECT_EQ(1, completions); |
| 170 | |
| 171 | expect_destruct(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame^] | 172 | destroy(userdata); |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | TEST_F(EventTest, ConstructFailure) |
| 176 | { |
| 177 | EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_, |
| 178 | nullptr)) |
| 179 | .WillOnce(Return(-EINVAL)); |
| 180 | int completions = 0; |
William A. Kennington III | 86be5dd | 2018-07-23 09:46:18 -0700 | [diff] [blame] | 181 | EventBase::Callback callback = [&completions](EventBase&) { |
| 182 | completions++; |
| 183 | }; |
William A. Kennington III | 5a7365c | 2018-07-22 00:22:08 -0700 | [diff] [blame] | 184 | EXPECT_THROW(Defer(*event, std::move(callback)), SdEventError); |
| 185 | EXPECT_TRUE(callback); |
| 186 | EXPECT_EQ(0, completions); |
| 187 | } |
| 188 | |
| 189 | } // namespace |
| 190 | } // namespace source |
| 191 | } // namespace sdeventplus |