2 Copyright (c) 2018-2019 Nokia.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 #include <type_traits>
20 #include <sys/eventfd.h>
21 #include <gmock/gmock.h>
22 #include "private/eventfd.hpp"
23 #include "private/timerfd.hpp"
24 #include "private/tst/systemmock.hpp"
25 #include "private/tst/enginemock.hpp"
27 using namespace shareddatalayer;
28 using namespace shareddatalayer::tst;
29 using namespace testing;
33 class EventFDTest: public testing::Test
37 NiceMock<SystemMock> systemMock;
38 EngineMock engineMock;
39 std::unique_ptr<EventFD> eventFD;
40 Engine::EventHandler savedEventHandler;
42 EventFDTest(): efd(123)
45 EXPECT_CALL(systemMock, eventfd(0U, EFD_CLOEXEC | EFD_NONBLOCK))
47 .WillOnce(Return(efd));
48 EXPECT_CALL(engineMock, addMonitoredFD(Matcher<FileDescriptor&>(_), Engine::EVENT_IN, _))
50 .WillOnce(Invoke([this] (FileDescriptor& fd, unsigned int, const Engine::EventHandler& eh)
52 EXPECT_EQ(efd, static_cast<int>(fd));
53 savedEventHandler = eh;
55 eventFD.reset(new EventFD(systemMock, engineMock));
56 Mock::VerifyAndClear(&systemMock);
57 Mock::VerifyAndClear(&engineMock);
62 EXPECT_CALL(systemMock, write(efd, NotNull(), sizeof(uint64_t)))
64 .WillOnce(Invoke([] (int, const void* buf, size_t) -> ssize_t
66 EXPECT_EQ(1U, *static_cast<const uint64_t*>(buf));
67 return sizeof(uint64_t);
73 EXPECT_CALL(systemMock, read(efd, NotNull(), sizeof(uint64_t)))
75 .WillOnce(Return(sizeof(uint64_t)));
78 void post(const EventFD::Callback& callback)
80 eventFD->post(callback);
85 post(std::bind(&EventFDTest::callback, this, i));
88 MOCK_METHOD1(callback, void(int i));
92 TEST_F(EventFDTest, IsNotCopyableAndIsNotMovable)
94 EXPECT_FALSE(std::is_copy_assignable<EventFD>::value);
95 EXPECT_FALSE(std::is_move_assignable<EventFD>::value);
96 EXPECT_FALSE(std::is_copy_constructible<EventFD>::value);
97 EXPECT_FALSE(std::is_move_constructible<EventFD>::value);
100 TEST_F(EventFDTest, PostWritesToEventFD)
106 TEST_F(EventFDTest, HandleEventsExecutesAllCallbacksInFIFOOrder)
112 EXPECT_CALL(*this, callback(1))
114 EXPECT_CALL(*this, callback(2))
116 savedEventHandler(Engine::EVENT_IN);
119 TEST_F(EventFDTest, CallbacksAddedInPostAreNotExecutedDuringTheSameHandleEvents)
121 post([this] () { post(1); });
124 EXPECT_CALL(*this, callback(_))
126 savedEventHandler(Engine::EVENT_IN);
129 TEST_F(EventFDTest, ExecutedCallbackIsDestroyedBeforeExecutingTheNextCallback)
131 std::shared_ptr<int> data(std::make_shared<int>(1));
132 std::weak_ptr<int> weak(data);
133 post([data] () { static_cast<void>(data); });
135 post([weak] () { EXPECT_EQ(nullptr, weak.lock()); });
136 savedEventHandler(Engine::EVENT_IN);
139 TEST_F(EventFDTest, PostingNullCallbackCallsSHAREDDATALAYER_ABORT)
141 EXPECT_EXIT(post(EventFD::Callback()),
142 KilledBySignal(SIGABRT), "ABORT.*eventfd\\.cpp");