blob: 95a3dc0d2b2d97be9cde17092ea7b1fb067c6b5b [file] [log] [blame]
#include <systemd/sd-event.h>
#include <sdeventplus/event.hpp>
#include <sdeventplus/exception.hpp>
#include <sdeventplus/source/event.hpp>
#include <sdeventplus/test/sdevent.hpp>
#include <cerrno>
#include <functional>
#include <memory>
#include <type_traits>
#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 EventTest : 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_destruct()
{
EXPECT_CALL(mock, sd_event_source_unref(expected_source))
.WillOnce(Return(nullptr));
EXPECT_CALL(mock, sd_event_unref(expected_event))
.WillOnce(Return(nullptr));
}
};
TEST_F(EventTest, DeferConstruct)
{
EXPECT_CALL(mock, sd_event_ref(expected_event))
.WillOnce(Return(expected_event));
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));
}
sd_event_handler_t handler;
EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_,
nullptr))
.WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
Return(0)));
int completions = 0;
EventBase::Callback callback = [&completions](EventBase&) {
completions++;
};
Defer defer(*event, std::move(callback));
EXPECT_NE(&defer, userdata);
EXPECT_FALSE(callback);
EXPECT_EQ(0, completions);
EXPECT_EQ(0, handler(nullptr, userdata));
EXPECT_EQ(1, completions);
defer.set_callback(std::bind([]() {}));
EXPECT_EQ(0, handler(nullptr, userdata));
EXPECT_EQ(1, completions);
expect_destruct();
destroy(userdata);
}
TEST_F(EventTest, PostConstruct)
{
EXPECT_CALL(mock, sd_event_ref(expected_event))
.WillOnce(Return(expected_event));
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));
}
sd_event_handler_t handler;
EXPECT_CALL(mock, sd_event_add_post(expected_event, testing::_, testing::_,
nullptr))
.WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
Return(0)));
int completions = 0;
EventBase::Callback callback = [&completions](EventBase&) {
completions++;
};
Post post(*event, std::move(callback));
EXPECT_NE(&post, userdata);
EXPECT_FALSE(callback);
EXPECT_EQ(0, completions);
EXPECT_EQ(0, handler(nullptr, userdata));
EXPECT_EQ(1, completions);
expect_destruct();
destroy(userdata);
}
TEST_F(EventTest, ExitConstruct)
{
EXPECT_CALL(mock, sd_event_ref(expected_event))
.WillOnce(Return(expected_event));
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));
}
sd_event_handler_t handler;
EXPECT_CALL(mock, sd_event_add_exit(expected_event, testing::_, testing::_,
nullptr))
.WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
Return(0)));
int completions = 0;
EventBase::Callback callback = [&completions](EventBase&) {
completions++;
};
Exit exit(*event, std::move(callback));
EXPECT_NE(&exit, userdata);
EXPECT_FALSE(callback);
EXPECT_EQ(0, completions);
EXPECT_EQ(0, handler(nullptr, userdata));
EXPECT_EQ(1, completions);
expect_destruct();
destroy(userdata);
}
TEST_F(EventTest, ConstructFailure)
{
EXPECT_CALL(mock, sd_event_add_defer(expected_event, testing::_, testing::_,
nullptr))
.WillOnce(Return(-EINVAL));
int completions = 0;
EventBase::Callback callback = [&completions](EventBase&) {
completions++;
};
EXPECT_THROW(Defer(*event, std::move(callback)), SdEventError);
EXPECT_TRUE(callback);
EXPECT_EQ(0, completions);
}
TEST_F(EventTest, CopyConstruct)
{
EXPECT_CALL(mock, sd_event_ref(expected_event))
.WillOnce(Return(expected_event));
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));
}
sd_event_handler_t handler;
EXPECT_CALL(mock, sd_event_add_exit(expected_event, testing::_, testing::_,
nullptr))
.WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<2>(&handler),
Return(0)));
auto exit = std::make_unique<Exit>(*event, [](EventBase&) {});
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 exit2 = std::make_unique<Exit>(*exit);
{
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));
Exit exit3(*exit);
expect_destruct();
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));
*exit2 = exit3;
expect_destruct();
}
// Delete the original exit
expect_destruct();
exit.reset();
// Make sure our new copy can still access data
exit2->set_callback(nullptr);
expect_destruct();
exit2.reset();
destroy(userdata);
}
} // namespace
} // namespace source
} // namespace sdeventplus