2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Piotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
22 * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
23 * @brief Unit tests of event poll
29 #include "epoll/event-poll.hpp"
30 #include "logger/logger.hpp"
31 #include "ipc/internals/socket.hpp"
32 #include "utils/latch.hpp"
33 #include "utils/glib-loop.hpp"
34 #include "epoll/glib-poll-dispatcher.hpp"
35 #include "epoll/thread-poll-dispatcher.hpp"
37 using namespace vasum::utils;
38 using namespace vasum::epoll;
39 using namespace vasum::ipc;
43 const int unsigned TIMEOUT = 1000;
47 BOOST_AUTO_TEST_SUITE(EventPollSuite)
49 BOOST_AUTO_TEST_CASE(EmptyPoll)
52 BOOST_CHECK(!poll.dispatchIteration(0));
55 BOOST_AUTO_TEST_CASE(ThreadedPoll)
57 ThreadPollDispatcher dispatcher;
60 BOOST_AUTO_TEST_CASE(GlibPoll)
64 GlibPollDispatcher dispatcher;
67 void doSocketTest(EventPoll& poll)
69 const std::string PATH = "/tmp/ut-poll.sock";
70 const std::string MESSAGE = "This is a test message";
75 Socket listen = Socket::createSocket(PATH);
76 std::shared_ptr<Socket> server;
78 auto serverCallback = [&](int, Events events) -> bool {
79 LOGD("Server events: " << eventsToString(events));
81 if (events & EPOLLOUT) {
82 server->write(MESSAGE.data(), MESSAGE.size());
83 poll.removeFD(server->getFD());
89 auto listenCallback = [&](int, Events events) -> bool {
90 LOGD("Listen events: " << eventsToString(events));
91 if (events & EPOLLIN) {
92 server = listen.accept();
93 poll.addFD(server->getFD(), EPOLLHUP | EPOLLRDHUP | EPOLLOUT, serverCallback);
98 poll.addFD(listen.getFD(), EPOLLIN, listenCallback);
100 Socket client = Socket::connectSocket(PATH);
102 auto clientCallback = [&](int, Events events) -> bool {
103 LOGD("Client events: " << eventsToString(events));
105 if (events & EPOLLIN) {
106 std::string ret(MESSAGE.size(), 'x');
107 client.read(&ret.front(), ret.size());
108 if (ret == MESSAGE) {
112 if (events & EPOLLRDHUP) {
113 poll.removeFD(client.getFD());
119 poll.addFD(client.getFD(), EPOLLHUP | EPOLLRDHUP | EPOLLIN, clientCallback);
121 BOOST_CHECK(goodMessage.wait(TIMEOUT));
122 BOOST_CHECK(remoteClosed.wait(TIMEOUT));
124 poll.removeFD(listen.getFD());
127 BOOST_AUTO_TEST_CASE(ThreadedPollSocket)
129 ThreadPollDispatcher dispatcher;
131 doSocketTest(dispatcher.getPoll());
134 BOOST_AUTO_TEST_CASE(GlibPollSocket)
138 GlibPollDispatcher dispatcher;
140 doSocketTest(dispatcher.getPoll());
143 BOOST_AUTO_TEST_CASE(PollStacking)
145 ThreadPollDispatcher dispatcher;
149 auto dispatchInner = [&](int, Events) -> bool {
150 innerPoll.dispatchIteration(0);
153 dispatcher.getPoll().addFD(innerPoll.getPollFD(), EPOLLIN, dispatchInner);
154 doSocketTest(innerPoll);
155 dispatcher.getPoll().removeFD(innerPoll.getPollFD());
158 BOOST_AUTO_TEST_SUITE_END()