William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 1 | #include <gmock/gmock.h> |
| 2 | #include <gtest/gtest.h> |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 3 | #include <memory> |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 4 | #include <sdeventplus/event.hpp> |
| 5 | #include <sdeventplus/exception.hpp> |
| 6 | #include <sdeventplus/test/sdevent.hpp> |
| 7 | #include <type_traits> |
| 8 | |
| 9 | namespace sdeventplus |
| 10 | { |
| 11 | namespace |
| 12 | { |
| 13 | |
| 14 | using testing::DoAll; |
| 15 | using testing::Return; |
| 16 | using testing::SetArgPointee; |
| 17 | |
| 18 | class EventTest : public testing::Test |
| 19 | { |
| 20 | protected: |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 21 | testing::StrictMock<test::SdEventMock> mock; |
| 22 | sd_event* const expected_event = reinterpret_cast<sd_event*>(1234); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | TEST_F(EventTest, NewEventRef) |
| 26 | { |
| 27 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 28 | .WillOnce(Return(expected_event)); |
| 29 | Event event(expected_event, &mock); |
William A. Kennington III | bdc5901 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 30 | EXPECT_EQ(&mock, event.getSdEvent()); |
| 31 | EXPECT_EQ(expected_event, event.get()); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 32 | |
| 33 | EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr)); |
| 34 | } |
| 35 | |
| 36 | TEST_F(EventTest, NewEventNoRef) |
| 37 | { |
| 38 | Event event(expected_event, std::false_type(), &mock); |
William A. Kennington III | bdc5901 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 39 | EXPECT_EQ(&mock, event.getSdEvent()); |
| 40 | EXPECT_EQ(expected_event, event.get()); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 41 | |
| 42 | EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr)); |
| 43 | } |
| 44 | |
| 45 | TEST_F(EventTest, GetNewEvent) |
| 46 | { |
| 47 | EXPECT_CALL(mock, sd_event_new(testing::_)) |
| 48 | .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0))); |
| 49 | Event event = Event::get_new(&mock); |
William A. Kennington III | bdc5901 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 50 | EXPECT_EQ(&mock, event.getSdEvent()); |
| 51 | EXPECT_EQ(expected_event, event.get()); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 52 | |
| 53 | EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr)); |
| 54 | } |
| 55 | |
| 56 | TEST_F(EventTest, GetNewEventFail) |
| 57 | { |
| 58 | EXPECT_CALL(mock, sd_event_new(testing::_)).WillOnce(Return(-EINVAL)); |
| 59 | EXPECT_THROW(Event::get_new(&mock), SdEventError); |
| 60 | } |
| 61 | |
| 62 | TEST_F(EventTest, GetDefaultEvent) |
| 63 | { |
| 64 | EXPECT_CALL(mock, sd_event_default(testing::_)) |
| 65 | .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0))); |
| 66 | Event event = Event::get_default(&mock); |
William A. Kennington III | bdc5901 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 67 | EXPECT_EQ(&mock, event.getSdEvent()); |
| 68 | EXPECT_EQ(expected_event, event.get()); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 69 | |
| 70 | EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr)); |
| 71 | } |
| 72 | |
| 73 | TEST_F(EventTest, GetDefaultEventFail) |
| 74 | { |
| 75 | EXPECT_CALL(mock, sd_event_default(testing::_)).WillOnce(Return(-EINVAL)); |
| 76 | EXPECT_THROW(Event::get_default(&mock), SdEventError); |
| 77 | } |
| 78 | |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 79 | class EventMethodTest : public EventTest |
| 80 | { |
| 81 | protected: |
| 82 | std::unique_ptr<Event> event; |
| 83 | |
| 84 | void SetUp() |
| 85 | { |
| 86 | event = |
| 87 | std::make_unique<Event>(expected_event, std::false_type(), &mock); |
| 88 | } |
| 89 | |
| 90 | void TearDown() |
| 91 | { |
| 92 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 93 | .WillOnce(Return(nullptr)); |
| 94 | } |
| 95 | }; |
| 96 | |
William A. Kennington III | fe05991 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 97 | TEST_F(EventMethodTest, PrepareSuccessNone) |
| 98 | { |
| 99 | EXPECT_CALL(mock, sd_event_prepare(expected_event)).WillOnce(Return(0)); |
| 100 | EXPECT_EQ(0, event->prepare()); |
| 101 | } |
| 102 | |
| 103 | TEST_F(EventMethodTest, PrepareSuccessReady) |
| 104 | { |
| 105 | const int events_ready = 10; |
| 106 | EXPECT_CALL(mock, sd_event_prepare(expected_event)) |
| 107 | .WillOnce(Return(events_ready)); |
| 108 | EXPECT_EQ(events_ready, event->prepare()); |
| 109 | } |
| 110 | |
| 111 | TEST_F(EventMethodTest, PrepareInternalError) |
| 112 | { |
| 113 | EXPECT_CALL(mock, sd_event_prepare(expected_event)) |
| 114 | .WillOnce(Return(-EINVAL)); |
| 115 | EXPECT_THROW(event->prepare(), SdEventError); |
| 116 | } |
| 117 | |
| 118 | TEST_F(EventMethodTest, WaitSuccessNone) |
| 119 | { |
| 120 | const std::chrono::microseconds timeout{20}; |
| 121 | EXPECT_CALL(mock, sd_event_wait(expected_event, timeout.count())) |
| 122 | .WillOnce(Return(0)); |
| 123 | EXPECT_EQ(0, event->wait(timeout)); |
| 124 | } |
| 125 | |
| 126 | TEST_F(EventMethodTest, WaitSuccessReady) |
| 127 | { |
| 128 | const int events_ready = 10; |
| 129 | EXPECT_CALL(mock, sd_event_wait(expected_event, static_cast<uint64_t>(-1))) |
| 130 | .WillOnce(Return(events_ready)); |
| 131 | EXPECT_EQ(events_ready, event->wait(std::experimental::nullopt)); |
| 132 | } |
| 133 | |
| 134 | TEST_F(EventMethodTest, WaitInternalError) |
| 135 | { |
| 136 | EXPECT_CALL(mock, sd_event_wait(expected_event, static_cast<uint64_t>(-1))) |
| 137 | .WillOnce(Return(-EINVAL)); |
| 138 | EXPECT_THROW(event->wait(std::experimental::nullopt), SdEventError); |
| 139 | } |
| 140 | |
| 141 | TEST_F(EventMethodTest, DispatchInitial) |
| 142 | { |
| 143 | EXPECT_CALL(mock, sd_event_dispatch(expected_event)).WillOnce(Return(0)); |
| 144 | EXPECT_EQ(0, event->dispatch()); |
| 145 | } |
| 146 | |
| 147 | TEST_F(EventMethodTest, DispatchDone) |
| 148 | { |
| 149 | const int done_code = 10; |
| 150 | EXPECT_CALL(mock, sd_event_dispatch(expected_event)) |
| 151 | .WillOnce(Return(done_code)); |
| 152 | EXPECT_EQ(done_code, event->dispatch()); |
| 153 | } |
| 154 | |
| 155 | TEST_F(EventMethodTest, DispatchInternalError) |
| 156 | { |
| 157 | EXPECT_CALL(mock, sd_event_dispatch(expected_event)) |
| 158 | .WillOnce(Return(-EINVAL)); |
| 159 | EXPECT_THROW(event->dispatch(), SdEventError); |
| 160 | } |
| 161 | |
| 162 | TEST_F(EventMethodTest, RunSuccessNone) |
| 163 | { |
| 164 | const std::chrono::microseconds timeout{20}; |
| 165 | EXPECT_CALL(mock, sd_event_run(expected_event, timeout.count())) |
| 166 | .WillOnce(Return(0)); |
| 167 | EXPECT_EQ(0, event->run(timeout)); |
| 168 | } |
| 169 | |
| 170 | TEST_F(EventMethodTest, RunSuccessReady) |
| 171 | { |
| 172 | const int events_ready = 10; |
| 173 | EXPECT_CALL(mock, sd_event_run(expected_event, static_cast<uint64_t>(-1))) |
| 174 | .WillOnce(Return(events_ready)); |
| 175 | EXPECT_EQ(events_ready, event->run(std::experimental::nullopt)); |
| 176 | } |
| 177 | |
| 178 | TEST_F(EventMethodTest, RunInternalError) |
| 179 | { |
| 180 | EXPECT_CALL(mock, sd_event_run(expected_event, static_cast<uint64_t>(-1))) |
| 181 | .WillOnce(Return(-EINVAL)); |
| 182 | EXPECT_THROW(event->run(std::experimental::nullopt), SdEventError); |
| 183 | } |
| 184 | |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 185 | TEST_F(EventMethodTest, LoopSuccess) |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 186 | { |
| 187 | EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(0)); |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 188 | EXPECT_EQ(0, event->loop()); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 189 | } |
| 190 | |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 191 | TEST_F(EventMethodTest, LoopUserError) |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 192 | { |
| 193 | const int user_error = 10; |
| 194 | EXPECT_CALL(mock, sd_event_loop(expected_event)) |
| 195 | .WillOnce(Return(user_error)); |
William A. Kennington III | 6364dd9 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 196 | EXPECT_EQ(user_error, event->loop()); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 197 | } |
| 198 | |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 199 | TEST_F(EventMethodTest, LoopInternalError) |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 200 | { |
| 201 | EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(-EINVAL)); |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 202 | EXPECT_THROW(event->loop(), SdEventError); |
| 203 | } |
| 204 | |
William A. Kennington III | fe05991 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 205 | TEST_F(EventMethodTest, ExitSuccess) |
| 206 | { |
| 207 | EXPECT_CALL(mock, sd_event_exit(expected_event, 0)).WillOnce(Return(2)); |
| 208 | EXPECT_EQ(2, event->exit(0)); |
| 209 | } |
| 210 | |
| 211 | TEST_F(EventMethodTest, ExitUserError) |
| 212 | { |
| 213 | const int user_error = 10; |
| 214 | EXPECT_CALL(mock, sd_event_exit(expected_event, user_error)) |
| 215 | .WillOnce(Return(user_error)); |
| 216 | EXPECT_EQ(user_error, event->exit(user_error)); |
| 217 | } |
| 218 | |
| 219 | TEST_F(EventMethodTest, ExitInternalError) |
| 220 | { |
| 221 | EXPECT_CALL(mock, sd_event_exit(expected_event, 5)) |
| 222 | .WillOnce(Return(-EINVAL)); |
| 223 | EXPECT_THROW(event->exit(5), SdEventError); |
| 224 | } |
| 225 | |
| 226 | TEST_F(EventMethodTest, GetExitCodeSuccess) |
| 227 | { |
| 228 | EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_)) |
| 229 | .WillOnce(DoAll(SetArgPointee<1>(1), Return(0))); |
| 230 | EXPECT_EQ(1, event->get_exit_code()); |
| 231 | |
| 232 | EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_)) |
| 233 | .WillOnce(DoAll(SetArgPointee<1>(0), Return(2))); |
| 234 | EXPECT_EQ(0, event->get_exit_code()); |
| 235 | } |
| 236 | |
| 237 | TEST_F(EventMethodTest, GetExitCodeError) |
| 238 | { |
| 239 | EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_)) |
| 240 | .WillOnce(Return(-EINVAL)); |
| 241 | EXPECT_THROW(event->get_exit_code(), SdEventError); |
| 242 | } |
| 243 | |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 244 | TEST_F(EventMethodTest, GetWatchdogSuccess) |
| 245 | { |
| 246 | EXPECT_CALL(mock, sd_event_get_watchdog(expected_event)) |
| 247 | .WillOnce(Return(0)); |
| 248 | EXPECT_EQ(0, event->get_watchdog()); |
| 249 | |
| 250 | EXPECT_CALL(mock, sd_event_get_watchdog(expected_event)) |
| 251 | .WillOnce(Return(2)); |
| 252 | EXPECT_EQ(2, event->get_watchdog()); |
| 253 | } |
| 254 | |
| 255 | TEST_F(EventMethodTest, GetWatchdogError) |
| 256 | { |
| 257 | EXPECT_CALL(mock, sd_event_get_watchdog(expected_event)) |
| 258 | .WillOnce(Return(-EINVAL)); |
| 259 | EXPECT_THROW(event->get_watchdog(), SdEventError); |
| 260 | } |
| 261 | |
| 262 | TEST_F(EventMethodTest, SetWatchdogSuccess) |
| 263 | { |
| 264 | EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 0)) |
| 265 | .WillOnce(Return(0)); |
| 266 | EXPECT_EQ(0, event->set_watchdog(0)); |
| 267 | |
| 268 | EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 1)) |
| 269 | .WillOnce(Return(2)); |
| 270 | EXPECT_EQ(2, event->set_watchdog(1)); |
| 271 | } |
| 272 | |
| 273 | TEST_F(EventMethodTest, SetWatchdogError) |
| 274 | { |
| 275 | EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 1)) |
| 276 | .WillOnce(Return(-EINVAL)); |
| 277 | EXPECT_THROW(event->set_watchdog(1), SdEventError); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | } // namespace |
| 281 | } // namespace sdeventplus |