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).
23 #include <sys/epoll.h>
24 #include <gmock/gmock.h>
25 #include "private/filedescriptor.hpp"
26 #include "private/engineimpl.hpp"
27 #include "private/tst/systemmock.hpp"
29 using namespace shareddatalayer;
30 using namespace shareddatalayer::tst;
31 using namespace testing;
35 class EventHandlerMock
38 MOCK_METHOD1(handleEvents, void(unsigned int events));
41 class EngineImplTest: public testing::Test
47 NiceMock<SystemMock> systemMock;
48 std::shared_ptr<EngineImpl> services;
49 EventHandlerMock eventHandlerMock1;
50 EventHandlerMock eventHandlerMock2;
52 EngineImplTest(): fd1(100), fd2(200), epfd(300)
54 EXPECT_CALL(systemMock, epoll_create1(EPOLL_CLOEXEC))
56 .WillOnce(Return(epfd));
57 services.reset(new EngineImpl(systemMock));
58 Mock::VerifyAndClear(&systemMock);
66 void addMonitoredFD(int fd, unsigned int events)
68 services->addMonitoredFD(fd, events, [] (unsigned int) { });
71 void addMonitoredFD(int fd, unsigned int events, EventHandlerMock& eventHandlerMock)
73 services->addMonitoredFD(fd, events, std::bind(&EventHandlerMock::handleEvents, &eventHandlerMock, std::placeholders::_1));
76 void modifyMonitoredFD(int fd, unsigned int events)
78 services->modifyMonitoredFD(fd, events);
81 void deleteMonitoredFD(int fd)
83 services->deleteMonitoredFD(fd);
86 void expectEpollCtl(int epfd, int op, int fd, unsigned int events)
88 EXPECT_CALL(systemMock, epoll_ctl(epfd, op, fd, NotNull()))
90 .WillOnce(Invoke([fd, events] (int, int, int, epoll_event* event)
92 epoll_data data = { };
94 EXPECT_EQ(data.fd, event->data.fd);
95 EXPECT_EQ(data.u64, event->data.u64);
96 EXPECT_EQ(data.ptr, event->data.ptr);
97 EXPECT_EQ(events, event->events);
101 void expectAtLeastOneEpollCtl(int epfd, int op, int fd, unsigned int events)
103 EXPECT_CALL(systemMock, epoll_ctl(epfd, op, fd, NotNull()))
105 .WillOnce(Invoke([fd, events] (int, int, int, epoll_event* event)
107 EXPECT_EQ(fd, event->data.fd);
108 EXPECT_EQ(events, event->events);
112 void stopEngineImpl()
117 EXPECT_CALL(systemMock, close(epfd)).Times(1);
119 Mock::VerifyAndClear(&systemMock);
123 using EngineImplDeathTest = EngineImplTest;
126 TEST_F(EngineImplTest, HandleEventsWithoutAnyAddedFDsDoesNothing)
128 EXPECT_CALL(systemMock, epoll_wait(_, _, _, _))
130 services->handleEvents();
133 TEST_F(EngineImplTest, FDReturnsTheEpollFD)
135 EXPECT_EQ(epfd, services->fd());
138 TEST_F(EngineImplTest, AddingFDAddsTheFDToEpoll)
140 expectEpollCtl(epfd, EPOLL_CTL_ADD, fd1, Engine::EVENT_IN);
141 addMonitoredFD(fd1, Engine::EVENT_IN);
144 TEST_F(EngineImplTest, AddingFileDescriptorSetsAtCloseCallback)
146 FileDescriptor fd(systemMock, fd1);
147 expectEpollCtl(epfd, EPOLL_CTL_ADD, fd1, Engine::EVENT_IN);
148 services->addMonitoredFD(fd, Engine::EVENT_IN, Engine::EventHandler());
151 EXPECT_CALL(systemMock, epoll_ctl(epfd, EPOLL_CTL_DEL, fd1, nullptr))
153 EXPECT_CALL(systemMock, close(fd1)).Times(1);
156 TEST_F(EngineImplDeathTest, AddingAlreadyAddedFDCallsSHAREDDATALAYER_ABORT)
158 expectAtLeastOneEpollCtl(epfd, EPOLL_CTL_ADD, fd1, Engine::EVENT_IN);
159 addMonitoredFD(fd1, Engine::EVENT_IN);
160 EXPECT_EXIT(addMonitoredFD(fd1, Engine::EVENT_IN),
161 KilledBySignal(SIGABRT), "ABORT.*engineimpl\\.cpp");
164 TEST_F(EngineImplTest, ModifyingFDModifiesTheFDInEpoll)
166 addMonitoredFD(fd1, Engine::EVENT_IN);
167 expectEpollCtl(epfd, EPOLL_CTL_MOD, fd1, Engine::EVENT_OUT);
168 modifyMonitoredFD(fd1, Engine::EVENT_OUT);
171 TEST_F(EngineImplDeathTest, ModifyingNonExistingFDCallsSHAREDDATALAYER_ABORT)
173 EXPECT_EXIT(modifyMonitoredFD(fd1, 0U),
174 KilledBySignal(SIGABRT), "ABORT.*engineimpl\\.cpp");
177 TEST_F(EngineImplDeathTest, DellingFDDelsTheFDFromEpollAndFromTheMap)
179 addMonitoredFD(fd1, Engine::EVENT_IN);
180 EXPECT_CALL(systemMock, epoll_ctl(epfd, EPOLL_CTL_DEL, fd1, nullptr))
182 deleteMonitoredFD(fd1);
183 EXPECT_EXIT(modifyMonitoredFD(fd1, 0U),
184 KilledBySignal(SIGABRT), "ABORT.*engineimpl\\.cpp");
187 TEST_F(EngineImplDeathTest, DellingNonExistingFDCallsSHAREDDATALAYER_ABORT)
189 EXPECT_EXIT(deleteMonitoredFD(fd1),
190 KilledBySignal(SIGABRT), "ABORT.*engineimpl\\.cpp");
193 TEST_F(EngineImplTest, HandleEventsCallsAddedEventHandlersAccordingToEpollReturnValue)
195 addMonitoredFD(fd1, Engine::EVENT_IN, eventHandlerMock1);
196 addMonitoredFD(fd2, Engine::EVENT_IN, eventHandlerMock2);
198 EXPECT_CALL(systemMock, epoll_wait(epfd, NotNull(), 2, 0))
200 .WillOnce(Invoke([this] (int, epoll_event* events, int, int) -> int
202 events[0].events = EPOLLIN;
203 events[0].data.fd = fd1;
204 events[1].events = EPOLLOUT;
205 events[1].data.fd = fd2;
208 EXPECT_CALL(eventHandlerMock1, handleEvents(Engine::EVENT_IN))
210 EXPECT_CALL(eventHandlerMock2, handleEvents(Engine::EVENT_OUT))
212 services->handleEvents();
215 TEST_F(EngineImplTest, PendingEventsOfDeletedFileDescriptorAreForgotten)
217 addMonitoredFD(fd1, Engine::EVENT_IN, eventHandlerMock1);
218 addMonitoredFD(fd2, Engine::EVENT_IN, eventHandlerMock2);
220 EXPECT_CALL(eventHandlerMock2, handleEvents(_))
222 EXPECT_CALL(systemMock, epoll_wait(epfd, NotNull(), 2, 0))
224 .WillOnce(Invoke([this] (int, epoll_event* events, int, int) -> int
226 events[0].events = EPOLLIN;
227 events[0].data.fd = fd1;
228 events[1].events = EPOLLIN;
229 events[1].data.fd = fd2;
232 EXPECT_CALL(eventHandlerMock1, handleEvents(_))
234 .WillOnce(Invoke([this](unsigned int)
236 deleteMonitoredFD(fd2);
237 addMonitoredFD(fd2, Engine::EVENT_IN, eventHandlerMock2);
239 services->handleEvents();