xref: /openbmc/sdeventplus/test/source/event.cpp (revision a8c11e3c)
1 #include <systemd/sd-event.h>
2 
3 #include <sdeventplus/event.hpp>
4 #include <sdeventplus/exception.hpp>
5 #include <sdeventplus/source/event.hpp>
6 #include <sdeventplus/test/sdevent.hpp>
7 
8 #include <cerrno>
9 #include <functional>
10 #include <memory>
11 #include <type_traits>
12 #include <utility>
13 
14 #include <gmock/gmock.h>
15 #include <gtest/gtest.h>
16 
17 namespace sdeventplus
18 {
19 namespace source
20 {
21 namespace
22 {
23 
24 using testing::DoAll;
25 using testing::Return;
26 using testing::ReturnPointee;
27 using testing::SaveArg;
28 using testing::SetArgPointee;
29 
30 using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
31 
32 class 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 
make_event(sd_event * event)41     UniqueEvent make_event(sd_event* event)
42     {
43         auto deleter = [this, event](Event* e) {
44             EXPECT_CALL(this->mock, sd_event_unref(event))
45                 .WillOnce(Return(nullptr));
46             delete e;
47         };
48         return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
49     }
50 
expect_destruct()51     void expect_destruct()
52     {
53         EXPECT_CALL(mock, sd_event_source_unref(expected_source))
54             .WillOnce(Return(nullptr));
55         EXPECT_CALL(mock, sd_event_unref(expected_event))
56             .WillOnce(Return(nullptr));
57     }
58 };
59 
TEST_F(EventTest,DeferConstruct)60 TEST_F(EventTest, DeferConstruct)
61 {
62     EXPECT_CALL(mock, sd_event_ref(expected_event))
63         .WillOnce(Return(expected_event));
64     sd_event_destroy_t destroy;
65     void* userdata;
66     {
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     }
77     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;
83     EventBase::Callback callback = [&completions](EventBase&) {
84         completions++;
85     };
86     Defer defer(*event, std::move(callback));
87     EXPECT_NE(&defer, userdata);
88     EXPECT_FALSE(callback);
89     EXPECT_EQ(0, completions);
90 
91     EXPECT_EQ(0, handler(nullptr, userdata));
92     EXPECT_EQ(1, completions);
93 
94     defer.set_callback(std::bind([]() {}));
95     EXPECT_EQ(0, handler(nullptr, userdata));
96     EXPECT_EQ(1, completions);
97 
98     expect_destruct();
99     destroy(userdata);
100 }
101 
TEST_F(EventTest,PostConstruct)102 TEST_F(EventTest, PostConstruct)
103 {
104     EXPECT_CALL(mock, sd_event_ref(expected_event))
105         .WillOnce(Return(expected_event));
106     sd_event_destroy_t destroy;
107     void* userdata;
108     {
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     }
119     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;
125     EventBase::Callback callback = [&completions](EventBase&) {
126         completions++;
127     };
128     Post post(*event, std::move(callback));
129     EXPECT_NE(&post, userdata);
130     EXPECT_FALSE(callback);
131     EXPECT_EQ(0, completions);
132 
133     EXPECT_EQ(0, handler(nullptr, userdata));
134     EXPECT_EQ(1, completions);
135 
136     expect_destruct();
137     destroy(userdata);
138 }
139 
TEST_F(EventTest,ExitConstruct)140 TEST_F(EventTest, ExitConstruct)
141 {
142     EXPECT_CALL(mock, sd_event_ref(expected_event))
143         .WillOnce(Return(expected_event));
144     sd_event_destroy_t destroy;
145     void* userdata;
146     {
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     }
157     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;
163     EventBase::Callback callback = [&completions](EventBase&) {
164         completions++;
165     };
166     Exit exit(*event, std::move(callback));
167     EXPECT_NE(&exit, userdata);
168     EXPECT_FALSE(callback);
169     EXPECT_EQ(0, completions);
170 
171     EXPECT_EQ(0, handler(nullptr, userdata));
172     EXPECT_EQ(1, completions);
173 
174     expect_destruct();
175     destroy(userdata);
176 }
177 
TEST_F(EventTest,ConstructFailure)178 TEST_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;
184     EventBase::Callback callback = [&completions](EventBase&) {
185         completions++;
186     };
187     EXPECT_THROW(Defer(*event, std::move(callback)), SdEventError);
188     EXPECT_TRUE(callback);
189     EXPECT_EQ(0, completions);
190 }
191 
TEST_F(EventTest,CopyConstruct)192 TEST_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 
249 } // namespace
250 } // namespace source
251 } // namespace sdeventplus
252