blob: 5cba35f113f80c9870faab46c082bd398c71221c [file] [log] [blame]
Patrick Williamsa8c11e32023-05-10 07:50:56 -05001#include <systemd/sd-event.h>
2
William A. Kennington III7597a082018-07-17 14:40:14 -07003#include <sdeventplus/clock.hpp>
4#include <sdeventplus/event.hpp>
5#include <sdeventplus/exception.hpp>
6#include <sdeventplus/test/sdevent.hpp>
Patrick Williamsa8c11e32023-05-10 07:50:56 -05007
8#include <cerrno>
William A. Kennington III7597a082018-07-17 14:40:14 -07009#include <type_traits>
10#include <utility>
11
Patrick Williamsa8c11e32023-05-10 07:50:56 -050012#include <gmock/gmock.h>
13#include <gtest/gtest.h>
14
William A. Kennington III7597a082018-07-17 14:40:14 -070015namespace sdeventplus
16{
17namespace
18{
19
20using testing::DoAll;
21using testing::Return;
22using testing::SetArgPointee;
23
24class ClockTest : public testing::Test
25{
26 protected:
27 testing::StrictMock<test::SdEventMock> mock;
28 sd_event* const expected_event = reinterpret_cast<sd_event*>(1234);
29};
30
31TEST_F(ClockTest, CopyEvent)
32{
33 Event event(expected_event, std::false_type(), &mock);
34
35 EXPECT_CALL(mock, sd_event_ref(expected_event))
36 .WillOnce(Return(expected_event));
37 Clock<ClockId::RealTime> clock(event);
38 EXPECT_CALL(mock, sd_event_now(expected_event, CLOCK_REALTIME, testing::_))
39 .WillOnce(DoAll(SetArgPointee<2>(2000000), Return(0)));
40 EXPECT_EQ(Clock<ClockId::RealTime>::time_point(std::chrono::seconds{2}),
41 clock.now());
42
43 EXPECT_CALL(mock, sd_event_unref(expected_event))
44 .Times(2)
45 .WillRepeatedly(Return(nullptr));
46}
47
48TEST_F(ClockTest, MoveEvent)
49{
50 Event event(expected_event, std::false_type(), &mock);
51
52 Clock<ClockId::Monotonic> clock(std::move(event));
53 EXPECT_CALL(mock, sd_event_now(expected_event, CLOCK_MONOTONIC, testing::_))
54 .WillOnce(Return(-EINVAL));
55 EXPECT_THROW(clock.now(), SdEventError);
56
57 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
58}
59
60} // namespace
61} // namespace sdeventplus