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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
22 #include <type_traits>
25 #include <sys/eventfd.h>
26 #include <gmock/gmock.h>
27 #include "private/eventfd.hpp"
28 #include "private/timerfd.hpp"
29 #include "private/tst/systemmock.hpp"
30 #include "private/tst/enginemock.hpp"
32 using namespace shareddatalayer;
33 using namespace shareddatalayer::tst;
34 using namespace testing;
38 class EventFDTest: public testing::Test
42 NiceMock<SystemMock> systemMock;
43 EngineMock engineMock;
44 std::unique_ptr<EventFD> eventFD;
45 Engine::EventHandler savedEventHandler;
47 EventFDTest(): efd(123)
50 EXPECT_CALL(systemMock, eventfd(0U, EFD_CLOEXEC | EFD_NONBLOCK))
52 .WillOnce(Return(efd));
53 EXPECT_CALL(engineMock, addMonitoredFD(Matcher<FileDescriptor&>(_), Engine::EVENT_IN, _))
55 .WillOnce(Invoke([this] (FileDescriptor& fd, unsigned int, const Engine::EventHandler& eh)
57 EXPECT_EQ(efd, static_cast<int>(fd));
58 savedEventHandler = eh;
60 eventFD.reset(new EventFD(systemMock, engineMock));
61 Mock::VerifyAndClear(&systemMock);
62 Mock::VerifyAndClear(&engineMock);
67 EXPECT_CALL(systemMock, write(efd, NotNull(), sizeof(uint64_t)))
69 .WillOnce(Invoke([] (int, const void* buf, size_t) -> ssize_t
71 EXPECT_EQ(1U, *static_cast<const uint64_t*>(buf));
72 return sizeof(uint64_t);
78 EXPECT_CALL(systemMock, read(efd, NotNull(), sizeof(uint64_t)))
80 .WillOnce(Return(sizeof(uint64_t)));
83 void post(const EventFD::Callback& callback)
85 eventFD->post(callback);
90 post(std::bind(&EventFDTest::callback, this, i));
93 MOCK_METHOD1(callback, void(int i));
97 TEST_F(EventFDTest, IsNotCopyableAndIsNotMovable)
99 EXPECT_FALSE(std::is_copy_assignable<EventFD>::value);
100 EXPECT_FALSE(std::is_move_assignable<EventFD>::value);
101 EXPECT_FALSE(std::is_copy_constructible<EventFD>::value);
102 EXPECT_FALSE(std::is_move_constructible<EventFD>::value);
105 TEST_F(EventFDTest, PostWritesToEventFD)
111 TEST_F(EventFDTest, HandleEventsExecutesAllCallbacksInFIFOOrder)
117 EXPECT_CALL(*this, callback(1))
119 EXPECT_CALL(*this, callback(2))
121 savedEventHandler(Engine::EVENT_IN);
124 TEST_F(EventFDTest, CallbacksAddedInPostAreNotExecutedDuringTheSameHandleEvents)
126 post([this] () { post(1); });
129 EXPECT_CALL(*this, callback(_))
131 savedEventHandler(Engine::EVENT_IN);
134 TEST_F(EventFDTest, ExecutedCallbackIsDestroyedBeforeExecutingTheNextCallback)
136 std::shared_ptr<int> data(std::make_shared<int>(1));
137 std::weak_ptr<int> weak(data);
138 post([data] () { static_cast<void>(data); });
140 post([weak] () { EXPECT_EQ(nullptr, weak.lock()); });
141 savedEventHandler(Engine::EVENT_IN);
144 TEST_F(EventFDTest, PostingNullCallbackCallsSHAREDDATALAYER_ABORT)
146 EXPECT_EXIT(post(EventFD::Callback()),
147 KilledBySignal(SIGABRT), "ABORT.*eventfd\\.cpp");