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 ///////////////////////////////////////////////////////////////////////////
113 sleep(1); // Wait for the server socket to be opened
117 bool stop_server(int pid)
119 sleep(1); // Wait for the server to terminate
120 if (kill(pid,SIGTERM)<0) {
125 if (waitpid(pid,&status,0)<0) {
130 if (WIFSIGNALED(status)) {
131 std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
134 if (WEXITSTATUS(status)!=0) {
135 std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
145 void callback(int fd, int ev)
148 switch (event & senf::scheduler::FdEvent::EV_ALL) {
149 case senf::scheduler::FdEvent::EV_READ:
150 size = recv(fd,buffer,1024,0);
152 case senf::scheduler::FdEvent::EV_PRIO:
153 size = recv(fd,buffer,1024,MSG_OOB);
155 case senf::scheduler::FdEvent::EV_WRITE:
156 size = write(fd,buffer,size);
159 senf::scheduler::terminate();
162 bool timeoutCalled = false;
165 timeoutCalled = true;
166 senf::scheduler::terminate();
171 HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
176 int retrieve_filehandle(HandleWrapper const & handle)
181 void handleCallback(HandleWrapper const & handle, int event)
183 if (handle.tag_ != "TheTag")
185 callback(handle.fd_,event);
188 bool is_close(ClockService::clock_type a, ClockService::clock_type b)
190 return (a<b ? b-a : a-b) < ClockService::milliseconds(100);
193 ClockService::clock_type sigtime (0);
195 void sigusr(siginfo_t const &)
197 sigtime = ClockService::now();
198 senf::scheduler::terminate();
201 void delay(unsigned long milliseconds)
204 ts.tv_sec = milliseconds / 1000;
205 ts.tv_nsec = (milliseconds % 1000) * 1000000;
206 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
209 void blockingHandler()
212 senf::scheduler::terminate();
217 BOOST_AUTO_UNIT_TEST(testScheduler)
219 int pid = start_server();
222 int sock = socket(PF_UNIX,SOCK_STREAM,0);
225 BOOST_FAIL("socket");
227 struct sockaddr_un sun;
228 memset(&sun,0,sizeof(sun));
229 sun.sun_family = AF_UNIX;
230 strcpy(sun.sun_path,SOCK_PATH);
232 if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
234 BOOST_FAIL("connect");
237 ///////////////////////////////////////////////////////////////////////////
240 senf::scheduler::FdEvent fde1 ("testFdEvent", boost::bind(&callback, sock, _1),
241 sock, senf::scheduler::FdEvent::EV_READ);
242 event = senf::scheduler::FdEvent::EV_NONE;
243 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
244 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ );
245 BOOST_REQUIRE_EQUAL( size, 4 );
247 BOOST_CHECK_EQUAL( buffer, "READ" );
249 HandleWrapper handle(sock,"TheTag");
250 senf::scheduler::FdEvent fde2 ("testFdEvent", boost::bind(&handleCallback,handle,_1),
251 handle, senf::scheduler::FdEvent::EV_WRITE);
252 strcpy(buffer,"WRITE");
254 event = senf::scheduler::FdEvent::EV_NONE;
255 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
256 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_WRITE );
258 SENF_CHECK_NO_THROW( fde2.disable() );
259 event = senf::scheduler::FdEvent::EV_NONE;
261 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
262 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ|senf::scheduler::FdEvent::EV_HUP );
263 BOOST_REQUIRE_EQUAL( size, 2 );
265 BOOST_CHECK_EQUAL( buffer, "OK" );
269 senf::scheduler::TimerEvent timer1 ("testTimer1", &timeout,
270 ClockService::now()+ClockService::milliseconds(200));
271 senf::scheduler::TimerEvent timer2 ("testTimer2", &timeout,
272 ClockService::now()+ClockService::milliseconds(400));
274 event = senf::scheduler::FdEvent::EV_NONE;
275 ClockService::clock_type t (ClockService::now());
276 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
277 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(200)) );
278 BOOST_CHECK( timeoutCalled );
279 BOOST_CHECK( ! timer1.enabled() );
280 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
281 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (senf::scheduler::eventTime()) );
282 timeoutCalled = false;
283 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
284 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(400)) );
285 BOOST_CHECK( timeoutCalled );
286 BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
287 BOOST_CHECK( ! timer2.enabled() );
289 BOOST_WARN_MESSAGE( false, "A 'Scheduler task hanging' error is expected to be signaled here." );
290 BOOST_CHECK_NO_THROW( timer1.action(&blockingHandler) );
291 BOOST_CHECK_NO_THROW( timer1.timeout(ClockService::now()) );
292 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
293 BOOST_CHECK_EQUAL( senf::scheduler::hangCount(), 1u );
297 senf::scheduler::TimerEvent timer ("testWatchdog", &timeout,
298 ClockService::now()+ClockService::milliseconds(400));
299 senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
301 ClockService::clock_type t = ClockService::now();
302 ::kill(::getpid(), SIGUSR1);
304 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
305 BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
306 BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+ClockService::milliseconds(200)) );
307 BOOST_CHECK_NO_THROW( senf::scheduler::process() );
310 ///////////////////////////////////////////////////////////////////////////
314 BOOST_CHECK (stop_server(pid));
317 ///////////////////////////////cc.e////////////////////////////////////////
324 // c-file-style: "senf"
325 // indent-tabs-mode: nil
326 // ispell-local-dictionary: "american"
327 // compile-command: "scons -u test"
328 // comment-column: 40