blob: f5547ae2366c89756623d948cd12d33b85caab29 [file] [log] [blame]
William A. Kennington III8f90e282018-07-17 14:40:14 -07001#include <gmock/gmock.h>
2#include <gtest/gtest.h>
William A. Kennington III631c2462018-07-17 14:40:14 -07003#include <memory>
William A. Kennington III8f90e282018-07-17 14:40:14 -07004#include <sdeventplus/event.hpp>
5#include <sdeventplus/exception.hpp>
6#include <sdeventplus/test/sdevent.hpp>
7#include <type_traits>
8
9namespace sdeventplus
10{
11namespace
12{
13
14using testing::DoAll;
15using testing::Return;
16using testing::SetArgPointee;
17
18class EventTest : public testing::Test
19{
20 protected:
William A. Kennington III0a816c52018-07-17 14:40:14 -070021 testing::StrictMock<test::SdEventMock> mock;
22 sd_event* const expected_event = reinterpret_cast<sd_event*>(1234);
William A. Kennington III8f90e282018-07-17 14:40:14 -070023};
24
25TEST_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 IIIbdc59012018-07-17 14:40:14 -070030 EXPECT_EQ(&mock, event.getSdEvent());
31 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070032
33 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
34}
35
36TEST_F(EventTest, NewEventNoRef)
37{
38 Event event(expected_event, std::false_type(), &mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -070039 EXPECT_EQ(&mock, event.getSdEvent());
40 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070041
42 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
43}
44
45TEST_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 IIIbdc59012018-07-17 14:40:14 -070050 EXPECT_EQ(&mock, event.getSdEvent());
51 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070052
53 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
54}
55
56TEST_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
62TEST_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 IIIbdc59012018-07-17 14:40:14 -070067 EXPECT_EQ(&mock, event.getSdEvent());
68 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070069
70 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
71}
72
73TEST_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 III631c2462018-07-17 14:40:14 -070079class 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 IIIfe059912018-07-17 14:40:14 -070097TEST_F(EventMethodTest, PrepareSuccessNone)
98{
99 EXPECT_CALL(mock, sd_event_prepare(expected_event)).WillOnce(Return(0));
100 EXPECT_EQ(0, event->prepare());
101}
102
103TEST_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
111TEST_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
118TEST_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
126TEST_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
134TEST_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
141TEST_F(EventMethodTest, DispatchInitial)
142{
143 EXPECT_CALL(mock, sd_event_dispatch(expected_event)).WillOnce(Return(0));
144 EXPECT_EQ(0, event->dispatch());
145}
146
147TEST_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
155TEST_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
162TEST_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
170TEST_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
178TEST_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 III631c2462018-07-17 14:40:14 -0700185TEST_F(EventMethodTest, LoopSuccess)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700186{
187 EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(0));
William A. Kennington III631c2462018-07-17 14:40:14 -0700188 EXPECT_EQ(0, event->loop());
William A. Kennington III8f90e282018-07-17 14:40:14 -0700189}
190
William A. Kennington III631c2462018-07-17 14:40:14 -0700191TEST_F(EventMethodTest, LoopUserError)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700192{
193 const int user_error = 10;
194 EXPECT_CALL(mock, sd_event_loop(expected_event))
195 .WillOnce(Return(user_error));
William A. Kennington III6364dd92018-07-17 14:40:14 -0700196 EXPECT_EQ(user_error, event->loop());
William A. Kennington III8f90e282018-07-17 14:40:14 -0700197}
198
William A. Kennington III631c2462018-07-17 14:40:14 -0700199TEST_F(EventMethodTest, LoopInternalError)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700200{
201 EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(-EINVAL));
William A. Kennington III631c2462018-07-17 14:40:14 -0700202 EXPECT_THROW(event->loop(), SdEventError);
203}
204
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700205TEST_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
211TEST_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
219TEST_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
226TEST_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
237TEST_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 III631c2462018-07-17 14:40:14 -0700244TEST_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
255TEST_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
262TEST_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
273TEST_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 III8f90e282018-07-17 14:40:14 -0700278}
279
280} // namespace
281} // namespace sdeventplus