blob: 1bf5976fbb9fb66c14cc5689c74c285dcbe5a8eb [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 III5f3cdfa2018-09-11 23:02:38 -07004#include <optional>
William A. Kennington III8f90e282018-07-17 14:40:14 -07005#include <sdeventplus/event.hpp>
6#include <sdeventplus/exception.hpp>
7#include <sdeventplus/test/sdevent.hpp>
8#include <type_traits>
9
10namespace sdeventplus
11{
12namespace
13{
14
15using testing::DoAll;
16using testing::Return;
17using testing::SetArgPointee;
18
19class EventTest : public testing::Test
20{
21 protected:
William A. Kennington III0a816c52018-07-17 14:40:14 -070022 testing::StrictMock<test::SdEventMock> mock;
23 sd_event* const expected_event = reinterpret_cast<sd_event*>(1234);
William A. Kennington III8f90e282018-07-17 14:40:14 -070024};
25
26TEST_F(EventTest, NewEventRef)
27{
28 EXPECT_CALL(mock, sd_event_ref(expected_event))
29 .WillOnce(Return(expected_event));
30 Event event(expected_event, &mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -070031 EXPECT_EQ(&mock, event.getSdEvent());
32 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070033
34 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
35}
36
37TEST_F(EventTest, NewEventNoRef)
38{
39 Event event(expected_event, std::false_type(), &mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -070040 EXPECT_EQ(&mock, event.getSdEvent());
41 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070042
43 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
44}
45
46TEST_F(EventTest, GetNewEvent)
47{
48 EXPECT_CALL(mock, sd_event_new(testing::_))
49 .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0)));
50 Event event = Event::get_new(&mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -070051 EXPECT_EQ(&mock, event.getSdEvent());
52 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070053
54 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
55}
56
57TEST_F(EventTest, GetNewEventFail)
58{
59 EXPECT_CALL(mock, sd_event_new(testing::_)).WillOnce(Return(-EINVAL));
60 EXPECT_THROW(Event::get_new(&mock), SdEventError);
61}
62
63TEST_F(EventTest, GetDefaultEvent)
64{
65 EXPECT_CALL(mock, sd_event_default(testing::_))
66 .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0)));
67 Event event = Event::get_default(&mock);
William A. Kennington IIIbdc59012018-07-17 14:40:14 -070068 EXPECT_EQ(&mock, event.getSdEvent());
69 EXPECT_EQ(expected_event, event.get());
William A. Kennington III8f90e282018-07-17 14:40:14 -070070
71 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
72}
73
74TEST_F(EventTest, GetDefaultEventFail)
75{
76 EXPECT_CALL(mock, sd_event_default(testing::_)).WillOnce(Return(-EINVAL));
77 EXPECT_THROW(Event::get_default(&mock), SdEventError);
78}
79
William A. Kennington III631c2462018-07-17 14:40:14 -070080class EventMethodTest : public EventTest
81{
82 protected:
83 std::unique_ptr<Event> event;
84
85 void SetUp()
86 {
87 event =
88 std::make_unique<Event>(expected_event, std::false_type(), &mock);
89 }
90
91 void TearDown()
92 {
93 EXPECT_CALL(mock, sd_event_unref(expected_event))
94 .WillOnce(Return(nullptr));
95 }
96};
97
William A. Kennington IIIfe059912018-07-17 14:40:14 -070098TEST_F(EventMethodTest, PrepareSuccessNone)
99{
100 EXPECT_CALL(mock, sd_event_prepare(expected_event)).WillOnce(Return(0));
101 EXPECT_EQ(0, event->prepare());
102}
103
104TEST_F(EventMethodTest, PrepareSuccessReady)
105{
106 const int events_ready = 10;
107 EXPECT_CALL(mock, sd_event_prepare(expected_event))
108 .WillOnce(Return(events_ready));
109 EXPECT_EQ(events_ready, event->prepare());
110}
111
112TEST_F(EventMethodTest, PrepareInternalError)
113{
114 EXPECT_CALL(mock, sd_event_prepare(expected_event))
115 .WillOnce(Return(-EINVAL));
116 EXPECT_THROW(event->prepare(), SdEventError);
117}
118
119TEST_F(EventMethodTest, WaitSuccessNone)
120{
121 const std::chrono::microseconds timeout{20};
122 EXPECT_CALL(mock, sd_event_wait(expected_event, timeout.count()))
123 .WillOnce(Return(0));
124 EXPECT_EQ(0, event->wait(timeout));
125}
126
127TEST_F(EventMethodTest, WaitSuccessReady)
128{
129 const int events_ready = 10;
130 EXPECT_CALL(mock, sd_event_wait(expected_event, static_cast<uint64_t>(-1)))
131 .WillOnce(Return(events_ready));
William A. Kennington III5f3cdfa2018-09-11 23:02:38 -0700132 EXPECT_EQ(events_ready, event->wait(std::nullopt));
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700133}
134
135TEST_F(EventMethodTest, WaitInternalError)
136{
137 EXPECT_CALL(mock, sd_event_wait(expected_event, static_cast<uint64_t>(-1)))
138 .WillOnce(Return(-EINVAL));
William A. Kennington III5f3cdfa2018-09-11 23:02:38 -0700139 EXPECT_THROW(event->wait(std::nullopt), SdEventError);
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700140}
141
142TEST_F(EventMethodTest, DispatchInitial)
143{
144 EXPECT_CALL(mock, sd_event_dispatch(expected_event)).WillOnce(Return(0));
145 EXPECT_EQ(0, event->dispatch());
146}
147
148TEST_F(EventMethodTest, DispatchDone)
149{
150 const int done_code = 10;
151 EXPECT_CALL(mock, sd_event_dispatch(expected_event))
152 .WillOnce(Return(done_code));
153 EXPECT_EQ(done_code, event->dispatch());
154}
155
156TEST_F(EventMethodTest, DispatchInternalError)
157{
158 EXPECT_CALL(mock, sd_event_dispatch(expected_event))
159 .WillOnce(Return(-EINVAL));
160 EXPECT_THROW(event->dispatch(), SdEventError);
161}
162
163TEST_F(EventMethodTest, RunSuccessNone)
164{
165 const std::chrono::microseconds timeout{20};
166 EXPECT_CALL(mock, sd_event_run(expected_event, timeout.count()))
167 .WillOnce(Return(0));
168 EXPECT_EQ(0, event->run(timeout));
169}
170
171TEST_F(EventMethodTest, RunSuccessReady)
172{
173 const int events_ready = 10;
174 EXPECT_CALL(mock, sd_event_run(expected_event, static_cast<uint64_t>(-1)))
175 .WillOnce(Return(events_ready));
William A. Kennington III5f3cdfa2018-09-11 23:02:38 -0700176 EXPECT_EQ(events_ready, event->run(std::nullopt));
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700177}
178
179TEST_F(EventMethodTest, RunInternalError)
180{
181 EXPECT_CALL(mock, sd_event_run(expected_event, static_cast<uint64_t>(-1)))
182 .WillOnce(Return(-EINVAL));
William A. Kennington III5f3cdfa2018-09-11 23:02:38 -0700183 EXPECT_THROW(event->run(std::nullopt), SdEventError);
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700184}
185
William A. Kennington III631c2462018-07-17 14:40:14 -0700186TEST_F(EventMethodTest, LoopSuccess)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700187{
188 EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(0));
William A. Kennington III631c2462018-07-17 14:40:14 -0700189 EXPECT_EQ(0, event->loop());
William A. Kennington III8f90e282018-07-17 14:40:14 -0700190}
191
William A. Kennington III631c2462018-07-17 14:40:14 -0700192TEST_F(EventMethodTest, LoopUserError)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700193{
194 const int user_error = 10;
195 EXPECT_CALL(mock, sd_event_loop(expected_event))
196 .WillOnce(Return(user_error));
William A. Kennington III6364dd92018-07-17 14:40:14 -0700197 EXPECT_EQ(user_error, event->loop());
William A. Kennington III8f90e282018-07-17 14:40:14 -0700198}
199
William A. Kennington III631c2462018-07-17 14:40:14 -0700200TEST_F(EventMethodTest, LoopInternalError)
William A. Kennington III8f90e282018-07-17 14:40:14 -0700201{
202 EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(-EINVAL));
William A. Kennington III631c2462018-07-17 14:40:14 -0700203 EXPECT_THROW(event->loop(), SdEventError);
204}
205
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700206TEST_F(EventMethodTest, ExitSuccess)
207{
208 EXPECT_CALL(mock, sd_event_exit(expected_event, 0)).WillOnce(Return(2));
William A. Kennington III0005e302018-07-23 16:03:11 -0700209 event->exit(0);
210 EXPECT_CALL(mock, sd_event_exit(expected_event, 0)).WillOnce(Return(0));
211 event->exit(0);
212 EXPECT_CALL(mock, sd_event_exit(expected_event, 10)).WillOnce(Return(0));
213 event->exit(10);
William A. Kennington IIIfe059912018-07-17 14:40:14 -0700214}
215
216TEST_F(EventMethodTest, ExitInternalError)
217{
218 EXPECT_CALL(mock, sd_event_exit(expected_event, 5))
219 .WillOnce(Return(-EINVAL));
220 EXPECT_THROW(event->exit(5), SdEventError);
221}
222
223TEST_F(EventMethodTest, GetExitCodeSuccess)
224{
225 EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
226 .WillOnce(DoAll(SetArgPointee<1>(1), Return(0)));
227 EXPECT_EQ(1, event->get_exit_code());
228
229 EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
230 .WillOnce(DoAll(SetArgPointee<1>(0), Return(2)));
231 EXPECT_EQ(0, event->get_exit_code());
232}
233
234TEST_F(EventMethodTest, GetExitCodeError)
235{
236 EXPECT_CALL(mock, sd_event_get_exit_code(expected_event, testing::_))
237 .WillOnce(Return(-EINVAL));
238 EXPECT_THROW(event->get_exit_code(), SdEventError);
239}
240
William A. Kennington III631c2462018-07-17 14:40:14 -0700241TEST_F(EventMethodTest, GetWatchdogSuccess)
242{
243 EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
244 .WillOnce(Return(0));
William A. Kennington III0005e302018-07-23 16:03:11 -0700245 EXPECT_FALSE(event->get_watchdog());
William A. Kennington III631c2462018-07-17 14:40:14 -0700246
247 EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
248 .WillOnce(Return(2));
William A. Kennington III0005e302018-07-23 16:03:11 -0700249 EXPECT_TRUE(event->get_watchdog());
William A. Kennington III631c2462018-07-17 14:40:14 -0700250}
251
252TEST_F(EventMethodTest, GetWatchdogError)
253{
254 EXPECT_CALL(mock, sd_event_get_watchdog(expected_event))
255 .WillOnce(Return(-EINVAL));
256 EXPECT_THROW(event->get_watchdog(), SdEventError);
257}
258
259TEST_F(EventMethodTest, SetWatchdogSuccess)
260{
William A. Kennington III0005e302018-07-23 16:03:11 -0700261 // Disable
262 EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, false))
William A. Kennington III631c2462018-07-17 14:40:14 -0700263 .WillOnce(Return(0));
William A. Kennington III0005e302018-07-23 16:03:11 -0700264 EXPECT_FALSE(event->set_watchdog(false));
William A. Kennington III631c2462018-07-17 14:40:14 -0700265
William A. Kennington III0005e302018-07-23 16:03:11 -0700266 // Enable but not supported
267 EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, true))
268 .WillOnce(Return(0));
269 EXPECT_FALSE(event->set_watchdog(true));
270
271 // Enabled and supported
272 EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, true))
William A. Kennington III631c2462018-07-17 14:40:14 -0700273 .WillOnce(Return(2));
William A. Kennington III0005e302018-07-23 16:03:11 -0700274 EXPECT_TRUE(event->set_watchdog(true));
William A. Kennington III631c2462018-07-17 14:40:14 -0700275}
276
277TEST_F(EventMethodTest, SetWatchdogError)
278{
279 EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 1))
280 .WillOnce(Return(-EINVAL));
281 EXPECT_THROW(event->set_watchdog(1), SdEventError);
William A. Kennington III8f90e282018-07-17 14:40:14 -0700282}
283
284} // namespace
285} // namespace sdeventplus