4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <stefan.bund@fokus.fraunhofer.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.
25 //#include "scheduler.test.hh"
26 //#include "scheduler.test.ih"
29 #include <sys/types.h>
33 #include <sys/socket.h>
39 #include "Scheduler.hh"
41 #include <boost/test/auto_unit_test.hpp>
42 #include <boost/test/test_tools.hpp>
45 ///////////////////////////////cc.p////////////////////////////////////////
51 char const * SOCK_PATH = "/tmp/sched_test.sock";
53 void error(char const * fn, char const * proc="")
55 std::cerr << "\n" << proc << fn << ": " << strerror(errno) << std::endl;
58 void fail(char const * fn)
66 int sock = socket(PF_UNIX,SOCK_STREAM,0);
67 if (sock<0) fail("socket");
68 struct sockaddr_un sun;
69 memset(&sun,0,sizeof(sun));
70 sun.sun_family = AF_UNIX;
71 strcpy(sun.sun_path,SOCK_PATH);
72 if (bind(sock,(struct sockaddr*)&sun,sizeof(sun))<0) fail("bind");
73 if (listen(sock,1)<0) fail("listen");
74 int conn = accept(sock,0,0);
75 if (conn < 0) fail("accept");
77 ///////////////////////////////////////////////////////////////////////////
79 if (write(conn,"READ",4)<0) fail("write");
81 int size = read(conn,buffer,1024);
82 if (size<0) fail("read");
85 if (strcmp(buffer,"WRITE")==0) {
86 if (write(conn,"OK",2)<0) fail("write");
88 if (write(conn,"FAIL",4)<0) fail("write");
90 if (write(conn,"FAIL",4)<0) fail("write");
92 ///////////////////////////////////////////////////////////////////////////
111 sleep(1); // Wait for the server socket to be opened
115 bool stop_server(int pid)
117 sleep(1); // Wait for the server to terminate
118 if (kill(pid,SIGTERM)<0) {
123 if (waitpid(pid,&status,0)<0) {
128 if (WIFSIGNALED(status)) {
129 std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
132 if (WEXITSTATUS(status)!=0) {
133 std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
143 void callback(int fd, Scheduler::EventId ev)
147 case Scheduler::EV_READ:
148 size = recv(fd,buffer,1024,0);
150 case Scheduler::EV_PRIO:
151 size = recv(fd,buffer,1024,MSG_OOB);
152 Scheduler::instance().terminate();
154 case Scheduler::EV_WRITE:
155 size = write(fd,buffer,size);
156 Scheduler::instance().terminate();
158 case Scheduler::EV_HUP:
159 case Scheduler::EV_ERR:
160 case Scheduler::EV_NONE:
161 case Scheduler::EV_ALL:
164 Scheduler::instance().terminate();
169 Scheduler::instance().terminate();
174 HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
179 int retrieve_filehandle(HandleWrapper const & handle)
184 void handleCallback(HandleWrapper const & handle, Scheduler::EventId event)
186 if (handle.tag_ != "TheTag")
188 callback(handle.fd_,event);
191 bool is_close(MicroTime a, MicroTime b)
193 return (a<b ? b-a : a-b) < 10100; // a little bit over 10ms
198 BOOST_AUTO_UNIT_TEST(scheduler)
200 int pid = start_server();
203 int sock = socket(PF_UNIX,SOCK_STREAM,0);
206 BOOST_FAIL("socket");
208 struct sockaddr_un sun;
209 memset(&sun,0,sizeof(sun));
210 sun.sun_family = AF_UNIX;
211 strcpy(sun.sun_path,SOCK_PATH);
213 if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
215 BOOST_FAIL("connect");
218 ///////////////////////////////////////////////////////////////////////////
220 BOOST_CHECK_NO_THROW( Scheduler::instance() );
222 BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,&callback,Scheduler::EV_READ) );
223 event = Scheduler::EV_NONE;
224 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
225 BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
226 BOOST_REQUIRE_EQUAL( size, 4 );
228 BOOST_CHECK_EQUAL( buffer, "READ" );
230 BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(100,&timeout) );
231 BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(200,&timeout) );
233 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
234 BOOST_CHECK_PREDICATE( is_close, (now()) (t+100*1000) );
235 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
236 BOOST_CHECK_PREDICATE( is_close, (now()) (t+200*1000) );
238 HandleWrapper handle(sock,"TheTag");
239 BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,&handleCallback,Scheduler::EV_WRITE) );
240 strcpy(buffer,"WRITE");
242 event = Scheduler::EV_NONE;
243 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
244 BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
246 BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
247 event = Scheduler::EV_NONE;
248 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
249 BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
250 BOOST_REQUIRE_EQUAL( size, 2 );
252 BOOST_CHECK_EQUAL( buffer, "OK" );
254 ///////////////////////////////////////////////////////////////////////////
258 BOOST_CHECK (stop_server(pid));
261 ///////////////////////////////cc.e////////////////////////////////////////
268 // c-file-style: "senf"
269 // indent-tabs-mode: nil
270 // ispell-local-dictionary: "american"