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.
25 //#include "scheduler.test.hh"
26 //#include "scheduler.test.ih"
29 #include <sys/types.h>
33 #include <sys/socket.h>
38 #include <boost/bind.hpp>
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 ///////////////////////////////////////////////////////////////////////////
104 signal(SIGCHLD, SIG_IGN);
112 signal(SIGCHLD, SIG_DFL);
114 sleep(1); // Wait for the server socket to be opened
118 bool stop_server(int pid)
120 sleep(1); // Wait for the server to terminate
121 if (kill(pid,SIGTERM)<0) {
126 if (waitpid(pid,&status,0)<0) {
131 if (WIFSIGNALED(status)) {
132 std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
135 if (WEXITSTATUS(status)!=0) {
136 std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
146 void callback(int fd, int ev)
149 switch (event & senf::scheduler::FdEvent::EV_ALL) {
150 case senf::scheduler::FdEvent::EV_READ:
151 size = recv(fd,buffer,1024,0);
153 case senf::scheduler::FdEvent::EV_PRIO:
154 size = recv(fd,buffer,1024,MSG_OOB);
156 case senf::scheduler::FdEvent::EV_WRITE:
157 size = write(fd,buffer,size);
160 senf::scheduler::terminate();
163 bool timeoutCalled = false;
166 timeoutCalled = true;
167 senf::scheduler::terminate();
172 HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
177 int retrieve_filehandle(HandleWrapper const & handle)
182 void handleCallback(HandleWrapper const & handle, int event)
184 if (handle.tag_ != "TheTag")
186 callback(handle.fd_,event);
189 bool is_close(ClockService::clock_type a, ClockService::clock_type b)
191 return (a<b ? b-a : a-b) < ClockService::milliseconds(100);
194 ClockService::clock_type sigtime (0);
196 void sigusr(siginfo_t const &)
198 sigtime = ClockService::now();
199 senf::scheduler::terminate();
202 void delay(unsigned long milliseconds)
205 ts.tv_sec = milliseconds / 1000;
206 ts.tv_nsec = (milliseconds % 1000) * 1000000;
207 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
210 void blockingHandler()
213 senf::scheduler::terminate();
216 unsigned eventCount (0);
218 void eventeventhandler()
224 BOOST_AUTO_UNIT_TEST(testScheduler)
226 int pid = start_server();
229 int sock = socket(PF_UNIX,SOCK_STREAM,0);
232 BOOST_FAIL("socket");
234 struct sockaddr_un sun;
235 memset(&sun,0,sizeof(sun));
236 sun.sun_family = AF_UNIX;
237 strcpy(sun.sun_path,SOCK_PATH);
239 if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
241 BOOST_FAIL("connect");
244 ///////////////////////////////////////////////////////////////////////////
246 senf::scheduler::EventHook evev ("eventCounter", eventeventhandler, true,
247 senf::scheduler::EventHook::PRE);
250 senf::scheduler::FdEvent fde1 ("testFdEvent", boost::bind(&callback, sock, _1),
251 sock, senf::scheduler::FdEvent::EV_READ);
252 event = senf::scheduler::FdEvent::EV_NONE;
253 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
254 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ );
255 BOOST_REQUIRE_EQUAL( size, 4 );
257 BOOST_CHECK_EQUAL( buffer, "READ" );
259 HandleWrapper handle(sock,"TheTag");
260 senf::scheduler::FdEvent fde2 ("testFdEvent", boost::bind(&handleCallback,handle,_1),
261 handle, senf::scheduler::FdEvent::EV_WRITE);
262 strcpy(buffer,"WRITE");
264 event = senf::scheduler::FdEvent::EV_NONE;
265 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
266 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_WRITE );
268 SENF_CHECK_NO_THROW( fde2.disable() );
269 event = senf::scheduler::FdEvent::EV_NONE;
271 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
272 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ|senf::scheduler::FdEvent::EV_HUP );
273 BOOST_REQUIRE_EQUAL( size, 2 );
275 BOOST_CHECK_EQUAL( buffer, "OK" );
279 senf::scheduler::TimerEvent timer1 ("testTimer1", &timeout,
280 ClockService::now()+ClockService::milliseconds(200));
281 senf::scheduler::TimerEvent timer2 ("testTimer2", &timeout,
282 ClockService::now()+ClockService::milliseconds(400));
284 event = senf::scheduler::FdEvent::EV_NONE;
285 ClockService::clock_type t (ClockService::now());
286 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
287 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(200)) );
288 BOOST_CHECK( timeoutCalled );
289 BOOST_CHECK( ! timer1.enabled() );
290 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
291 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (senf::scheduler::eventTime()) );
292 timeoutCalled = false;
293 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
294 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(400)) );
295 BOOST_CHECK( timeoutCalled );
296 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
297 BOOST_CHECK( ! timer2.enabled() );
299 BOOST_WARN_MESSAGE( false, "A 'Scheduler task hanging' error is expected to be signaled here." );
300 BOOST_CHECK_NO_THROW( timer1.action(&blockingHandler) );
301 BOOST_CHECK_NO_THROW( timer1.timeout(ClockService::now()) );
302 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
303 BOOST_CHECK_EQUAL( senf::scheduler::hangCount(), 1u );
307 senf::scheduler::TimerEvent timer ("testWatchdog", &timeout,
308 ClockService::now()+ClockService::milliseconds(400));
309 senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
311 ClockService::clock_type t = ClockService::now();
312 ::kill(::getpid(), SIGUSR1);
314 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
315 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
316 BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+ClockService::milliseconds(200)) );
317 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
320 BOOST_CHECK( eventCount >= 8u );
322 ///////////////////////////////////////////////////////////////////////////
326 BOOST_CHECK (stop_server(pid));
329 ///////////////////////////////cc.e////////////////////////////////////////
336 // c-file-style: "senf"
337 // indent-tabs-mode: nil
338 // ispell-local-dictionary: "american"
339 // compile-command: "scons -u test"
340 // comment-column: 40