William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 1 | #include <cerrno> |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 2 | #include <functional> |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 3 | #include <gmock/gmock.h> |
| 4 | #include <gtest/gtest.h> |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 5 | #include <memory> |
William A. Kennington III | e04cb03 | 2018-11-13 14:28:36 -0800 | [diff] [blame] | 6 | #include <optional> |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 7 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 8 | #include <sdeventplus/exception.hpp> |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 9 | #include <sdeventplus/internal/sdevent.hpp> |
| 10 | #include <sdeventplus/source/base.hpp> |
| 11 | #include <sdeventplus/test/sdevent.hpp> |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 12 | #include <sdeventplus/types.hpp> |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 13 | #include <string> |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 14 | #include <systemd/sd-event.h> |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 15 | #include <tuple> |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 16 | #include <type_traits> |
William A. Kennington III | cc6b12b | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 17 | #include <utility> |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 18 | |
| 19 | namespace sdeventplus |
| 20 | { |
| 21 | namespace source |
| 22 | { |
| 23 | namespace |
| 24 | { |
| 25 | |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 26 | using testing::DoAll; |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 27 | using testing::Return; |
William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 28 | using testing::SaveArg; |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 29 | using testing::SetArgPointee; |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 30 | |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 31 | using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>; |
| 32 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 33 | class BaseImplData; |
| 34 | |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 35 | class BaseImpl : public Base |
| 36 | { |
| 37 | public: |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 38 | BaseImpl(const Event& event, sd_event_source* source, std::false_type); |
| 39 | |
| 40 | BaseImpl(const BaseImpl& other, sdeventplus::internal::NoOwn) : |
| 41 | Base(other, sdeventplus::internal::NoOwn()) |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 42 | { |
| 43 | } |
William A. Kennington III | e0f1d19 | 2018-07-17 17:00:56 -0700 | [diff] [blame] | 44 | |
| 45 | using Base::get_prepare; |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 48 | class BaseImplData : public BaseImpl, public detail::BaseData |
| 49 | { |
| 50 | public: |
| 51 | BaseImplData(const BaseImpl& base) : |
| 52 | BaseImpl(base, sdeventplus::internal::NoOwn()), BaseData(base) |
| 53 | { |
| 54 | } |
| 55 | }; |
| 56 | |
| 57 | BaseImpl::BaseImpl(const Event& event, sd_event_source* source, |
| 58 | std::false_type) : |
| 59 | Base(event, source, std::false_type()) |
| 60 | { |
| 61 | set_userdata(std::make_unique<BaseImplData>(*this)); |
| 62 | } |
| 63 | |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 64 | class BaseTest : public testing::Test |
| 65 | { |
| 66 | protected: |
| 67 | testing::StrictMock<test::SdEventMock> mock; |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 68 | sd_event_source* const expected_source = |
| 69 | reinterpret_cast<sd_event_source*>(1234); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 70 | sd_event_source* const expected_source2 = |
| 71 | reinterpret_cast<sd_event_source*>(3456); |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 72 | sd_event* const expected_event = reinterpret_cast<sd_event*>(2345); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 73 | sd_event* const expected_event2 = reinterpret_cast<sd_event*>(4567); |
| 74 | |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 75 | UniqueEvent event = make_event(expected_event); |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 76 | |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 77 | UniqueEvent make_event(sd_event* event) |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 78 | { |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 79 | auto deleter = [this, event](Event* e) { |
| 80 | EXPECT_CALL(this->mock, sd_event_unref(event)) |
| 81 | .WillOnce(Return(nullptr)); |
| 82 | delete e; |
| 83 | }; |
| 84 | return UniqueEvent(new Event(event, std::false_type(), &mock), deleter); |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 85 | } |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 86 | |
| 87 | // Using a unique_ptr to make sure we don't get any superfluous moves or |
| 88 | // copies. |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 89 | std::tuple<std::unique_ptr<BaseImpl>, std::function<void()>> |
| 90 | make_base(const Event& event, sd_event_source* source) |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 91 | { |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 92 | EXPECT_CALL(mock, sd_event_ref(event.get())) |
| 93 | .WillOnce(Return(event.get())); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 94 | sd_event_destroy_t destroy; |
William A. Kennington III | 2d943ea | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 95 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 96 | { |
| 97 | testing::InSequence seq; |
| 98 | EXPECT_CALL( |
| 99 | mock, sd_event_source_set_destroy_callback(source, testing::_)) |
| 100 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 101 | EXPECT_CALL(mock, sd_event_source_set_userdata(source, testing::_)) |
| 102 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 103 | } |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 104 | auto ret = std::make_unique<BaseImpl>(event, source, std::false_type()); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 105 | EXPECT_CALL(mock, sd_event_source_get_userdata(source)) |
| 106 | .WillRepeatedly(Return(userdata)); |
| 107 | EXPECT_NE(ret.get(), userdata); |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 108 | EXPECT_EQ(source, ret->get()); |
| 109 | EXPECT_NE(&event, &ret->get_event()); |
| 110 | EXPECT_EQ(event.get(), ret->get_event().get()); |
| 111 | EXPECT_FALSE(ret->get_prepare()); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 112 | return {std::move(ret), std::bind(destroy, userdata)}; |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 113 | } |
| 114 | |
William A. Kennington III | e0f1d19 | 2018-07-17 17:00:56 -0700 | [diff] [blame] | 115 | void set_prepare_placeholder(BaseImpl& base) |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 116 | { |
| 117 | EXPECT_CALL(mock, sd_event_source_set_prepare(base.get(), testing::_)) |
| 118 | .WillOnce(Return(0)); |
| 119 | base.set_prepare([](Base&) {}); |
| 120 | EXPECT_TRUE(base.get_prepare()); |
| 121 | } |
| 122 | |
| 123 | void empty_base(BaseImpl&& other) |
| 124 | { |
| 125 | BaseImpl mover(std::move(other)); |
William A. Kennington III | e04cb03 | 2018-11-13 14:28:36 -0800 | [diff] [blame] | 126 | EXPECT_THROW(other.get(), std::bad_optional_access); |
| 127 | EXPECT_THROW(other.get_event().get(), std::bad_optional_access); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 128 | EXPECT_THROW(other.get_prepare(), std::bad_optional_access); |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 129 | |
| 130 | expect_base_destruct(mover.get_event(), mover.get()); |
| 131 | } |
| 132 | |
| 133 | void expect_base_destruct(const Event& event, sd_event_source* source) |
| 134 | { |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 135 | EXPECT_CALL(mock, sd_event_source_unref(source)) |
| 136 | .WillOnce(Return(nullptr)); |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 137 | EXPECT_CALL(mock, sd_event_unref(event.get())) |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 138 | .WillOnce(Return(nullptr)); |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 139 | } |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 142 | TEST_F(BaseTest, NewBaseFail) |
| 143 | { |
| 144 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 145 | .WillOnce(Return(expected_event)); |
| 146 | EXPECT_CALL( |
| 147 | mock, sd_event_source_set_destroy_callback(expected_source, testing::_)) |
| 148 | .WillOnce(Return(-EINVAL)); |
| 149 | expect_base_destruct(*event, expected_source); |
| 150 | EXPECT_THROW(BaseImpl(*event, expected_source, std::false_type()), |
| 151 | SdEventError); |
| 152 | } |
| 153 | |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 154 | TEST_F(BaseTest, NewBaseNoRef) |
| 155 | { |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 156 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 157 | .WillOnce(Return(expected_event)); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 158 | sd_event_destroy_t destroy; |
William A. Kennington III | 2d943ea | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 159 | void* userdata; |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 160 | { |
| 161 | testing::InSequence seq; |
| 162 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 163 | testing::_)) |
| 164 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
| 165 | EXPECT_CALL(mock, |
| 166 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 167 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
| 168 | } |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 169 | BaseImpl source(*event, expected_source, std::false_type()); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 170 | EXPECT_NE(&source, userdata); |
William A. Kennington III | 4863b96 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 171 | EXPECT_EQ(expected_source, source.get()); |
| 172 | EXPECT_NE(event.get(), &source.get_event()); |
| 173 | EXPECT_EQ(expected_event, source.get_event().get()); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 174 | |
| 175 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 176 | .WillOnce(Return(userdata)); |
William A. Kennington III | f5285c7 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 177 | EXPECT_FALSE(source.get_prepare()); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 178 | |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 179 | expect_base_destruct(*event, expected_source); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 180 | destroy(userdata); |
William A. Kennington III | 7c24e05 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 181 | } |
| 182 | |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 183 | TEST_F(BaseTest, UserdataOutlives) |
William A. Kennington III | cc6b12b | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 184 | { |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 185 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 186 | .WillOnce(Return(expected_event)); |
| 187 | sd_event_destroy_t destroy; |
William A. Kennington III | 2d943ea | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 188 | void* userdata; |
William A. Kennington III | cc6b12b | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 189 | { |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 190 | testing::InSequence seq; |
| 191 | EXPECT_CALL(mock, sd_event_source_set_destroy_callback(expected_source, |
| 192 | testing::_)) |
| 193 | .WillOnce(DoAll(SaveArg<1>(&destroy), Return(0))); |
William A. Kennington III | 2d943ea | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 194 | EXPECT_CALL(mock, |
| 195 | sd_event_source_set_userdata(expected_source, testing::_)) |
| 196 | .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr))); |
William A. Kennington III | cc6b12b | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 197 | } |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 198 | auto source = |
| 199 | std::make_unique<BaseImpl>(*event, expected_source, std::false_type()); |
| 200 | EXPECT_CALL(mock, sd_event_source_get_userdata(expected_source)) |
| 201 | .WillRepeatedly(Return(userdata)); |
| 202 | EXPECT_FALSE(source->get_prepare()); |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 203 | |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 204 | expect_base_destruct(*event, expected_source); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 205 | source.reset(); |
| 206 | EXPECT_FALSE(reinterpret_cast<BaseImpl*>(userdata)->get_prepare()); |
| 207 | destroy(userdata); |
William A. Kennington III | cc6b12b | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 208 | } |
| 209 | |
William A. Kennington III | 56dc78b | 2019-03-29 21:09:13 -0700 | [diff] [blame] | 210 | TEST_F(BaseTest, CopyCorrectness) |
| 211 | { |
| 212 | std::unique_ptr<BaseImpl> base1, base2; |
| 213 | std::function<void()> destroy; |
| 214 | std::tie(base1, destroy) = make_base(*event, expected_source); |
| 215 | set_prepare_placeholder(*base1); |
| 216 | EXPECT_TRUE(base1->get_prepare()); |
| 217 | |
| 218 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 219 | .WillOnce(Return(expected_event)); |
| 220 | EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| 221 | .WillOnce(Return(expected_source)); |
| 222 | base2 = std::make_unique<BaseImpl>(*base1); |
| 223 | EXPECT_EQ(&base1->get_prepare(), &base2->get_prepare()); |
| 224 | |
| 225 | empty_base(std::move(*base1)); |
| 226 | EXPECT_THROW(base1->get_prepare(), std::bad_optional_access); |
| 227 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 228 | .WillOnce(Return(expected_event)); |
| 229 | EXPECT_CALL(mock, sd_event_source_ref(expected_source)) |
| 230 | .WillOnce(Return(expected_source)); |
| 231 | *base1 = *base2; |
| 232 | EXPECT_EQ(&base1->get_prepare(), &base2->get_prepare()); |
| 233 | |
| 234 | expect_base_destruct(*event, expected_source); |
| 235 | base2.reset(); |
| 236 | expect_base_destruct(*event, expected_source); |
| 237 | base1.reset(); |
| 238 | destroy(); |
| 239 | } |
| 240 | |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 241 | class BaseMethodTest : public BaseTest |
| 242 | { |
| 243 | protected: |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 244 | std::unique_ptr<BaseImpl> base; |
| 245 | std::function<void()> destroy; |
| 246 | |
| 247 | void SetUp() |
| 248 | { |
| 249 | std::tie(base, destroy) = make_base(*event, expected_source); |
| 250 | } |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 251 | |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 252 | void TearDown() |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 253 | { |
William A. Kennington III | e3e1df0 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 254 | expect_base_destruct(base->get_event(), base->get()); |
William A. Kennington III | 715c72f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 255 | base.reset(); |
William A. Kennington III | 5320b1f | 2019-03-29 20:00:37 -0700 | [diff] [blame] | 256 | destroy(); |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 257 | } |
| 258 | }; |
| 259 | |
| 260 | TEST_F(BaseMethodTest, GetDescriptionSuccess) |
| 261 | { |
| 262 | const char* expected = "test_desc"; |
| 263 | EXPECT_CALL(mock, |
| 264 | sd_event_source_get_description(expected_source, testing::_)) |
| 265 | .WillOnce(DoAll(SetArgPointee<1>(expected), Return(0))); |
| 266 | // Intentionally comparing pointers to make sure no copying is happening |
| 267 | EXPECT_EQ(expected, base->get_description()); |
| 268 | } |
| 269 | |
| 270 | TEST_F(BaseMethodTest, GetDescriptionError) |
| 271 | { |
| 272 | EXPECT_CALL(mock, |
| 273 | sd_event_source_get_description(expected_source, testing::_)) |
| 274 | .WillOnce(Return(-EINVAL)); |
| 275 | EXPECT_THROW(base->get_description(), SdEventError); |
| 276 | } |
| 277 | |
| 278 | TEST_F(BaseMethodTest, SetDescriptionSuccess) |
| 279 | { |
| 280 | const char* expected = "test desc"; |
| 281 | // Intentionally comparing pointers to make sure no copying is happening |
| 282 | EXPECT_CALL(mock, |
| 283 | sd_event_source_set_description(expected_source, expected)) |
| 284 | .WillOnce(Return(0)); |
| 285 | base->set_description(expected); |
| 286 | } |
| 287 | |
| 288 | TEST_F(BaseMethodTest, SetDescriptionError) |
| 289 | { |
| 290 | const char* expected = "test desc"; |
| 291 | // Intentionally comparing pointers to make sure no copying is happening |
| 292 | EXPECT_CALL(mock, |
| 293 | sd_event_source_set_description(expected_source, expected)) |
| 294 | .WillOnce(Return(-EINVAL)); |
| 295 | EXPECT_THROW(base->set_description(expected), SdEventError); |
| 296 | } |
| 297 | |
William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 298 | TEST_F(BaseMethodTest, SetPrepareCallback) |
| 299 | { |
| 300 | bool completed = false; |
| 301 | Base::Callback callback = [&completed](Base&) { completed = true; }; |
| 302 | sd_event_handler_t event_handler; |
| 303 | EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) |
| 304 | .WillOnce(DoAll(SaveArg<1>(&event_handler), Return(0))); |
| 305 | base->set_prepare(std::move(callback)); |
William A. Kennington III | f5285c7 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 306 | EXPECT_TRUE(base->get_prepare()); |
William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 307 | EXPECT_FALSE(callback); |
| 308 | EXPECT_FALSE(completed); |
| 309 | |
| 310 | EXPECT_EQ(0, event_handler(nullptr, base.get())); |
| 311 | EXPECT_TRUE(completed); |
| 312 | } |
| 313 | |
| 314 | TEST_F(BaseMethodTest, SetPrepareCallbackNoUserdata) |
| 315 | { |
| 316 | Base::Callback callback = [](Base&) {}; |
| 317 | sd_event_handler_t event_handler; |
| 318 | EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) |
| 319 | .WillOnce(DoAll(SaveArg<1>(&event_handler), Return(0))); |
| 320 | base->set_prepare(std::move(callback)); |
William A. Kennington III | f5285c7 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 321 | EXPECT_TRUE(base->get_prepare()); |
William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 322 | EXPECT_FALSE(callback); |
| 323 | |
| 324 | EXPECT_EQ(-EINVAL, event_handler(nullptr, nullptr)); |
| 325 | } |
| 326 | |
William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 327 | TEST_F(BaseMethodTest, SetPrepareError) |
| 328 | { |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 329 | EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) |
| 330 | .WillOnce(Return(0)); |
William A. Kennington III | 651e87a | 2018-12-11 16:45:37 -0800 | [diff] [blame] | 331 | base->set_prepare([](Base&) {}); |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 332 | EXPECT_TRUE(base->get_prepare()); |
| 333 | |
William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 334 | Base::Callback callback = [](Base&) {}; |
| 335 | EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) |
| 336 | .WillOnce(Return(-EINVAL)); |
| 337 | EXPECT_THROW(base->set_prepare(std::move(callback)), SdEventError); |
William A. Kennington III | f5285c7 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 338 | EXPECT_FALSE(base->get_prepare()); |
William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 339 | EXPECT_TRUE(callback); |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST_F(BaseMethodTest, SetPrepareNull) |
| 343 | { |
| 344 | EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, testing::_)) |
| 345 | .WillOnce(Return(0)); |
William A. Kennington III | 651e87a | 2018-12-11 16:45:37 -0800 | [diff] [blame] | 346 | base->set_prepare([](Base&) {}); |
William A. Kennington III | feef68f | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 347 | EXPECT_TRUE(base->get_prepare()); |
| 348 | |
| 349 | EXPECT_CALL(mock, sd_event_source_set_prepare(expected_source, nullptr)) |
| 350 | .WillOnce(Return(0)); |
| 351 | base->set_prepare(nullptr); |
| 352 | EXPECT_FALSE(base->get_prepare()); |
William A. Kennington III | 48c4275 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 353 | } |
| 354 | |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 355 | TEST_F(BaseMethodTest, GetPendingSuccess) |
| 356 | { |
| 357 | EXPECT_CALL(mock, sd_event_source_get_pending(expected_source)) |
| 358 | .WillOnce(Return(0)); |
William A. Kennington III | 8fd0cd4 | 2018-07-23 18:33:04 -0700 | [diff] [blame] | 359 | EXPECT_FALSE(base->get_pending()); |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 360 | EXPECT_CALL(mock, sd_event_source_get_pending(expected_source)) |
| 361 | .WillOnce(Return(4)); |
William A. Kennington III | 8fd0cd4 | 2018-07-23 18:33:04 -0700 | [diff] [blame] | 362 | EXPECT_TRUE(base->get_pending()); |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | TEST_F(BaseMethodTest, GetPendingError) |
| 366 | { |
| 367 | EXPECT_CALL(mock, sd_event_source_get_pending(expected_source)) |
| 368 | .WillOnce(Return(-EINVAL)); |
| 369 | EXPECT_THROW(base->get_pending(), SdEventError); |
| 370 | } |
| 371 | |
| 372 | TEST_F(BaseMethodTest, GetPrioritySuccess) |
| 373 | { |
| 374 | EXPECT_CALL(mock, sd_event_source_get_priority(expected_source, testing::_)) |
| 375 | .WillOnce(DoAll(SetArgPointee<1>(1024), Return(0))); |
| 376 | EXPECT_EQ(1024, base->get_priority()); |
| 377 | } |
| 378 | |
| 379 | TEST_F(BaseMethodTest, GetPriorityError) |
| 380 | { |
| 381 | EXPECT_CALL(mock, sd_event_source_get_priority(expected_source, testing::_)) |
| 382 | .WillOnce(Return(-EINVAL)); |
| 383 | EXPECT_THROW(base->get_priority(), SdEventError); |
| 384 | } |
| 385 | |
| 386 | TEST_F(BaseMethodTest, SetPrioritySuccess) |
| 387 | { |
| 388 | EXPECT_CALL(mock, sd_event_source_set_priority(expected_source, 1024)) |
| 389 | .WillOnce(Return(0)); |
| 390 | base->set_priority(1024); |
| 391 | } |
| 392 | |
| 393 | TEST_F(BaseMethodTest, SetPriorityError) |
| 394 | { |
| 395 | EXPECT_CALL(mock, sd_event_source_set_priority(expected_source, 1024)) |
| 396 | .WillOnce(Return(-EINVAL)); |
| 397 | EXPECT_THROW(base->set_priority(1024), SdEventError); |
| 398 | } |
| 399 | |
| 400 | TEST_F(BaseMethodTest, GetEnabledSuccess) |
| 401 | { |
| 402 | EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_)) |
| 403 | .WillOnce(DoAll(SetArgPointee<1>(SD_EVENT_ON), Return(0))); |
William A. Kennington III | 8fd0cd4 | 2018-07-23 18:33:04 -0700 | [diff] [blame] | 404 | EXPECT_EQ(Enabled::On, base->get_enabled()); |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | TEST_F(BaseMethodTest, GetEnabledError) |
| 408 | { |
| 409 | EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_)) |
| 410 | .WillOnce(Return(-EINVAL)); |
| 411 | EXPECT_THROW(base->get_enabled(), SdEventError); |
| 412 | } |
| 413 | |
| 414 | TEST_F(BaseMethodTest, SetEnabledSuccess) |
| 415 | { |
| 416 | EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source, SD_EVENT_ON)) |
| 417 | .WillOnce(Return(0)); |
William A. Kennington III | 8fd0cd4 | 2018-07-23 18:33:04 -0700 | [diff] [blame] | 418 | base->set_enabled(Enabled::On); |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | TEST_F(BaseMethodTest, SetEnabledError) |
| 422 | { |
William A. Kennington III | 8fd0cd4 | 2018-07-23 18:33:04 -0700 | [diff] [blame] | 423 | EXPECT_CALL(mock, |
| 424 | sd_event_source_set_enabled(expected_source, SD_EVENT_ONESHOT)) |
William A. Kennington III | 65db863 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 425 | .WillOnce(Return(-EINVAL)); |
William A. Kennington III | 8fd0cd4 | 2018-07-23 18:33:04 -0700 | [diff] [blame] | 426 | EXPECT_THROW(base->set_enabled(Enabled::OneShot), SdEventError); |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 427 | } |
| 428 | |
William A. Kennington III | 43493b5 | 2019-03-29 21:29:04 -0700 | [diff] [blame] | 429 | TEST_F(BaseMethodTest, GetFloatingSuccess) |
| 430 | { |
| 431 | EXPECT_CALL(mock, sd_event_source_get_floating(expected_source)) |
| 432 | .WillOnce(Return(2)); |
| 433 | EXPECT_TRUE(base->get_floating()); |
| 434 | EXPECT_CALL(mock, sd_event_source_get_floating(expected_source)) |
| 435 | .WillOnce(Return(0)); |
| 436 | EXPECT_FALSE(base->get_floating()); |
| 437 | } |
| 438 | |
| 439 | TEST_F(BaseMethodTest, GetFloatingError) |
| 440 | { |
| 441 | EXPECT_CALL(mock, sd_event_source_get_floating(expected_source)) |
| 442 | .WillOnce(Return(-EINVAL)); |
| 443 | EXPECT_THROW(base->get_floating(), SdEventError); |
| 444 | } |
| 445 | |
| 446 | TEST_F(BaseMethodTest, SetFloatingSuccess) |
| 447 | { |
| 448 | EXPECT_CALL(mock, sd_event_source_set_floating(expected_source, 1)) |
| 449 | .WillOnce(Return(0)); |
| 450 | base->set_floating(true); |
| 451 | } |
| 452 | |
| 453 | TEST_F(BaseMethodTest, SetFloatingError) |
| 454 | { |
| 455 | EXPECT_CALL(mock, sd_event_source_set_floating(expected_source, 1)) |
| 456 | .WillOnce(Return(-EINVAL)); |
| 457 | EXPECT_THROW(base->set_floating(true), SdEventError); |
| 458 | } |
| 459 | |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 460 | } // namespace |
| 461 | } // namespace source |
| 462 | } // namespace sdeventplus |