blob: 95a3dc0d2b2d97be9cde17092ea7b1fb067c6b5b [file] [log] [blame]
Patrick Williamsa8c11e32023-05-10 07:50:56 -05001#include <systemd/sd-event.h>
2
William A. Kennington III5a7365c2018-07-22 00:22:08 -07003#include <sdeventplus/event.hpp>
4#include <sdeventplus/exception.hpp>
5#include <sdeventplus/source/event.hpp>
6#include <sdeventplus/test/sdevent.hpp>
Patrick Williamsa8c11e32023-05-10 07:50:56 -05007
8#include <cerrno>
9#include <functional>
10#include <memory>
William A. Kennington IIIebcb4f12018-07-22 16:51:36 -070011#include <type_traits>
William A. Kennington III5a7365c2018-07-22 00:22:08 -070012#include <utility>
13
Patrick Williamsa8c11e32023-05-10 07:50:56 -050014#include <gmock/gmock.h>
15#include <gtest/gtest.h>
16
William A. Kennington III5a7365c2018-07-22 00:22:08 -070017namespace sdeventplus
18{
19namespace source
20{
21namespace
22{
23
24using testing::DoAll;
25using testing::Return;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070026using testing::ReturnPointee;
William A. Kennington III5a7365c2018-07-22 00:22:08 -070027using testing::SaveArg;
28using testing::SetArgPointee;
29
William A. Kennington III86be5dd2018-07-23 09:46:18 -070030using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
William A. Kennington III5a7365c2018-07-22 00:22:08 -070031
32class EventTest : public testing::Test
33{
34 protected:
35 testing::StrictMock<test::SdEventMock> mock;
36 sd_event_source* const expected_source =
37 reinterpret_cast<sd_event_source*>(1234);
38 sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
39 UniqueEvent event = make_event(expected_event);
40
41 UniqueEvent make_event(sd_event* event)
42 {
William A. Kennington III86be5dd2018-07-23 09:46:18 -070043 auto deleter = [this, event](Event* e) {
William A. Kennington III5a7365c2018-07-22 00:22:08 -070044 EXPECT_CALL(this->mock, sd_event_unref(event))
45 .WillOnce(Return(nullptr));
46 delete e;
47 };
William A. Kennington III86be5dd2018-07-23 09:46:18 -070048 return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
William A. Kennington III5a7365c2018-07-22 00:22:08 -070049 }
50
51 void expect_destruct()
52 {
William A. Kennington III5320b1f2019-03-29 20:00:37 -070053 EXPECT_CALL(mock, sd_event_source_unref(expected_source))
54 .WillOnce(Return(nullptr));
William A. Kennington III5a7365c2018-07-22 00:22:08 -070055 EXPECT_CALL(mock, sd_event_unref(expected_event))
56 .WillOnce(Return(nullptr));
57 }
58};
59
60TEST_F(EventTest, DeferConstruct)
61{
62 EXPECT_CALL(mock, sd_event_ref(expected_event))
63 .WillOnce(Return(expected_event));
William A. Kennington III5320b1f2019-03-29 20:00:37 -070064 sd_event_destroy_t destroy;
William A. Kennington III5a7365c2018-07-22 00:22:08 -070065 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070066 {
67 testing::InSequence seq;
68 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
69 testing::_))
70 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
71 EXPECT_CALL(mock,
72 sd_event_source_set_userdata(expected_source, testing::_))
73 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
74 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
75 .WillRepeatedly(ReturnPointee(&userdata));
76 }
William A. Kennington III5a7365c2018-07-22 00:22:08 -070077 sd_event_handler_t handler;
78 EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_,
79 nullptr))
80 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
81 Return(0)));
82 int completions = 0;
William A. Kennington III86be5dd2018-07-23 09:46:18 -070083 EventBase::Callback callback = [&completions](EventBase&) {
84 completions++;
85 };
William A. Kennington III5a7365c2018-07-22 00:22:08 -070086 Defer defer(*event, std::move(callback));
William A. Kennington III5320b1f2019-03-29 20:00:37 -070087 EXPECT_NE(&defer, userdata);
William A. Kennington III5a7365c2018-07-22 00:22:08 -070088 EXPECT_FALSE(callback);
89 EXPECT_EQ(0, completions);
90
William A. Kennington III5320b1f2019-03-29 20:00:37 -070091 EXPECT_EQ(0, handler(nullptr, userdata));
William A. Kennington III5a7365c2018-07-22 00:22:08 -070092 EXPECT_EQ(1, completions);
93
William A. Kennington IIIb53ef902018-10-29 20:06:45 -070094 defer.set_callback(std::bind([]() {}));
William A. Kennington III5320b1f2019-03-29 20:00:37 -070095 EXPECT_EQ(0, handler(nullptr, userdata));
William A. Kennington IIIb53ef902018-10-29 20:06:45 -070096 EXPECT_EQ(1, completions);
97
William A. Kennington III5a7365c2018-07-22 00:22:08 -070098 expect_destruct();
William A. Kennington III5320b1f2019-03-29 20:00:37 -070099 destroy(userdata);
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700100}
101
102TEST_F(EventTest, PostConstruct)
103{
104 EXPECT_CALL(mock, sd_event_ref(expected_event))
105 .WillOnce(Return(expected_event));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700106 sd_event_destroy_t destroy;
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700107 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700108 {
109 testing::InSequence seq;
110 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
111 testing::_))
112 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
113 EXPECT_CALL(mock,
114 sd_event_source_set_userdata(expected_source, testing::_))
115 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
116 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
117 .WillRepeatedly(ReturnPointee(&userdata));
118 }
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700119 sd_event_handler_t handler;
120 EXPECT_CALL(mock, sd_event_add_post(expected_event, testing::_, testing::_,
121 nullptr))
122 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
123 Return(0)));
124 int completions = 0;
William A. Kennington III86be5dd2018-07-23 09:46:18 -0700125 EventBase::Callback callback = [&completions](EventBase&) {
126 completions++;
127 };
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700128 Post post(*event, std::move(callback));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700129 EXPECT_NE(&post, userdata);
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700130 EXPECT_FALSE(callback);
131 EXPECT_EQ(0, completions);
132
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700133 EXPECT_EQ(0, handler(nullptr, userdata));
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700134 EXPECT_EQ(1, completions);
135
136 expect_destruct();
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700137 destroy(userdata);
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700138}
139
140TEST_F(EventTest, ExitConstruct)
141{
142 EXPECT_CALL(mock, sd_event_ref(expected_event))
143 .WillOnce(Return(expected_event));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700144 sd_event_destroy_t destroy;
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700145 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700146 {
147 testing::InSequence seq;
148 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
149 testing::_))
150 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
151 EXPECT_CALL(mock,
152 sd_event_source_set_userdata(expected_source, testing::_))
153 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
154 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
155 .WillRepeatedly(ReturnPointee(&userdata));
156 }
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700157 sd_event_handler_t handler;
158 EXPECT_CALL(mock, sd_event_add_exit(expected_event, testing::_, testing::_,
159 nullptr))
160 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
161 Return(0)));
162 int completions = 0;
William A. Kennington III86be5dd2018-07-23 09:46:18 -0700163 EventBase::Callback callback = [&completions](EventBase&) {
164 completions++;
165 };
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700166 Exit exit(*event, std::move(callback));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700167 EXPECT_NE(&exit, userdata);
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700168 EXPECT_FALSE(callback);
169 EXPECT_EQ(0, completions);
170
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700171 EXPECT_EQ(0, handler(nullptr, userdata));
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700172 EXPECT_EQ(1, completions);
173
174 expect_destruct();
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700175 destroy(userdata);
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700176}
177
178TEST_F(EventTest, ConstructFailure)
179{
180 EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_,
181 nullptr))
182 .WillOnce(Return(-EINVAL));
183 int completions = 0;
William A. Kennington III86be5dd2018-07-23 09:46:18 -0700184 EventBase::Callback callback = [&completions](EventBase&) {
185 completions++;
186 };
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700187 EXPECT_THROW(Defer(*event, std::move(callback)), SdEventError);
188 EXPECT_TRUE(callback);
189 EXPECT_EQ(0, completions);
190}
191
William A. Kennington III56dc78b2019-03-29 21:09:13 -0700192TEST_F(EventTest, CopyConstruct)
193{
194 EXPECT_CALL(mock, sd_event_ref(expected_event))
195 .WillOnce(Return(expected_event));
196 sd_event_destroy_t destroy;
197 void* userdata;
198 {
199 testing::InSequence seq;
200 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
201 testing::_))
202 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
203 EXPECT_CALL(mock,
204 sd_event_source_set_userdata(expected_source, testing::_))
205 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
206 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
207 .WillRepeatedly(ReturnPointee(&userdata));
208 }
209 sd_event_handler_t handler;
210 EXPECT_CALL(mock, sd_event_add_exit(expected_event, testing::_, testing::_,
211 nullptr))
212 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
213 Return(0)));
214 auto exit = std::make_unique<Exit>(*event, [](EventBase&) {});
215
216 EXPECT_CALL(mock, sd_event_ref(expected_event))
217 .WillOnce(Return(expected_event));
218 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
219 .WillOnce(Return(expected_source));
220 auto exit2 = std::make_unique<Exit>(*exit);
221 {
222 EXPECT_CALL(mock, sd_event_ref(expected_event))
223 .WillOnce(Return(expected_event));
224 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
225 .WillOnce(Return(expected_source));
226 Exit exit3(*exit);
227
228 expect_destruct();
229 EXPECT_CALL(mock, sd_event_ref(expected_event))
230 .WillOnce(Return(expected_event));
231 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
232 .WillOnce(Return(expected_source));
233 *exit2 = exit3;
234
235 expect_destruct();
236 }
237
238 // Delete the original exit
239 expect_destruct();
240 exit.reset();
241
242 // Make sure our new copy can still access data
243 exit2->set_callback(nullptr);
244 expect_destruct();
245 exit2.reset();
246 destroy(userdata);
247}
248
William A. Kennington III5a7365c2018-07-22 00:22:08 -0700249} // namespace
250} // namespace source
251} // namespace sdeventplus