blob: 01d4ae23e83bbc7e16b3e6586f3b0f2698a9bc7c [file] [log] [blame]
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -07001#include <cerrno>
2#include <chrono>
William A. Kennington III425691a2018-07-22 12:22:13 -07003#include <functional>
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -07004#include <gmock/gmock.h>
5#include <gtest/gtest.h>
6#include <memory>
7#include <sdeventplus/clock.hpp>
8#include <sdeventplus/exception.hpp>
9#include <sdeventplus/source/time.hpp>
10#include <sdeventplus/test/sdevent.hpp>
11#include <systemd/sd-event.h>
12#include <time.h>
13#include <utility>
14
15namespace sdeventplus
16{
17namespace source
18{
19namespace
20{
21
22using testing::DoAll;
23using testing::Return;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070024using testing::ReturnPointee;
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070025using testing::SaveArg;
26using testing::SetArgPointee;
27
28using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
29
30class TimeTest : public testing::Test
31{
32 protected:
33 testing::StrictMock<test::SdEventMock> mock;
34 sd_event_source* const expected_source =
35 reinterpret_cast<sd_event_source*>(1234);
36 sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
37 UniqueEvent event = make_event(expected_event);
38
39 UniqueEvent make_event(sd_event* event)
40 {
41 auto deleter = [this, event](Event* e) {
42 EXPECT_CALL(this->mock, sd_event_unref(event))
43 .WillOnce(Return(nullptr));
44 delete e;
45 };
46 return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
47 }
48
49 void expect_time_destroy(sd_event* event, sd_event_source* source)
50 {
William A. Kennington III5320b1f2019-03-29 20:00:37 -070051 EXPECT_CALL(mock, sd_event_source_unref(source))
52 .WillOnce(Return(nullptr));
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070053 EXPECT_CALL(mock, sd_event_unref(event)).WillOnce(Return(nullptr));
54 }
55};
56
57TEST_F(TimeTest, ConstructSuccess)
58{
59 constexpr ClockId id = ClockId::RealTime;
60 const Time<id>::TimePoint expected_time(std::chrono::seconds{2});
61 const Time<id>::Accuracy expected_accuracy(std::chrono::milliseconds{50});
62 Time<id>::TimePoint saved_time;
63 Time<id>::Callback callback = [&saved_time](Time<id>&,
64 Time<id>::TimePoint time) {
65 saved_time = time;
66 };
67
68 EXPECT_CALL(mock, sd_event_ref(expected_event))
69 .WillOnce(Return(expected_event));
70 sd_event_time_handler_t handler;
71 EXPECT_CALL(mock,
72 sd_event_add_time(expected_event, testing::_, CLOCK_REALTIME,
73 2000000, 50000, testing::_, nullptr))
74 .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<5>(&handler),
75 Return(0)));
William A. Kennington III5320b1f2019-03-29 20:00:37 -070076 sd_event_destroy_t destroy;
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070077 void* userdata;
William A. Kennington III5320b1f2019-03-29 20:00:37 -070078 {
79 testing::InSequence seq;
80 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source,
81 testing::_))
82 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
83 EXPECT_CALL(mock,
84 sd_event_source_set_userdata(expected_source, testing::_))
85 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
86 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
87 .WillRepeatedly(ReturnPointee(&userdata));
88 }
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070089 Time<id> time(*event, expected_time, expected_accuracy,
90 std::move(callback));
William A. Kennington III1072c7f2018-07-19 18:02:52 -070091 EXPECT_FALSE(callback);
William A. Kennington III5320b1f2019-03-29 20:00:37 -070092 EXPECT_NE(&time, userdata);
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -070093 EXPECT_EQ(expected_event, time.get_event().get());
94 EXPECT_EQ(expected_source, time.get());
95
96 EXPECT_EQ(0, handler(nullptr, 2000100, userdata));
97 EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)),
98 saved_time);
99
William A. Kennington IIIb53ef902018-10-29 20:06:45 -0700100 time.set_callback(std::bind([]() {}));
101 EXPECT_EQ(0, handler(nullptr, 0, userdata));
102 EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)),
103 saved_time);
104
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700105 expect_time_destroy(expected_event, expected_source);
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700106 destroy(userdata);
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700107}
108
109TEST_F(TimeTest, ConstructError)
110{
111 constexpr ClockId id = ClockId::Monotonic;
112 const Time<id>::TimePoint expected_time(std::chrono::seconds{2});
113 const Time<id>::Accuracy expected_accuracy(std::chrono::milliseconds{50});
114 Time<id>::Callback callback = [](Time<id>&, Time<id>::TimePoint) {};
115
116 EXPECT_CALL(mock,
117 sd_event_add_time(expected_event, testing::_, CLOCK_MONOTONIC,
118 2000000, 50000, testing::_, nullptr))
119 .WillOnce(Return(-ENOSYS));
120 EXPECT_THROW(
121 Time<id>(*event, expected_time, expected_accuracy, std::move(callback)),
122 SdEventError);
William A. Kennington III1072c7f2018-07-19 18:02:52 -0700123 EXPECT_TRUE(callback);
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700124}
125
126class TimeMethodTest : public TimeTest
127{
128 protected:
129 static constexpr ClockId id = ClockId::BootTime;
130 std::unique_ptr<Time<id>> time;
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700131 sd_event_destroy_t destroy;
132 void* userdata;
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700133
134 void SetUp()
135 {
136 EXPECT_CALL(mock, sd_event_ref(expected_event))
137 .WillOnce(Return(expected_event));
138 EXPECT_CALL(mock, sd_event_add_time(expected_event, testing::_,
139 CLOCK_BOOTTIME, 2000000, 50000,
140 testing::_, nullptr))
141 .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700142 {
143 testing::InSequence seq;
144 EXPECT_CALL(mock, sd_event_source_set_destroy_callback(
145 expected_source, testing::_))
146 .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0)));
147 EXPECT_CALL(
148 mock, sd_event_source_set_userdata(expected_source, testing::_))
149 .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
150 EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source))
151 .WillRepeatedly(ReturnPointee(&userdata));
152 }
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700153 time = std::make_unique<Time<id>>(
154 *event, Time<id>::TimePoint(std::chrono::seconds{2}),
155 std::chrono::milliseconds{50},
156 [](Time<id>&, Time<id>::TimePoint) {});
157 }
158
159 void TearDown()
160 {
161 expect_time_destroy(expected_event, expected_source);
162 time.reset();
William A. Kennington III5320b1f2019-03-29 20:00:37 -0700163 destroy(userdata);
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700164 }
165};
166
William A. Kennington III56dc78b2019-03-29 21:09:13 -0700167TEST_F(TimeMethodTest, Copy)
168{
169 EXPECT_CALL(mock, sd_event_ref(expected_event))
170 .WillOnce(Return(expected_event));
171 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
172 .WillOnce(Return(expected_source));
173 auto time2 = std::make_unique<Time<id>>(*time);
174 {
175 EXPECT_CALL(mock, sd_event_ref(expected_event))
176 .WillOnce(Return(expected_event));
177 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
178 .WillOnce(Return(expected_source));
179 Time<id> time3(*time);
180
181 expect_time_destroy(expected_event, expected_source);
182 EXPECT_CALL(mock, sd_event_ref(expected_event))
183 .WillOnce(Return(expected_event));
184 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
185 .WillOnce(Return(expected_source));
186 *time2 = time3;
187
188 expect_time_destroy(expected_event, expected_source);
189 }
190
191 // Delete the original time
192 time2.swap(time);
193 expect_time_destroy(expected_event, expected_source);
194 time2.reset();
195
196 // Make sure our new copy can still access data
197 time->set_callback(nullptr);
198}
199
William A. Kennington IIIe32a88e2018-07-17 14:40:14 -0700200TEST_F(TimeMethodTest, SetTimeSuccess)
201{
202 EXPECT_CALL(mock, sd_event_source_set_time(expected_source, 1000000))
203 .WillOnce(Return(0));
204 time->set_time(Time<id>::TimePoint(std::chrono::seconds{1}));
205}
206
207TEST_F(TimeMethodTest, SetTimeError)
208{
209 EXPECT_CALL(mock, sd_event_source_set_time(expected_source, 1000000))
210 .WillOnce(Return(-EINVAL));
211 EXPECT_THROW(time->set_time(Time<id>::TimePoint(std::chrono::seconds{1})),
212 SdEventError);
213}
214
215TEST_F(TimeMethodTest, GetTimeSuccess)
216{
217 EXPECT_CALL(mock, sd_event_source_get_time(expected_source, testing::_))
218 .WillOnce(DoAll(SetArgPointee<1>(10), Return(0)));
219 EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds{10}),
220 time->get_time());
221}
222
223TEST_F(TimeMethodTest, GetTimeError)
224{
225 EXPECT_CALL(mock, sd_event_source_get_time(expected_source, testing::_))
226 .WillOnce(Return(-ENOSYS));
227 EXPECT_THROW(time->get_time(), SdEventError);
228}
229
230TEST_F(TimeMethodTest, SetAccuracySuccess)
231{
232 EXPECT_CALL(mock,
233 sd_event_source_set_time_accuracy(expected_source, 5000000))
234 .WillOnce(Return(0));
235 time->set_accuracy(std::chrono::seconds{5});
236}
237
238TEST_F(TimeMethodTest, SetAccuracyError)
239{
240 EXPECT_CALL(mock,
241 sd_event_source_set_time_accuracy(expected_source, 5000000))
242 .WillOnce(Return(-EINVAL));
243 EXPECT_THROW(time->set_accuracy(std::chrono::seconds{5}), SdEventError);
244}
245
246TEST_F(TimeMethodTest, GetAccuracySuccess)
247{
248 EXPECT_CALL(mock,
249 sd_event_source_get_time_accuracy(expected_source, testing::_))
250 .WillOnce(DoAll(SetArgPointee<1>(1000), Return(0)));
251 EXPECT_EQ(std::chrono::milliseconds{1}, time->get_accuracy());
252}
253
254TEST_F(TimeMethodTest, GetAccuracyError)
255{
256 EXPECT_CALL(mock,
257 sd_event_source_get_time_accuracy(expected_source, testing::_))
258 .WillOnce(Return(-ENOSYS));
259 EXPECT_THROW(time->get_accuracy(), SdEventError);
260}
261
262} // namespace
263} // namespace source
264} // namespace sdeventplus