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 FdDispatcher.test unit tests */
26 //#include "FdDispatcher.test.hh"
27 //#include "FdDispatcher.test.ih"
30 #include <sys/types.h>
34 #include <sys/socket.h>
40 #include "FdDispatcher.hh"
42 #include <boost/bind.hpp>
44 #include "../Utils/auto_unit_test.hh"
45 #include <boost/test/test_tools.hpp>
48 ///////////////////////////////cc.p////////////////////////////////////////
52 char const * SOCK_PATH = "/tmp/sched_test.sock";
54 void error(char const * fn, char const * proc="")
56 std::cerr << "\n" << proc << fn << ": " << strerror(errno) << std::endl;
59 void fail(char const * fn)
67 int sock = socket(PF_UNIX,SOCK_STREAM,0);
68 if (sock<0) fail("socket");
69 struct sockaddr_un sun;
70 memset(&sun,0,sizeof(sun));
71 sun.sun_family = AF_UNIX;
72 strcpy(sun.sun_path,SOCK_PATH);
73 if (bind(sock,(struct sockaddr*)&sun,sizeof(sun))<0) fail("bind");
74 if (listen(sock,1)<0) fail("listen");
75 int conn = accept(sock,0,0);
76 if (conn < 0) fail("accept");
78 ///////////////////////////////////////////////////////////////////////////
80 if (write(conn,"READ",4)<0) fail("write");
82 int size = read(conn,buffer,1024);
83 if (size<0) fail("read");
86 if (strcmp(buffer,"WRITE")==0) {
87 if (write(conn,"OK",2)<0) fail("write");
89 if (write(conn,"FAIL",4)<0) fail("write");
91 if (write(conn,"FAIL",4)<0) fail("write");
93 ///////////////////////////////////////////////////////////////////////////
112 sleep(1); // Wait for the server socket to be opened
116 bool stop_server(int pid)
118 sleep(1); // Wait for the server to terminate
119 if (kill(pid,SIGTERM)<0) {
124 if (waitpid(pid,&status,0)<0) {
129 if (WIFSIGNALED(status)) {
130 std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
133 if (WEXITSTATUS(status)!=0) {
134 std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
145 void callback(int fd, int ev)
149 switch (event & senf::scheduler::FdDispatcher::EV_ALL) {
150 case senf::scheduler::FdDispatcher::EV_READ:
151 size = recv(fd,buffer,1024,0);
153 case senf::scheduler::FdDispatcher::EV_PRIO:
154 size = recv(fd,buffer,1024,MSG_OOB);
156 case senf::scheduler::FdDispatcher::EV_WRITE:
157 size = write(fd,buffer,size);
164 BOOST_AUTO_UNIT_TEST(fdDispatcher)
166 senf::scheduler::FdManager manager;
167 senf::scheduler::FIFORunner runner;
168 senf::scheduler::FdDispatcher dispatcher (manager, runner);
170 int pid (start_server());
171 BOOST_REQUIRE( pid );
173 int sock = socket(PF_UNIX,SOCK_STREAM,0);
176 BOOST_FAIL("socket");
178 struct sockaddr_un sun;
179 memset(&sun,0,sizeof(sun));
180 sun.sun_family = AF_UNIX;
181 strcpy(sun.sun_path,SOCK_PATH);
183 if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
185 BOOST_FAIL("connect");
188 SENF_CHECK_NO_THROW( dispatcher.add(sock, boost::bind(&callback, sock, _1),
189 senf::scheduler::FdDispatcher::EV_READ) );
190 manager.timeout(1000);
192 SENF_CHECK_NO_THROW( manager.processOnce() );
193 SENF_CHECK_NO_THROW( runner.run() );
194 BOOST_CHECK_EQUAL( event, senf::scheduler::FdDispatcher::EV_READ );
195 BOOST_CHECK_EQUAL( size, 4 );
197 BOOST_CHECK_EQUAL( buffer, "READ" );
199 strcpy(buffer,"WRITE");
201 SENF_CHECK_NO_THROW( dispatcher.add(sock, boost::bind(&callback, sock, _1),
202 senf::scheduler::FdDispatcher::EV_WRITE) );
205 SENF_CHECK_NO_THROW( manager.processOnce() );
206 SENF_CHECK_NO_THROW( runner.run() );
207 BOOST_CHECK_EQUAL( event, senf::scheduler::FdDispatcher::EV_WRITE );
209 SENF_CHECK_NO_THROW( dispatcher.remove(sock, senf::scheduler::FdDispatcher::EV_WRITE) );
212 SENF_CHECK_NO_THROW( manager.processOnce() );
213 SENF_CHECK_NO_THROW( runner.run() );
214 BOOST_CHECK_EQUAL( event, senf::scheduler::FdDispatcher::EV_HUP | senf::scheduler::FdDispatcher::EV_READ );
215 BOOST_CHECK_EQUAL( size, 2 );
217 BOOST_CHECK_EQUAL( buffer, "OK" );
219 BOOST_CHECK_EQUAL( calls, 3 );
220 SENF_CHECK_NO_THROW( runner.run() );
221 BOOST_CHECK_EQUAL( calls, 3 );
223 // Ensure, removing an already closed file-descriptor doesn't wreak havoc
225 SENF_CHECK_NO_THROW( dispatcher.remove(sock) );
227 SENF_CHECK_NO_THROW( runner.run() );
228 BOOST_CHECK_EQUAL( calls, 3 );
231 BOOST_CHECK (stop_server(pid));
234 ///////////////////////////////cc.e////////////////////////////////////////
241 // comment-column: 40
242 // c-file-style: "senf"
243 // indent-tabs-mode: nil
244 // ispell-local-dictionary: "american"
245 // compile-command: "scons -u test"