blob: aa25da049d71876ada313c3a653d0345ef39dbe9 [file] [log] [blame]
#include <cerrno>
#include <chrono>
#include <functional>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
#include <sdeventplus/clock.hpp>
#include <sdeventplus/exception.hpp>
#include <sdeventplus/source/time.hpp>
#include <sdeventplus/test/sdevent.hpp>
#include <systemd/sd-event.h>
#include <time.h>
#include <utility>
namespace sdeventplus
{
namespace source
{
namespace
{
using testing::DoAll;
using testing::Return;
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)
{
{
testing::InSequence sequence;
EXPECT_CALL(mock, sd_event_source_set_enabled(source, SD_EVENT_OFF))
.WillOnce(Return(0));
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)));
void* userdata;
EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
.WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
Time<id> time(*event, expected_time, expected_accuracy,
std::move(callback));
EXPECT_FALSE(callback);
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);
expect_time_destroy(expected_event, expected_source);
}
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;
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)));
EXPECT_CALL(mock,
sd_event_source_set_userdata(expected_source, testing::_))
.WillOnce(Return(nullptr));
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();
}
};
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