5 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // Competence Center NETwork research (NET), St. Augustin, GERMANY
7 // Stefan Bund <g0dil@berlios.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>
39 #include <boost/bind.hpp>
41 #include "Scheduler.hh"
43 #include "../Utils/auto_unit_test.hh"
44 #include <boost/test/test_tools.hpp>
47 ///////////////////////////////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;
147 void callback(int fd, int ev)
150 switch (event & senf::scheduler::FdEvent::EV_ALL) {
151 case senf::scheduler::FdEvent::EV_READ:
152 size = recv(fd,buffer,1024,0);
154 case senf::scheduler::FdEvent::EV_PRIO:
155 size = recv(fd,buffer,1024,MSG_OOB);
157 case senf::scheduler::FdEvent::EV_WRITE:
158 size = write(fd,buffer,size);
161 senf::scheduler::terminate();
164 bool timeoutCalled = false;
167 timeoutCalled = true;
168 senf::scheduler::terminate();
173 HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
178 int retrieve_filehandle(HandleWrapper const & handle)
183 void handleCallback(HandleWrapper const & handle, int event)
185 if (handle.tag_ != "TheTag")
187 callback(handle.fd_,event);
190 bool is_close(ClockService::clock_type a, ClockService::clock_type b)
192 return (a<b ? b-a : a-b) < ClockService::milliseconds(100);
195 ClockService::clock_type sigtime (0);
197 void sigusr(siginfo_t const &)
199 sigtime = ClockService::now();
200 senf::scheduler::terminate();
203 void delay(unsigned long milliseconds)
206 ts.tv_sec = milliseconds / 1000;
207 ts.tv_nsec = (milliseconds % 1000) * 1000000;
208 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
211 void blockingHandler()
214 senf::scheduler::terminate();
217 unsigned eventCount (0);
219 void eventeventhandler()
225 BOOST_AUTO_UNIT_TEST(testScheduler)
227 int pid = start_server();
230 int sock = socket(PF_UNIX,SOCK_STREAM,0);
233 BOOST_FAIL("socket");
235 struct sockaddr_un sun;
236 memset(&sun,0,sizeof(sun));
237 sun.sun_family = AF_UNIX;
238 strcpy(sun.sun_path,SOCK_PATH);
240 if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
242 BOOST_FAIL("connect");
245 ///////////////////////////////////////////////////////////////////////////
247 senf::scheduler::EventHook evev ("eventCounter", eventeventhandler, true,
248 senf::scheduler::EventHook::PRE);
251 senf::scheduler::FdEvent fde1 ("testFdEvent", boost::bind(&callback, sock, _1),
252 sock, senf::scheduler::FdEvent::EV_READ);
253 event = senf::scheduler::FdEvent::EV_NONE;
254 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
255 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ );
256 BOOST_REQUIRE_EQUAL( size, 4 );
258 BOOST_CHECK_EQUAL( buffer, "READ" );
260 HandleWrapper handle(sock,"TheTag");
261 senf::scheduler::FdEvent fde2 ("testFdEvent", boost::bind(&handleCallback,handle,_1),
262 handle, senf::scheduler::FdEvent::EV_WRITE);
263 strcpy(buffer,"WRITE");
265 event = senf::scheduler::FdEvent::EV_NONE;
266 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
267 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_WRITE );
269 SENF_CHECK_NO_THROW( fde2.disable() );
270 event = senf::scheduler::FdEvent::EV_NONE;
272 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
273 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ|senf::scheduler::FdEvent::EV_HUP );
274 BOOST_REQUIRE_EQUAL( size, 2 );
276 BOOST_CHECK_EQUAL( buffer, "OK" );
280 senf::scheduler::TimerEvent timer1 ("testTimer1", &timeout,
281 ClockService::now()+ClockService::milliseconds(200));
282 senf::scheduler::TimerEvent timer2 ("testTimer2", &timeout,
283 ClockService::now()+ClockService::milliseconds(400));
285 event = senf::scheduler::FdEvent::EV_NONE;
286 ClockService::clock_type t (ClockService::now());
287 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
288 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(200)) );
289 BOOST_CHECK( timeoutCalled );
290 BOOST_CHECK( ! timer1.enabled() );
291 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
292 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (senf::scheduler::eventTime()) );
293 timeoutCalled = false;
294 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
295 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(400)) );
296 BOOST_CHECK( timeoutCalled );
297 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
298 BOOST_CHECK( ! timer2.enabled() );
300 BOOST_WARN_MESSAGE( false, "A 'Scheduler task hanging' error is expected to be signaled here." );
301 BOOST_CHECK_NO_THROW( timer1.action(&blockingHandler) );
302 BOOST_CHECK_NO_THROW( timer1.timeout(ClockService::now()) );
303 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
304 BOOST_CHECK_EQUAL( senf::scheduler::hangCount(), 1u );
308 senf::scheduler::TimerEvent timer ("testWatchdog", &timeout,
309 ClockService::now()+ClockService::milliseconds(400));
310 senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
312 ClockService::clock_type t = ClockService::now();
313 ::kill(::getpid(), SIGUSR1);
315 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
316 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
317 BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+ClockService::milliseconds(200)) );
318 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
321 BOOST_CHECK_EQUAL( eventCount, 8u );
323 ///////////////////////////////////////////////////////////////////////////
327 BOOST_CHECK (stop_server(pid));
330 ///////////////////////////////cc.e////////////////////////////////////////
337 // c-file-style: "senf"
338 // indent-tabs-mode: nil
339 // ispell-local-dictionary: "american"
340 // compile-command: "scons -u test"
341 // comment-column: 40