4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief FdEvent unit tests */
26 //#include "FdEvent.test.hh"
27 //#include "FdEvent.test.ih"
30 #include <sys/types.h>
35 #include <sys/socket.h>
43 #include <boost/bind.hpp>
45 #include "../Utils/auto_unit_test.hh"
46 #include <boost/test/test_tools.hpp>
49 ///////////////////////////////cc.p////////////////////////////////////////
53 char const * SOCK_PATH = "/tmp/sched_test.sock";
55 void error(char const * fn, char const * proc="")
57 std::cerr << "\n" << proc << fn << ": " << strerror(errno) << std::endl;
60 void fail(char const * fn)
68 int sock = socket(PF_UNIX,SOCK_STREAM,0);
69 if (sock<0) fail("socket");
70 struct sockaddr_un sun;
71 memset(&sun,0,sizeof(sun));
72 sun.sun_family = AF_UNIX;
73 strcpy(sun.sun_path,SOCK_PATH);
74 if (bind(sock,(struct sockaddr*)&sun,sizeof(sun))<0) fail("bind");
75 if (listen(sock,1)<0) fail("listen");
76 int conn = accept(sock,0,0);
77 if (conn < 0) fail("accept");
79 ///////////////////////////////////////////////////////////////////////////
81 if (write(conn,"READ",4)<0) fail("write");
83 int size = read(conn,buffer,1024);
84 if (size<0) fail("read");
87 if (strcmp(buffer,"WRITE")==0) {
88 if (write(conn,"OK",2)<0) fail("write");
90 if (write(conn,"FAIL",4)<0) fail("write");
92 if (write(conn,"FAIL",4)<0) fail("write");
94 ///////////////////////////////////////////////////////////////////////////
105 signal(SIGCHLD, SIG_IGN);
113 signal(SIGCHLD, SIG_DFL);
115 sleep(1); // Wait for the server socket to be opened
119 bool stop_server(int pid)
121 sleep(1); // Wait for the server to terminate
122 if (kill(pid,SIGTERM)<0) {
127 if (waitpid(pid,&status,0)<0) {
132 if (WIFSIGNALED(status)) {
133 std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
136 if (WEXITSTATUS(status)!=0) {
137 std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
148 void callback(int fd, int ev)
152 switch (event & senf::scheduler::FdEvent::EV_ALL) {
153 case senf::scheduler::FdEvent::EV_READ:
154 size = recv(fd,buffer,1024,0);
156 case senf::scheduler::FdEvent::EV_PRIO:
157 size = recv(fd,buffer,1024,MSG_OOB);
159 case senf::scheduler::FdEvent::EV_WRITE:
160 size = write(fd,buffer,size);
167 BOOST_AUTO_UNIT_TEST(fdDispatcher)
169 senf::scheduler::detail::FdManager::instance().timeout(1000);
171 int pid (start_server());
172 BOOST_REQUIRE( pid );
174 int sock = socket(PF_UNIX,SOCK_STREAM,0);
177 BOOST_FAIL("socket");
179 struct sockaddr_un sun;
180 memset(&sun,0,sizeof(sun));
181 sun.sun_family = AF_UNIX;
182 strcpy(sun.sun_path,SOCK_PATH);
184 if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
186 BOOST_FAIL("connect");
190 senf::scheduler::FdEvent sockread ("testHandler", boost::bind(&callback, sock, _1),
191 sock, senf::scheduler::FdEvent::EV_READ);
192 senf::scheduler::FdEvent sockwrite ("testHandler", boost::bind(&callback, sock, _1),
193 sock, senf::scheduler::FdEvent::EV_WRITE, false);
195 SENF_CHECK_NO_THROW( senf::scheduler::detail::FdManager::instance().processOnce() );
196 SENF_CHECK_NO_THROW( senf::scheduler::detail::FIFORunner::instance().run() );
197 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ );
198 BOOST_CHECK_EQUAL( size, 4 );
200 BOOST_CHECK_EQUAL( buffer, "READ" );
202 strcpy(buffer,"WRITE");
204 SENF_CHECK_NO_THROW( sockwrite.enable() );
207 SENF_CHECK_NO_THROW( senf::scheduler::detail::FdManager::instance().processOnce() );
208 SENF_CHECK_NO_THROW( senf::scheduler::detail::FIFORunner::instance().run() );
209 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_WRITE );
211 SENF_CHECK_NO_THROW( sockwrite.disable() );
214 SENF_CHECK_NO_THROW( senf::scheduler::detail::FdManager::instance().processOnce() );
215 SENF_CHECK_NO_THROW( senf::scheduler::detail::FIFORunner::instance().run() );
216 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_HUP | senf::scheduler::FdEvent::EV_READ );
217 BOOST_CHECK_EQUAL( size, 2 );
219 BOOST_CHECK_EQUAL( buffer, "OK" );
221 BOOST_CHECK_EQUAL( calls, 3 );
222 SENF_CHECK_NO_THROW( senf::scheduler::detail::FIFORunner::instance().run() );
223 BOOST_CHECK_EQUAL( calls, 3 );
225 // Ensure, removing an already closed file-descriptor doesn't wreak havoc
229 SENF_CHECK_NO_THROW( senf::scheduler::detail::FIFORunner::instance().run() );
230 BOOST_CHECK_EQUAL( calls, 3 );
232 BOOST_CHECK (stop_server(pid));
237 bool is_close(senf::ClockService::clock_type a, senf::ClockService::clock_type b)
239 return (a<b ? b-a : a-b) < senf::ClockService::milliseconds(100);
243 void handler(int events)
249 BOOST_AUTO_UNIT_TEST(fileDispatcher)
251 senf::scheduler::detail::FileDispatcher::instance().timeout(500);
253 int fd (open("test.empty.file", O_RDWR|O_CREAT|O_TRUNC, 0600));
255 senf::ClockService::clock_type t (senf::ClockService::now());
257 senf::scheduler::FdEvent fde ("testHandler", &handler,
258 fd, senf::scheduler::FdEvent::EV_READ);
259 SENF_CHECK_NO_THROW( senf::scheduler::detail::FdManager::instance().processOnce() );
260 SENF_CHECK_NO_THROW( senf::scheduler::detail::FileDispatcher::instance().prepareRun() );
261 SENF_CHECK_NO_THROW( senf::scheduler::detail::FIFORunner::instance().run() );
263 BOOST_CHECK( called );
264 BOOST_CHECK_PREDICATE( is_close, (t)(senf::ClockService::now()) );
266 catch (std::exception const & ex) {
267 std::cerr << "Exception:\n" << ex.what() << "\n";
273 t = senf::ClockService::now();
274 SENF_CHECK_NO_THROW( senf::scheduler::detail::FdManager::instance().processOnce() );
275 SENF_CHECK_NO_THROW( senf::scheduler::detail::FileDispatcher::instance().prepareRun() );
276 SENF_CHECK_NO_THROW( senf::scheduler::detail::FIFORunner::instance().run() );
278 BOOST_CHECK( ! called );
279 BOOST_CHECK_PREDICATE(
280 is_close, (t+senf::ClockService::milliseconds(500))(senf::ClockService::now()) );
282 unlink("test.empty.file");
283 senf::scheduler::detail::FileDispatcher::instance().timeout(-1);
286 ///////////////////////////////cc.e////////////////////////////////////////
293 // comment-column: 40
294 // c-file-style: "senf"
295 // indent-tabs-mode: nil
296 // ispell-local-dictionary: "american"
297 // compile-command: "scons -u test"