blob: b6f5699c3b80d19c6d114790721ed7cd0175afc6 [file] [log] [blame]
William A. Kennington III8f90e282018-07-17 14:40:14 -07001#include <sdeventplus/event.hpp>
2#include <sdeventplus/exception.hpp>
3#include <sdeventplus/test/sdevent.hpp>
Patrick Williamsa8c11e32023-05-10 07:50:56 -05004
5#include <memory>
6#include <optional>
William A. Kennington III8f90e282018-07-17 14:40:14 -07007#include <type_traits>
8
Patrick Williamsa8c11e32023-05-10 07:50:56 -05009#include <gmock/gmock.h>
10#include <gtest/gtest.h>
11
William A. Kennington III8f90e282018-07-17 14:40:14 -070012namespace sdeventplus
13{
14namespace
15{
16
17using testing::DoAll;
18using testing::Return;
19using testing::SetArgPointee;
20
21class EventTest : public testing::Test
22{
23 protected:
William A. Kennington III0a816c52018-07-17 14:40:14 -070024 testing::StrictMock<test::SdEventMock> mock;
25 sd_event* const expected_event = reinterpret_cast<sd_event*>(1234);
William A. Kennington III8f90e282018-07-17 14:40:14 -070026};
27
28TEST_F(EventTest, NewEventRef)
29{
30 EXPECT_CALL(mock, sd_event_ref(expected_event))
31 .WillOnce(Return(expected_event));
32 Event event(expected_event, &mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -070033 EXPECT_EQ(&mock, event.getSdEvent());
34 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070035
36 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
37}
38
39TEST_F(EventTest, NewEventNoRef)
40{
41 Event event(expected_event, std::false_type(), &mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -070042 EXPECT_EQ(&mock, event.getSdEvent());
43 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070044
45 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
46}
47
William A. Kennington IIIe68f5b52019-03-23 02:49:28 -070048TEST_F(EventTest, CopyEventNoOwn)
49{
50 Event event(expected_event, std::false_type(), &mock);
51 EXPECT_EQ(&mock, event.getSdEvent());
52 EXPECT_EQ(expected_event, event.get());
53
54 Event event_noown(event, sdeventplus::internal::NoOwn());
55 EXPECT_EQ(&mock, event_noown.getSdEvent());
56 EXPECT_EQ(expected_event, event_noown.get());
57
58 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
59}
60
61TEST_F(EventTest, CopyEventNoOwnCopy)
62{
63 Event event(expected_event, std::false_type(), &mock);
64 EXPECT_EQ(&mock, event.getSdEvent());
65 EXPECT_EQ(expected_event, event.get());
66
67 Event event_noown(event, sdeventplus::internal::NoOwn());
68 EXPECT_EQ(&mock, event_noown.getSdEvent());
69 EXPECT_EQ(expected_event, event_noown.get());
70
71 EXPECT_CALL(mock, sd_event_ref(expected_event))
72 .WillOnce(Return(expected_event));
73 Event event2(event_noown);
74 EXPECT_EQ(&mock, event2.getSdEvent());
75 EXPECT_EQ(expected_event, event2.get());
76
77 EXPECT_CALL(mock, sd_event_unref(expected_event))
78 .WillOnce(Return(nullptr))
79 .WillOnce(Return(nullptr));
80}
81
William A. Kennington III8f90e282018-07-17 14:40:14 -070082TEST_F(EventTest, GetNewEvent)
83{
84 EXPECT_CALL(mock, sd_event_new(testing::_))
85 .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0)));
86 Event event = Event::get_new(&mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -070087 EXPECT_EQ(&mock, event.getSdEvent());
88 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070089
90 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
91}
92
93TEST_F(EventTest, GetNewEventFail)
94{
95 EXPECT_CALL(mock, sd_event_new(testing::_)).WillOnce(Return(-EINVAL));
96 EXPECT_THROW(Event::get_new(&mock), SdEventError);
97}
98
99TEST_F(EventTest, GetDefaultEvent)
100{
101 EXPECT_CALL(mock, sd_event_default(testing::_))
102 .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0)));
103 Event event = Event::get_default(&mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -0700104 EXPECT_EQ(&mock, event.getSdEvent());
105 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -0700106
107 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
108}
109
110TEST_F(EventTest, GetDefaultEventFail)
111{
112 EXPECT_CALL(mock, sd_event_default(testing::_)).WillOnce(Return(-EINVAL));
113 EXPECT_THROW(Event::get_default(&mock), SdEventError);
114}
115
William A. Kennington III631c2462018-07-17 14:40:14 -0700116class EventMethodTest : public EventTest
117{
118 protected:
119 std::unique_ptr<Event> event;
120
121 void SetUp()
122 {
Patrick Williamsa8c11e32023-05-10 07:50:56 -0500123 event = std::make_unique<Event>(expected_event, std::false_type(),
124 &mock);
William A. Kennington III631c2462018-07-17 14:40:14 -0700125 }
126
127 void TearDown()
128 {
129 EXPECT_CALL(mock, sd_event_unref(expected_event))
130 .WillOnce(Return(nullptr));
131 }
132};
133
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700134TEST_F(EventMethodTest, PrepareSuccessNone)
135{
136 EXPECT_CALL(mock, sd_event_prepare(expected_event)).WillOnce(Return(0));
137 EXPECT_EQ(0, event->prepare());
138}
139
140TEST_F(EventMethodTest, PrepareSuccessReady)
141{
142 const int events_ready = 10;
143 EXPECT_CALL(mock, sd_event_prepare(expected_event))
144 .WillOnce(Return(events_ready));
145 EXPECT_EQ(events_ready, event->prepare());
146}
147
148TEST_F(EventMethodTest, PrepareInternalError)
149{
150 EXPECT_CALL(mock, sd_event_prepare(expected_event))
151 .WillOnce(Return(-EINVAL));
152 EXPECT_THROW(event->prepare(), SdEventError);
153}
154
155TEST_F(EventMethodTest, WaitSuccessNone)
156{
157 const std::chrono::microseconds timeout{20};
158 EXPECT_CALL(mock, sd_event_wait(expected_event, timeout.count()))
159 .WillOnce(Return(0));
160 EXPECT_EQ(0, event->wait(timeout));
161}
162
163TEST_F(EventMethodTest, WaitSuccessReady)
164{
165 const int events_ready = 10;
166 EXPECT_CALL(mock, sd_event_wait(expected_event, static_cast<uint64_t>(-1)))
167 .WillOnce(Return(events_ready));
William A. Kennington III5f3cdfa2018-09-11 23:02:38 -0700168 EXPECT_EQ(events_ready, event->wait(std::nullopt));
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700169}
170
171TEST_F(EventMethodTest, WaitInternalError)
172{
173 EXPECT_CALL(mock, sd_event_wait(expected_event, static_cast<uint64_t>(-1)))
174 .WillOnce(Return(-EINVAL));
William A. Kennington III5f3cdfa2018-09-11 23:02:38 -0700175 EXPECT_THROW(event->wait(std::nullopt), SdEventError);
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700176}
177
178TEST_F(EventMethodTest, DispatchInitial)
179{
180 EXPECT_CALL(mock, sd_event_dispatch(expected_event)).WillOnce(Return(0));
181 EXPECT_EQ(0, event->dispatch());
182}
183
184TEST_F(EventMethodTest, DispatchDone)
185{
186 const int done_code = 10;
187 EXPECT_CALL(mock, sd_event_dispatch(expected_event))
188 .WillOnce(Return(done_code));
189 EXPECT_EQ(done_code, event->dispatch());
190}
191
192TEST_F(EventMethodTest, DispatchInternalError)
193{
194 EXPECT_CALL(mock, sd_event_dispatch(expected_event))
195 .WillOnce(Return(-EINVAL));
196 EXPECT_THROW(event->dispatch(), SdEventError);
197}
198
199TEST_F(EventMethodTest, RunSuccessNone)
200{
201 const std::chrono::microseconds timeout{20};
202 EXPECT_CALL(mock, sd_event_run(expected_event, timeout.count()))
203 .WillOnce(Return(0));
204 EXPECT_EQ(0, event->run(timeout));
205}
206
207TEST_F(EventMethodTest, RunSuccessReady)
208{
209 const int events_ready = 10;
210 EXPECT_CALL(mock, sd_event_run(expected_event, static_cast<uint64_t>(-1)))
211 .WillOnce(Return(events_ready));
William A. Kennington III5f3cdfa2018-09-11 23:02:38 -0700212 EXPECT_EQ(events_ready, event->run(std::nullopt));
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700213}
214
215TEST_F(EventMethodTest, RunInternalError)
216{
217 EXPECT_CALL(mock, sd_event_run(expected_event, static_cast<uint64_t>(-1)))
218 .WillOnce(Return(-EINVAL));
William A. Kennington III5f3cdfa2018-09-11 23:02:38 -0700219 EXPECT_THROW(event->run(std::nullopt), SdEventError);
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700220}
221
William A. Kennington III631c2462018-07-17 14:40:14 -0700222TEST_F(EventMethodTest, LoopSuccess)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700223{
224 EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(0));
William A. Kennington III631c2462018-07-17 14:40:14 -0700225 EXPECT_EQ(0, event->loop());
William A. Kennington III8f90e282018-07-17 14:40:14 -0700226}
227
William A. Kennington III631c2462018-07-17 14:40:14 -0700228TEST_F(EventMethodTest, LoopUserError)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700229{
230 const int user_error = 10;
231 EXPECT_CALL(mock, sd_event_loop(expected_event))
232 .WillOnce(Return(user_error));
William A. Kennington III6364dd92018-07-17 14:40:14 -0700233 EXPECT_EQ(user_error, event->loop());
William A. Kennington III8f90e282018-07-17 14:40:14 -0700234}
235
William A. Kennington III631c2462018-07-17 14:40:14 -0700236TEST_F(EventMethodTest, LoopInternalError)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700237{
238 EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(-EINVAL));
William A. Kennington III631c2462018-07-17 14:40:14 -0700239 EXPECT_THROW(event->loop(), SdEventError);
240}
241
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700242TEST_F(EventMethodTest, ExitSuccess)
243{
244 EXPECT_CALL(mock, sd_event_exit(expected_event, 0)).WillOnce(Return(2));
William A. Kennington III0005e302018-07-23 16:03:11 -0700245 event->exit(0);
246 EXPECT_CALL(mock, sd_event_exit(expected_event, 0)).WillOnce(Return(0));
247 event->exit(0);
248 EXPECT_CALL(mock, sd_event_exit(expected_event, 10)).WillOnce(Return(0));
249 event->exit(10);
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700250}
251
252TEST_F(EventMethodTest, ExitInternalError)
253{
254 EXPECT_CALL(mock, sd_event_exit(expected_event, 5))
255 .WillOnce(Return(-EINVAL));
256 EXPECT_THROW(event->exit(5), SdEventError);
257}
258
259TEST_F(EventMethodTest, GetExitCodeSuccess)
260{
261 EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
262 .WillOnce(DoAll(SetArgPointee<1>(1), Return(0)));
263 EXPECT_EQ(1, event->get_exit_code());
264
265 EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
266 .WillOnce(DoAll(SetArgPointee<1>(0), Return(2)));
267 EXPECT_EQ(0, event->get_exit_code());
268}
269
270TEST_F(EventMethodTest, GetExitCodeError)
271{
272 EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
273 .WillOnce(Return(-EINVAL));
274 EXPECT_THROW(event->get_exit_code(), SdEventError);
275}
276
William A. Kennington III631c2462018-07-17 14:40:14 -0700277TEST_F(EventMethodTest, GetWatchdogSuccess)
278{
279 EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
280 .WillOnce(Return(0));
William A. Kennington III0005e302018-07-23 16:03:11 -0700281 EXPECT_FALSE(event->get_watchdog());
William A. Kennington III631c2462018-07-17 14:40:14 -0700282
283 EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
284 .WillOnce(Return(2));
William A. Kennington III0005e302018-07-23 16:03:11 -0700285 EXPECT_TRUE(event->get_watchdog());
William A. Kennington III631c2462018-07-17 14:40:14 -0700286}
287
288TEST_F(EventMethodTest, GetWatchdogError)
289{
290 EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
291 .WillOnce(Return(-EINVAL));
292 EXPECT_THROW(event->get_watchdog(), SdEventError);
293}
294
295TEST_F(EventMethodTest, SetWatchdogSuccess)
296{
William A. Kennington III0005e302018-07-23 16:03:11 -0700297 // Disable
298 EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, false))
William A. Kennington III631c2462018-07-17 14:40:14 -0700299 .WillOnce(Return(0));
William A. Kennington III0005e302018-07-23 16:03:11 -0700300 EXPECT_FALSE(event->set_watchdog(false));
William A. Kennington III631c2462018-07-17 14:40:14 -0700301
William A. Kennington III0005e302018-07-23 16:03:11 -0700302 // Enable but not supported
303 EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, true))
304 .WillOnce(Return(0));
305 EXPECT_FALSE(event->set_watchdog(true));
306
307 // Enabled and supported
308 EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, true))
William A. Kennington III631c2462018-07-17 14:40:14 -0700309 .WillOnce(Return(2));
William A. Kennington III0005e302018-07-23 16:03:11 -0700310 EXPECT_TRUE(event->set_watchdog(true));
William A. Kennington III631c2462018-07-17 14:40:14 -0700311}
312
313TEST_F(EventMethodTest, SetWatchdogError)
314{
315 EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 1))
316 .WillOnce(Return(-EINVAL));
317 EXPECT_THROW(event->set_watchdog(1), SdEventError);
William A. Kennington III8f90e282018-07-17 14:40:14 -0700318}
319
320} // namespace
321} // namespace sdeventplus