blob: 5cef5d8fd4d163c4e904ddc2a183127556ed83fe [file] [log] [blame]
Patrick Williamsa8c11e32023-05-10 07:50:56 -05001#include <systemd/sd-event.h>
2#include <time.h>
3
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -07004#include <sdeventplus/clock.hpp>
5#include <sdeventplus/exception.hpp>
6#include <sdeventplus/source/time.hpp>
7#include <sdeventplus/test/sdevent.hpp>
Patrick Williamsa8c11e32023-05-10 07:50:56 -05008
9#include <cerrno>
10#include <chrono>
11#include <functional>
12#include <memory>
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070013#include <utility>
14
Patrick Williamsa8c11e32023-05-10 07:50:56 -050015#include <gmock/gmock.h>
16#include <gtest/gtest.h>
17
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070018namespace sdeventplus
19{
20namespace source
21{
22namespace
23{
24
25using testing::DoAll;
26using testing::Return;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070027using testing::ReturnPointee;
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070028using testing::SaveArg;
29using testing::SetArgPointee;
30
31using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
32
33class TimeTest : public testing::Test
34{
35 protected:
36 testing::StrictMock<test::SdEventMock> mock;
37 sd_event_source* const expected_source =
38 reinterpret_cast<sd_event_source*>(1234);
39 sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
40 UniqueEvent event = make_event(expected_event);
41
42 UniqueEvent make_event(sd_event* event)
43 {
44 auto deleter = [this, event](Event* e) {
45 EXPECT_CALL(this->mock, sd_event_unref(event))
46 .WillOnce(Return(nullptr));
47 delete e;
48 };
49 return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
50 }
51
52 void expect_time_destroy(sd_event* event, sd_event_source* source)
53 {
William A. Kennington III5320b1f2019-03-29 20:00:37 -070054 EXPECT_CALL(mock, sd_event_source_unref(source))
55 .WillOnce(Return(nullptr));
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070056 EXPECT_CALL(mock, sd_event_unref(event)).WillOnce(Return(nullptr));
57 }
58};
59
60TEST_F(TimeTest, ConstructSuccess)
61{
62 constexpr ClockId id = ClockId::RealTime;
63 const Time<id>::TimePoint expected_time(std::chrono::seconds{2});
64 const Time<id>::Accuracy expected_accuracy(std::chrono::milliseconds{50});
65 Time<id>::TimePoint saved_time;
Patrick Williamsa8c11e32023-05-10 07:50:56 -050066 Time<id>::Callback callback =
67 [&saved_time](Time<id>&, Time<id>::TimePoint time) {
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070068 saved_time = time;
69 };
70
71 EXPECT_CALL(mock, sd_event_ref(expected_event))
72 .WillOnce(Return(expected_event));
73 sd_event_time_handler_t handler;
74 EXPECT_CALL(mock,
75 sd_event_add_time(expected_event, testing::_, CLOCK_REALTIME,
76 2000000, 50000, testing::_, nullptr))
77 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<5>(&handler),
78 Return(0)));
William A. Kennington III5320b1f2019-03-29 20:00:37 -070079 sd_event_destroy_t destroy;
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070080 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070081 {
82 testing::InSequence seq;
83 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
84 testing::_))
85 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
86 EXPECT_CALL(mock,
87 sd_event_source_set_userdata(expected_source, testing::_))
88 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
89 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
90 .WillRepeatedly(ReturnPointee(&userdata));
91 }
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070092 Time<id> time(*event, expected_time, expected_accuracy,
93 std::move(callback));
William A. Kennington III1072c7f2018-07-19 18:02:52 -070094 EXPECT_FALSE(callback);
William A. Kennington III5320b1f2019-03-29 20:00:37 -070095 EXPECT_NE(&time, userdata);
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070096 EXPECT_EQ(expected_event, time.get_event().get());
97 EXPECT_EQ(expected_source, time.get());
98
99 EXPECT_EQ(0, handler(nullptr, 2000100, userdata));
100 EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)),
101 saved_time);
102
William A. Kennington IIIb53ef902018-10-29 20:06:45 -0700103 time.set_callback(std::bind([]() {}));
104 EXPECT_EQ(0, handler(nullptr, 0, userdata));
105 EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)),
106 saved_time);
107
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700108 expect_time_destroy(expected_event, expected_source);
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700109 destroy(userdata);
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700110}
111
112TEST_F(TimeTest, ConstructError)
113{
114 constexpr ClockId id = ClockId::Monotonic;
115 const Time<id>::TimePoint expected_time(std::chrono::seconds{2});
116 const Time<id>::Accuracy expected_accuracy(std::chrono::milliseconds{50});
117 Time<id>::Callback callback = [](Time<id>&, Time<id>::TimePoint) {};
118
119 EXPECT_CALL(mock,
120 sd_event_add_time(expected_event, testing::_, CLOCK_MONOTONIC,
121 2000000, 50000, testing::_, nullptr))
122 .WillOnce(Return(-ENOSYS));
123 EXPECT_THROW(
124 Time<id>(*event, expected_time, expected_accuracy, std::move(callback)),
125 SdEventError);
William A. Kennington III1072c7f2018-07-19 18:02:52 -0700126 EXPECT_TRUE(callback);
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700127}
128
129class TimeMethodTest : public TimeTest
130{
131 protected:
132 static constexpr ClockId id = ClockId::BootTime;
133 std::unique_ptr<Time<id>> time;
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700134 sd_event_destroy_t destroy;
135 void* userdata;
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700136
137 void SetUp()
138 {
139 EXPECT_CALL(mock, sd_event_ref(expected_event))
140 .WillOnce(Return(expected_event));
141 EXPECT_CALL(mock, sd_event_add_time(expected_event, testing::_,
142 CLOCK_BOOTTIME, 2000000, 50000,
143 testing::_, nullptr))
144 .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700145 {
146 testing::InSequence seq;
147 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(
148 expected_source, testing::_))
149 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
150 EXPECT_CALL(
151 mock, sd_event_source_set_userdata(expected_source, testing::_))
152 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
153 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
154 .WillRepeatedly(ReturnPointee(&userdata));
155 }
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700156 time = std::make_unique<Time<id>>(
157 *event, Time<id>::TimePoint(std::chrono::seconds{2}),
158 std::chrono::milliseconds{50},
159 [](Time<id>&, Time<id>::TimePoint) {});
160 }
161
162 void TearDown()
163 {
164 expect_time_destroy(expected_event, expected_source);
165 time.reset();
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700166 destroy(userdata);
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700167 }
168};
169
William A. Kennington III56dc78b2019-03-29 21:09:13 -0700170TEST_F(TimeMethodTest, Copy)
171{
172 EXPECT_CALL(mock, sd_event_ref(expected_event))
173 .WillOnce(Return(expected_event));
174 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
175 .WillOnce(Return(expected_source));
176 auto time2 = std::make_unique<Time<id>>(*time);
177 {
178 EXPECT_CALL(mock, sd_event_ref(expected_event))
179 .WillOnce(Return(expected_event));
180 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
181 .WillOnce(Return(expected_source));
182 Time<id> time3(*time);
183
184 expect_time_destroy(expected_event, expected_source);
185 EXPECT_CALL(mock, sd_event_ref(expected_event))
186 .WillOnce(Return(expected_event));
187 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
188 .WillOnce(Return(expected_source));
189 *time2 = time3;
190
191 expect_time_destroy(expected_event, expected_source);
192 }
193
194 // Delete the original time
195 time2.swap(time);
196 expect_time_destroy(expected_event, expected_source);
197 time2.reset();
198
199 // Make sure our new copy can still access data
200 time->set_callback(nullptr);
201}
202
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700203TEST_F(TimeMethodTest, SetTimeSuccess)
204{
205 EXPECT_CALL(mock, sd_event_source_set_time(expected_source, 1000000))
206 .WillOnce(Return(0));
207 time->set_time(Time<id>::TimePoint(std::chrono::seconds{1}));
208}
209
210TEST_F(TimeMethodTest, SetTimeError)
211{
212 EXPECT_CALL(mock, sd_event_source_set_time(expected_source, 1000000))
213 .WillOnce(Return(-EINVAL));
214 EXPECT_THROW(time->set_time(Time<id>::TimePoint(std::chrono::seconds{1})),
215 SdEventError);
216}
217
218TEST_F(TimeMethodTest, GetTimeSuccess)
219{
220 EXPECT_CALL(mock, sd_event_source_get_time(expected_source, testing::_))
221 .WillOnce(DoAll(SetArgPointee<1>(10), Return(0)));
222 EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds{10}),
223 time->get_time());
224}
225
226TEST_F(TimeMethodTest, GetTimeError)
227{
228 EXPECT_CALL(mock, sd_event_source_get_time(expected_source, testing::_))
229 .WillOnce(Return(-ENOSYS));
230 EXPECT_THROW(time->get_time(), SdEventError);
231}
232
233TEST_F(TimeMethodTest, SetAccuracySuccess)
234{
235 EXPECT_CALL(mock,
236 sd_event_source_set_time_accuracy(expected_source, 5000000))
237 .WillOnce(Return(0));
238 time->set_accuracy(std::chrono::seconds{5});
239}
240
241TEST_F(TimeMethodTest, SetAccuracyError)
242{
243 EXPECT_CALL(mock,
244 sd_event_source_set_time_accuracy(expected_source, 5000000))
245 .WillOnce(Return(-EINVAL));
246 EXPECT_THROW(time->set_accuracy(std::chrono::seconds{5}), SdEventError);
247}
248
249TEST_F(TimeMethodTest, GetAccuracySuccess)
250{
251 EXPECT_CALL(mock,
252 sd_event_source_get_time_accuracy(expected_source, testing::_))
253 .WillOnce(DoAll(SetArgPointee<1>(1000), Return(0)));
254 EXPECT_EQ(std::chrono::milliseconds{1}, time->get_accuracy());
255}
256
257TEST_F(TimeMethodTest, GetAccuracyError)
258{
259 EXPECT_CALL(mock,
260 sd_event_source_get_time_accuracy(expected_source, testing::_))
261 .WillOnce(Return(-ENOSYS));
262 EXPECT_THROW(time->get_accuracy(), SdEventError);
263}
264
265} // namespace
266} // namespace source
267} // namespace sdeventplus