xref: /openbmc/sdeventplus/test/clock.cpp (revision 7597a081)
1 #include <cerrno>
2 #include <gmock/gmock.h>
3 #include <gtest/gtest.h>
4 #include <sdeventplus/clock.hpp>
5 #include <sdeventplus/event.hpp>
6 #include <sdeventplus/exception.hpp>
7 #include <sdeventplus/test/sdevent.hpp>
8 #include <systemd/sd-event.h>
9 #include <type_traits>
10 #include <utility>
11 
12 namespace sdeventplus
13 {
14 namespace
15 {
16 
17 using testing::DoAll;
18 using testing::Return;
19 using testing::SetArgPointee;
20 
21 class ClockTest : public testing::Test
22 {
23   protected:
24     testing::StrictMock<test::SdEventMock> mock;
25     sd_event* const expected_event = reinterpret_cast<sd_event*>(1234);
26 };
27 
28 TEST_F(ClockTest, CopyEvent)
29 {
30     Event event(expected_event, std::false_type(), &mock);
31 
32     EXPECT_CALL(mock, sd_event_ref(expected_event))
33         .WillOnce(Return(expected_event));
34     Clock<ClockId::RealTime> clock(event);
35     EXPECT_CALL(mock, sd_event_now(expected_event, CLOCK_REALTIME, testing::_))
36         .WillOnce(DoAll(SetArgPointee<2>(2000000), Return(0)));
37     EXPECT_EQ(Clock<ClockId::RealTime>::time_point(std::chrono::seconds{2}),
38               clock.now());
39 
40     EXPECT_CALL(mock, sd_event_unref(expected_event))
41         .Times(2)
42         .WillRepeatedly(Return(nullptr));
43 }
44 
45 TEST_F(ClockTest, MoveEvent)
46 {
47     Event event(expected_event, std::false_type(), &mock);
48 
49     Clock<ClockId::Monotonic> clock(std::move(event));
50     EXPECT_CALL(mock, sd_event_now(expected_event, CLOCK_MONOTONIC, testing::_))
51         .WillOnce(Return(-EINVAL));
52     EXPECT_THROW(clock.now(), SdEventError);
53 
54     EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
55 }
56 
57 } // namespace
58 } // namespace sdeventplus
59