| #include <systemd/sd-event.h> |
| #include <time.h> |
| |
| #include <sdeventplus/clock.hpp> |
| #include <sdeventplus/exception.hpp> |
| #include <sdeventplus/source/time.hpp> |
| #include <sdeventplus/test/sdevent.hpp> |
| |
| #include <cerrno> |
| #include <chrono> |
| #include <functional> |
| #include <memory> |
| #include <utility> |
| |
| #include <gmock/gmock.h> |
| #include <gtest/gtest.h> |
| |
| namespace sdeventplus |
| { |
| namespace source |
| { |
| namespace |
| { |
| |
| using testing::DoAll; |
| using testing::Return; |
| using testing::ReturnPointee; |
| using testing::SaveArg; |
| using testing::SetArgPointee; |
| |
| using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>; |
| |
| class TimeTest : public testing::Test |
| { |
| protected: |
| testing::StrictMock<test::SdEventMock> mock; |
| sd_event_source* const expected_source = |
| reinterpret_cast<sd_event_source*>(1234); |
| sd_event* const expected_event = reinterpret_cast<sd_event*>(2345); |
| UniqueEvent event = make_event(expected_event); |
| |
| UniqueEvent make_event(sd_event* event) |
| { |
| auto deleter = [this, event](Event* e) { |
| EXPECT_CALL(this->mock, sd_event_unref(event)) |
| .WillOnce(Return(nullptr)); |
| delete e; |
| }; |
| return UniqueEvent(new Event(event, std::false_type(), &mock), deleter); |
| } |
| |
| void expect_time_destroy(sd_event* event, sd_event_source* source) |
| { |
| EXPECT_CALL(mock, sd_event_source_unref(source)) |
| .WillOnce(Return(nullptr)); |
| EXPECT_CALL(mock, sd_event_unref(event)).WillOnce(Return(nullptr)); |
| } |
| }; |
| |
| TEST_F(TimeTest, ConstructSuccess) |
| { |
| constexpr ClockId id = ClockId::RealTime; |
| const Time<id>::TimePoint expected_time(std::chrono::seconds{2}); |
| const Time<id>::Accuracy expected_accuracy(std::chrono::milliseconds{50}); |
| Time<id>::TimePoint saved_time; |
| Time<id>::Callback callback = [&saved_time](Time<id>&, |
| Time<id>::TimePoint time) { |
| saved_time = time; |
| }; |
| |
| EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| .WillOnce(Return(expected_event)); |
| sd_event_time_handler_t handler; |
| EXPECT_CALL(mock, |
| sd_event_add_time(expected_event, testing::_, CLOCK_REALTIME, |
| 2000000, 50000, testing::_, nullptr)) |
| .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<5>(&handler), |
| Return(0))); |
| sd_event_destroy_t destroy; |
| void* userdata; |
| { |
| testing::InSequence seq; |
| EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| testing::_)) |
| .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| EXPECT_CALL(mock, |
| sd_event_source_set_userdata(expected_source, testing::_)) |
| .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| .WillRepeatedly(ReturnPointee(&userdata)); |
| } |
| Time<id> time(*event, expected_time, expected_accuracy, |
| std::move(callback)); |
| EXPECT_FALSE(callback); |
| EXPECT_NE(&time, userdata); |
| EXPECT_EQ(expected_event, time.get_event().get()); |
| EXPECT_EQ(expected_source, time.get()); |
| |
| EXPECT_EQ(0, handler(nullptr, 2000100, userdata)); |
| EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)), |
| saved_time); |
| |
| time.set_callback(std::bind([]() {})); |
| EXPECT_EQ(0, handler(nullptr, 0, userdata)); |
| EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds(2000100)), |
| saved_time); |
| |
| expect_time_destroy(expected_event, expected_source); |
| destroy(userdata); |
| } |
| |
| TEST_F(TimeTest, ConstructError) |
| { |
| constexpr ClockId id = ClockId::Monotonic; |
| const Time<id>::TimePoint expected_time(std::chrono::seconds{2}); |
| const Time<id>::Accuracy expected_accuracy(std::chrono::milliseconds{50}); |
| Time<id>::Callback callback = [](Time<id>&, Time<id>::TimePoint) {}; |
| |
| EXPECT_CALL(mock, |
| sd_event_add_time(expected_event, testing::_, CLOCK_MONOTONIC, |
| 2000000, 50000, testing::_, nullptr)) |
| .WillOnce(Return(-ENOSYS)); |
| EXPECT_THROW( |
| Time<id>(*event, expected_time, expected_accuracy, std::move(callback)), |
| SdEventError); |
| EXPECT_TRUE(callback); |
| } |
| |
| class TimeMethodTest : public TimeTest |
| { |
| protected: |
| static constexpr ClockId id = ClockId::BootTime; |
| std::unique_ptr<Time<id>> time; |
| sd_event_destroy_t destroy; |
| void* userdata; |
| |
| void SetUp() |
| { |
| EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| .WillOnce(Return(expected_event)); |
| EXPECT_CALL(mock, sd_event_add_time(expected_event, testing::_, |
| CLOCK_BOOTTIME, 2000000, 50000, |
| testing::_, nullptr)) |
| .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0))); |
| { |
| testing::InSequence seq; |
| EXPECT_CALL(mock, sd_event_source_set_destroy_callback( |
| expected_source, testing::_)) |
| .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| EXPECT_CALL( |
| mock, sd_event_source_set_userdata(expected_source, testing::_)) |
| .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| .WillRepeatedly(ReturnPointee(&userdata)); |
| } |
| time = std::make_unique<Time<id>>( |
| *event, Time<id>::TimePoint(std::chrono::seconds{2}), |
| std::chrono::milliseconds{50}, |
| [](Time<id>&, Time<id>::TimePoint) {}); |
| } |
| |
| void TearDown() |
| { |
| expect_time_destroy(expected_event, expected_source); |
| time.reset(); |
| destroy(userdata); |
| } |
| }; |
| |
| TEST_F(TimeMethodTest, Copy) |
| { |
| EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| .WillOnce(Return(expected_event)); |
| EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| .WillOnce(Return(expected_source)); |
| auto time2 = std::make_unique<Time<id>>(*time); |
| { |
| EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| .WillOnce(Return(expected_event)); |
| EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| .WillOnce(Return(expected_source)); |
| Time<id> time3(*time); |
| |
| expect_time_destroy(expected_event, expected_source); |
| EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| .WillOnce(Return(expected_event)); |
| EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| .WillOnce(Return(expected_source)); |
| *time2 = time3; |
| |
| expect_time_destroy(expected_event, expected_source); |
| } |
| |
| // Delete the original time |
| time2.swap(time); |
| expect_time_destroy(expected_event, expected_source); |
| time2.reset(); |
| |
| // Make sure our new copy can still access data |
| time->set_callback(nullptr); |
| } |
| |
| TEST_F(TimeMethodTest, SetTimeSuccess) |
| { |
| EXPECT_CALL(mock, sd_event_source_set_time(expected_source, 1000000)) |
| .WillOnce(Return(0)); |
| time->set_time(Time<id>::TimePoint(std::chrono::seconds{1})); |
| } |
| |
| TEST_F(TimeMethodTest, SetTimeError) |
| { |
| EXPECT_CALL(mock, sd_event_source_set_time(expected_source, 1000000)) |
| .WillOnce(Return(-EINVAL)); |
| EXPECT_THROW(time->set_time(Time<id>::TimePoint(std::chrono::seconds{1})), |
| SdEventError); |
| } |
| |
| TEST_F(TimeMethodTest, GetTimeSuccess) |
| { |
| EXPECT_CALL(mock, sd_event_source_get_time(expected_source, testing::_)) |
| .WillOnce(DoAll(SetArgPointee<1>(10), Return(0))); |
| EXPECT_EQ(Time<id>::TimePoint(std::chrono::microseconds{10}), |
| time->get_time()); |
| } |
| |
| TEST_F(TimeMethodTest, GetTimeError) |
| { |
| EXPECT_CALL(mock, sd_event_source_get_time(expected_source, testing::_)) |
| .WillOnce(Return(-ENOSYS)); |
| EXPECT_THROW(time->get_time(), SdEventError); |
| } |
| |
| TEST_F(TimeMethodTest, SetAccuracySuccess) |
| { |
| EXPECT_CALL(mock, |
| sd_event_source_set_time_accuracy(expected_source, 5000000)) |
| .WillOnce(Return(0)); |
| time->set_accuracy(std::chrono::seconds{5}); |
| } |
| |
| TEST_F(TimeMethodTest, SetAccuracyError) |
| { |
| EXPECT_CALL(mock, |
| sd_event_source_set_time_accuracy(expected_source, 5000000)) |
| .WillOnce(Return(-EINVAL)); |
| EXPECT_THROW(time->set_accuracy(std::chrono::seconds{5}), SdEventError); |
| } |
| |
| TEST_F(TimeMethodTest, GetAccuracySuccess) |
| { |
| EXPECT_CALL(mock, |
| sd_event_source_get_time_accuracy(expected_source, testing::_)) |
| .WillOnce(DoAll(SetArgPointee<1>(1000), Return(0))); |
| EXPECT_EQ(std::chrono::milliseconds{1}, time->get_accuracy()); |
| } |
| |
| TEST_F(TimeMethodTest, GetAccuracyError) |
| { |
| EXPECT_CALL(mock, |
| sd_event_source_get_time_accuracy(expected_source, testing::_)) |
| .WillOnce(Return(-ENOSYS)); |
| EXPECT_THROW(time->get_accuracy(), SdEventError); |
| } |
| |
| } // namespace |
| } // namespace source |
| } // namespace sdeventplus |