Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Scheduler / Scheduler.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Scheduler unit tests */
25
26 //#include "Scheduler.test.hh"
27 //#include "Scheduler.test.ih"
28
29 // Custom includes
30 #include <sys/types.h>
31 #include <signal.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
34 #include <sys/socket.h>
35 #include <sys/un.h>
36 #include <errno.h>
37 #include <string.h>
38 #include <iostream>
39 #include <boost/bind.hpp>
40
41 #include "Scheduler.hh"
42
43 #include <senf/Utils/auto_unit_test.hh>
44 #include <boost/test/test_tools.hpp>
45
46 #define prefix_
47 //-/////////////////////////////////////////////////////////////////////////////////////////////////
48
49 namespace {
50
51     char const * SOCK_PATH = "/tmp/sched_test.sock";
52
53     void error(char const * fn, char const * proc="")
54     {
55         std::cerr << "\n" << proc << fn << ": " << strerror(errno) << std::endl;
56     }
57
58     void fail(char const * fn)
59     {
60         error(fn,"server: ");
61         _exit(1);
62     }
63
64     void server()
65     {
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");
76
77         //-/////////////////////////////////////////////////////////////////////////////////////////
78
79         if (write(conn,"READ",4)<0) fail("write");
80         char buffer[1024];
81         int size =  read(conn,buffer,1024);
82         if (size<0) fail("read");
83         if (size == 5) {
84             buffer[5] = 0;
85             if (strcmp(buffer,"WRITE")==0) {
86                 if (write(conn,"OK",2)<0) fail("write");
87             } else
88                 if (write(conn,"FAIL",4)<0) fail("write");
89         } else
90             if (write(conn,"FAIL",4)<0) fail("write");
91
92         //-/////////////////////////////////////////////////////////////////////////////////////////
93
94         close(conn);
95         close(sock);
96     }
97
98     int start_server()
99     {
100         unlink(SOCK_PATH);
101         int pid = fork();
102         if (pid == 0) {
103             signal(SIGCHLD, SIG_IGN);
104             server();
105             _exit(0);
106         }
107         if (pid < 0) {
108             error("fork");
109             return 0;
110         }
111         signal(SIGCHLD, SIG_DFL);
112
113         sleep(1); // Wait for the server socket to be opened
114         return pid;
115     }
116
117     bool stop_server(int pid)
118     {
119         sleep(1); // Wait for the server to terminate
120         if (kill(pid,SIGTERM)<0) {
121             error("kill");
122             return false;
123         }
124         int status = 0;
125         if (waitpid(pid,&status,0)<0) {
126             error("waitpid");
127             return false;
128         }
129         unlink(SOCK_PATH);
130         if (WIFSIGNALED(status)) {
131             std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
132             return false;
133         }
134         if (WEXITSTATUS(status)!=0) {
135             std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
136             return false;
137         }
138         return true;
139     }
140
141     char buffer[1024];
142     int size;
143     int event;
144
145     void callback(int fd, int ev)
146     {
147         event = ev;
148         switch (event & senf::scheduler::FdEvent::EV_ALL) {
149         case senf::scheduler::FdEvent::EV_READ:
150             size = recv(fd,buffer,1024,0);
151             break;
152         case senf::scheduler::FdEvent::EV_PRIO:
153             size = recv(fd,buffer,1024,MSG_OOB);
154             break;
155         case senf::scheduler::FdEvent::EV_WRITE:
156             size = write(fd,buffer,size);
157             break;
158         }
159         senf::scheduler::terminate();
160     }
161
162     bool timeoutCalled = false;
163     void timeout()
164     {
165         timeoutCalled = true;
166         senf::scheduler::terminate();
167     }
168
169     struct HandleWrapper
170     {
171         HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
172         int fd_;
173         std::string tag_;
174     };
175
176     int retrieve_filehandle(HandleWrapper const & handle)
177     {
178         return handle.fd_;
179     }
180
181     void handleCallback(HandleWrapper const & handle, int event)
182     {
183         if (handle.tag_ != "TheTag")
184             return;
185         callback(handle.fd_,event);
186     }
187
188     bool is_close(senf::ClockService::clock_type a, senf::ClockService::clock_type b)
189     {
190         return (a<b ? b-a : a-b) < senf::ClockService::milliseconds(100);
191     }
192
193     senf::ClockService::clock_type sigtime (0);
194
195     void sigusr(siginfo_t const &)
196     {
197         sigtime = senf::ClockService::now();
198         senf::scheduler::terminate();
199     }
200
201     void delay(unsigned long milliseconds)
202     {
203         struct timespec ts;
204         ts.tv_sec = milliseconds / 1000;
205         ts.tv_nsec = (milliseconds % 1000) * 1000000;
206         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
207     }
208
209     void blockingHandler()
210     {
211         delay(2200);
212         senf::scheduler::terminate();
213     }
214
215     unsigned eventCount (0);
216
217     void eventeventhandler()
218     {
219         ++ eventCount;
220     }
221 }
222
223 void schedulerTest()
224 {
225     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
226     BOOST_WARN_MESSAGE(enabled, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
227
228     int pid = start_server();
229     BOOST_REQUIRE (pid);
230
231     int sock = socket(PF_UNIX,SOCK_STREAM,0);
232     if (sock<0) {
233         error("socket");
234         BOOST_FAIL("socket");
235     }
236     struct sockaddr_un sun;
237     memset(&sun,0,sizeof(sun));
238     sun.sun_family = AF_UNIX;
239     strcpy(sun.sun_path,SOCK_PATH);
240
241     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
242         error("connect");
243         BOOST_FAIL("connect");
244     }
245
246     //-/////////////////////////////////////////////////////////////////////////////////////////////
247
248     senf::scheduler::EventHook evev ("eventCounter", eventeventhandler,
249                                      senf::scheduler::EventHook::PRE);
250
251     {
252         senf::scheduler::FdEvent fde1 ("testFdEvent", boost::bind(&callback, sock, _1),
253                                       sock, senf::scheduler::FdEvent::EV_READ);
254         event = senf::scheduler::FdEvent::EV_NONE;
255         SENF_CHECK_NO_THROW( senf::scheduler::process() );
256         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ );
257         BOOST_REQUIRE_EQUAL( size, 4 );
258         buffer[size]=0;
259         BOOST_CHECK_EQUAL( buffer, "READ" );
260
261         HandleWrapper handle(sock,"TheTag");
262         senf::scheduler::FdEvent fde2 ("testFdEvent", boost::bind(&handleCallback,handle,_1),
263                                       handle, senf::scheduler::FdEvent::EV_WRITE);
264         strcpy(buffer,"WRITE");
265         size=5;
266         event = senf::scheduler::FdEvent::EV_NONE;
267         SENF_CHECK_NO_THROW( senf::scheduler::process() );
268         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_WRITE );
269
270         SENF_CHECK_NO_THROW( fde2.disable() );
271         event = senf::scheduler::FdEvent::EV_NONE;
272         sleep(1);
273         SENF_CHECK_NO_THROW( senf::scheduler::process() );
274         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ|senf::scheduler::FdEvent::EV_HUP );
275         BOOST_REQUIRE_EQUAL( size, 2 );
276         buffer[size]=0;
277         BOOST_CHECK_EQUAL( buffer, "OK" );
278     }
279
280     {
281         senf::scheduler::TimerEvent timer1 ("testTimer1", &timeout,
282                                             senf::ClockService::now()+senf::ClockService::milliseconds(200));
283         senf::scheduler::TimerEvent timer2 ("testTimer2", &timeout,
284                                             senf::ClockService::now()+senf::ClockService::milliseconds(400));
285
286         event = senf::scheduler::FdEvent::EV_NONE;
287         senf::ClockService::clock_type t (senf::ClockService::now());
288         SENF_CHECK_NO_THROW( senf::scheduler::process() );
289         if (enabled)
290             BOOST_CHECK_PREDICATE( is_close, (senf::ClockService::now()-t) (senf::ClockService::milliseconds(200)) );
291         BOOST_CHECK( timeoutCalled );
292         BOOST_CHECK( ! timer1.enabled() );
293         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
294         if (enabled)
295             BOOST_CHECK_PREDICATE( is_close, (senf::ClockService::now()) (senf::scheduler::eventTime()) );
296         timeoutCalled = false;
297         SENF_CHECK_NO_THROW( senf::scheduler::process() );
298         if (enabled)
299             BOOST_CHECK_PREDICATE( is_close, (senf::ClockService::now()-t) (senf::ClockService::milliseconds(400)) );
300         BOOST_CHECK( timeoutCalled );
301         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
302         BOOST_CHECK( ! timer2.enabled() );
303
304         BOOST_MESSAGE( "A 'Scheduler task hanging' error is expected to be signaled here." );
305         SENF_CHECK_NO_THROW( timer1.action(&blockingHandler) );
306         SENF_CHECK_NO_THROW( timer1.timeout(senf::ClockService::now()) );
307         SENF_CHECK_NO_THROW( senf::scheduler::process() );
308         BOOST_CHECK_EQUAL( senf::scheduler::watchdogEvents(), 1u );
309     }
310
311     {
312         senf::scheduler::TimerEvent timer ("testWatchdog", &timeout,
313                                            senf::ClockService::now()+senf::ClockService::milliseconds(400));
314         senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
315
316         senf::ClockService::clock_type t = senf::ClockService::now();
317         ::kill(::getpid(), SIGUSR1);
318         delay(200);
319         SENF_CHECK_NO_THROW( senf::scheduler::process() );
320         if (enabled) {
321             BOOST_CHECK_PREDICATE( is_close, (senf::ClockService::now()) (t+senf::ClockService::milliseconds(200)) );
322             BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+senf::ClockService::milliseconds(200)) );
323         }
324         SENF_CHECK_NO_THROW( senf::scheduler::process() );
325     }
326
327     BOOST_CHECK( eventCount >= 8u );
328
329
330     //-/////////////////////////////////////////////////////////////////////////////////////////////
331
332     close(sock);
333
334     BOOST_CHECK (stop_server(pid));
335 }
336
337 SENF_AUTO_UNIT_TEST(testSchedulerPollTimers)
338 {
339     BOOST_CHECK( ! senf::scheduler::usingHiresTimers() );
340     schedulerTest();
341 }
342
343 SENF_AUTO_UNIT_TEST(testSchedulerHiresTimers)
344 {
345     if (senf::scheduler::haveScalableHiresTimers())
346         BOOST_MESSAGE( "Using timerfd() hires timers" );
347     else
348         BOOST_MESSAGE( "Using POSIX hires timers");
349     SENF_CHECK_NO_THROW( senf::scheduler::hiresTimers() );
350     BOOST_CHECK( senf::scheduler::usingHiresTimers() );
351     schedulerTest();
352     SENF_CHECK_NO_THROW( senf::scheduler::loresTimers() );
353     BOOST_CHECK( ! senf::scheduler::usingHiresTimers() );
354 }
355
356 SENF_AUTO_UNIT_TEST(testSchedulerPOSIXTimers)
357 {
358     if (senf::scheduler::haveScalableHiresTimers()) {
359         SENF_CHECK_NO_THROW( senf::scheduler::detail::TimerDispatcher::instance().timerSource(
360                                  std::auto_ptr<senf::scheduler::detail::TimerSource>(
361                                      new senf::scheduler::detail::POSIXTimerSource())) );
362         schedulerTest();
363         senf::scheduler::loresTimers();
364     }
365 }
366
367 namespace {
368
369     void sigme()
370     {
371         senf::scheduler::BlockSignals signalBlocker;
372         signalBlocker.block();
373         signalBlocker.unblock();
374         BOOST_CHECK( ! signalBlocker.blocked() );
375         signalBlocker.unblock();
376         signalBlocker.block();
377         BOOST_CHECK( signalBlocker.blocked() );
378         ::kill(::getpid(), SIGUSR1);
379         delay(200);
380     }
381
382 }
383
384 SENF_AUTO_UNIT_TEST(blockSignals)
385 {
386     char const * enabled (getenv("SENF_TIMING_CRITICAL_TESTS"));
387     BOOST_WARN_MESSAGE(enabled, "Set SENF_TIMING_CRITICAL_TESTS to not skip timing critical tests");
388
389     senf::scheduler::TimerEvent signaler ("sigme", &sigme, senf::ClockService::now());
390     senf::scheduler::TimerEvent timer (
391         "testWatchdog", &timeout, senf::ClockService::now()+senf::ClockService::milliseconds(400));
392     senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
393
394     senf::ClockService::clock_type t = senf::ClockService::now();
395     SENF_CHECK_NO_THROW( senf::scheduler::process() );
396
397     if (enabled) {
398         BOOST_CHECK_PREDICATE( is_close,
399                                (senf::ClockService::now())
400                                (t+senf::ClockService::milliseconds(200)) );
401         BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+senf::ClockService::milliseconds(200)) );
402     }
403
404     SENF_CHECK_NO_THROW( senf::scheduler::process() );
405 }
406
407 //-/////////////////////////////////////////////////////////////////////////////////////////////////
408 #undef prefix_
409
410 \f
411 // Local Variables:
412 // mode: c++
413 // fill-column: 100
414 // c-file-style: "senf"
415 // indent-tabs-mode: nil
416 // ispell-local-dictionary: "american"
417 // compile-command: "scons -u test"
418 // comment-column: 40
419 // End: