xref: /openbmc/sdeventplus/test/clock.cpp (revision a8c11e3c)
1 #include <systemd/sd-event.h>
2 
3 #include <sdeventplus/clock.hpp>
4 #include <sdeventplus/event.hpp>
5 #include <sdeventplus/exception.hpp>
6 #include <sdeventplus/test/sdevent.hpp>
7 
8 #include <cerrno>
9 #include <type_traits>
10 #include <utility>
11 
12 #include <gmock/gmock.h>
13 #include <gtest/gtest.h>
14 
15 namespace sdeventplus
16 {
17 namespace
18 {
19 
20 using testing::DoAll;
21 using testing::Return;
22 using testing::SetArgPointee;
23 
24 class 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 
TEST_F(ClockTest,CopyEvent)31 TEST_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 
TEST_F(ClockTest,MoveEvent)48 TEST_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
62