5 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
6 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
7 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the
21 // Free Software Foundation, Inc.,
22 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 //#include "scheduler.test.hh"
27 //#include "scheduler.test.ih"
30 #include <sys/types.h>
34 #include <sys/socket.h>
40 #include "Scheduler.hh"
42 #include "../Utils/auto_unit_test.hh"
43 #include <boost/test/test_tools.hpp>
46 ///////////////////////////////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;
144 void callback(int fd, Scheduler::EventId ev)
147 switch (event & Scheduler::EV_ALL) {
148 case Scheduler::EV_READ:
149 size = recv(fd,buffer,1024,0);
151 case Scheduler::EV_PRIO:
152 size = recv(fd,buffer,1024,MSG_OOB);
153 Scheduler::instance().terminate();
155 case Scheduler::EV_WRITE:
156 size = write(fd,buffer,size);
157 Scheduler::instance().terminate();
160 Scheduler::instance().terminate();
165 Scheduler::instance().terminate();
170 HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
175 int retrieve_filehandle(HandleWrapper const & handle)
180 void handleCallback(HandleWrapper const & handle, Scheduler::EventId event)
182 if (handle.tag_ != "TheTag")
184 callback(handle.fd_,event);
187 bool is_close(ClockService::clock_type a, ClockService::clock_type b)
189 return (a<b ? b-a : a-b) < ClockService::milliseconds(15);
192 ClockService::clock_type sigtime (0);
196 sigtime = ClockService::now();
197 Scheduler::instance().terminate();
200 void delay(unsigned long milliseconds)
203 ts.tv_sec = milliseconds / 1000;
204 ts.tv_nsec = (milliseconds % 1000) * 1000000;
205 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
209 BOOST_AUTO_UNIT_TEST(scheduler)
211 int pid = start_server();
214 int sock = socket(PF_UNIX,SOCK_STREAM,0);
217 BOOST_FAIL("socket");
219 struct sockaddr_un sun;
220 memset(&sun,0,sizeof(sun));
221 sun.sun_family = AF_UNIX;
222 strcpy(sun.sun_path,SOCK_PATH);
224 if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
226 BOOST_FAIL("connect");
229 ///////////////////////////////////////////////////////////////////////////
231 BOOST_CHECK_NO_THROW( Scheduler::instance() );
233 BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,boost::bind(&callback, sock, _1),
234 Scheduler::EV_READ) );
235 event = Scheduler::EV_NONE;
236 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
237 BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
238 BOOST_REQUIRE_EQUAL( size, 4 );
240 BOOST_CHECK_EQUAL( buffer, "READ" );
242 BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
243 ClockService::now()+ClockService::milliseconds(100),&timeout) );
244 BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
245 ClockService::now()+ClockService::milliseconds(200),&timeout) );
246 ClockService::clock_type t (ClockService::now());
247 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
248 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(100)) );
249 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
250 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
252 HandleWrapper handle(sock,"TheTag");
253 BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,
254 boost::bind(&handleCallback,handle,_1),
255 Scheduler::EV_WRITE) );
256 strcpy(buffer,"WRITE");
258 event = Scheduler::EV_NONE;
259 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
260 BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
262 BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
263 event = Scheduler::EV_NONE;
265 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
266 BOOST_CHECK_EQUAL( event, Scheduler::EventId(Scheduler::EV_READ|Scheduler::EV_HUP) );
267 BOOST_REQUIRE_EQUAL( size, 2 );
269 BOOST_CHECK_EQUAL( buffer, "OK" );
271 BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
272 ClockService::now()+ClockService::milliseconds(200),&timeout) );
273 BOOST_CHECK_NO_THROW( Scheduler::instance().registerSignal(SIGUSR1, &sigusr) );
274 t = ClockService::now();
275 ::kill(::getpid(), SIGUSR1);
277 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
278 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(100)) );
280 ///////////////////////////////////////////////////////////////////////////
284 BOOST_CHECK (stop_server(pid));
287 ///////////////////////////////cc.e////////////////////////////////////////
294 // c-file-style: "senf"
295 // indent-tabs-mode: nil
296 // ispell-local-dictionary: "american"
297 // compile-command: "scons -u test"
298 // comment-column: 40