c8dc0b4195ac311ae8fb9796879fc388d75b9a2e
[senf.git] / Scheduler / Scheduler.test.cc
1
2 // $Id$
3 //
4 // Copyright (C) 2006
5 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // Competence Center NETwork research (NET), St. Augustin, GERMANY
7 //     Stefan Bund <g0dil@berlios.de>
8 //
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.
13 //
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.
18 //
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.
23
24 // 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 "../Utils/auto_unit_test.hh"
44 #include <boost/test/test_tools.hpp>
45
46 #define prefix_
47 ///////////////////////////////cc.p////////////////////////////////////////
48
49 using namespace senf;
50
51 namespace {
52
53     char const * SOCK_PATH = "/tmp/sched_test.sock";
54
55     void error(char const * fn, char const * proc="")
56     {
57         std::cerr << "\n" << proc << fn << ": " << strerror(errno) << std::endl;
58     }
59
60     void fail(char const * fn)
61     {
62         error(fn,"server: ");
63         _exit(1);
64     }
65
66     void server()
67     {
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");
78
79         ///////////////////////////////////////////////////////////////////////////
80
81         if (write(conn,"READ",4)<0) fail("write");
82         char buffer[1024];
83         int size =  read(conn,buffer,1024);
84         if (size<0) fail("read");
85         if (size == 5) {
86             buffer[5] = 0;
87             if (strcmp(buffer,"WRITE")==0) {
88                 if (write(conn,"OK",2)<0) fail("write");
89             } else
90                 if (write(conn,"FAIL",4)<0) fail("write");
91         } else
92             if (write(conn,"FAIL",4)<0) fail("write");
93
94         ///////////////////////////////////////////////////////////////////////////
95
96         close(conn);
97         close(sock);
98     }
99
100     int start_server()
101     {
102         unlink(SOCK_PATH);
103         int pid = fork();
104         if (pid == 0) {
105             signal(SIGCHLD, SIG_IGN);
106             server();
107             _exit(0);
108         }
109         if (pid < 0) {
110             error("fork");
111             return 0;
112         }
113         signal(SIGCHLD, SIG_DFL);
114
115         sleep(1); // Wait for the server socket to be opened
116         return pid;
117     }
118
119     bool stop_server(int pid)
120     {
121         sleep(1); // Wait for the server to terminate
122         if (kill(pid,SIGTERM)<0) {
123             error("kill");
124             return false;
125         }
126         int status = 0;
127         if (waitpid(pid,&status,0)<0) {
128             error("waitpid");
129             return false;
130         }
131         unlink(SOCK_PATH);
132         if (WIFSIGNALED(status)) {
133             std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
134             return false;
135         }
136         if (WEXITSTATUS(status)!=0) {
137             std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
138             return false;
139         }
140         return true;
141     }
142
143     char buffer[1024];
144     int size;
145     int event;
146
147     void callback(int fd, int ev)
148     {
149         event = ev;
150         switch (event & senf::scheduler::FdEvent::EV_ALL) {
151         case senf::scheduler::FdEvent::EV_READ:
152             size = recv(fd,buffer,1024,0);
153             break;
154         case senf::scheduler::FdEvent::EV_PRIO:
155             size = recv(fd,buffer,1024,MSG_OOB);
156             break;
157         case senf::scheduler::FdEvent::EV_WRITE:
158             size = write(fd,buffer,size);
159             break;
160         }
161         senf::scheduler::terminate();
162     }
163
164     bool timeoutCalled = false;
165     void timeout()
166     {
167         timeoutCalled = true;
168         senf::scheduler::terminate();
169     }
170
171     struct HandleWrapper
172     {
173         HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
174         int fd_;
175         std::string tag_;
176     };
177
178     int retrieve_filehandle(HandleWrapper const & handle)
179     {
180         return handle.fd_;
181     }
182
183     void handleCallback(HandleWrapper const & handle, int event)
184     {
185         if (handle.tag_ != "TheTag")
186             return;
187         callback(handle.fd_,event);
188     }
189
190     bool is_close(ClockService::clock_type a, ClockService::clock_type b)
191     {
192         return (a<b ? b-a : a-b) < ClockService::milliseconds(100);
193     }
194     
195     ClockService::clock_type sigtime (0);
196
197     void sigusr(siginfo_t const &)
198     {
199         sigtime = ClockService::now();
200         senf::scheduler::terminate();
201     }
202
203     void delay(unsigned long milliseconds)
204     {
205         struct timespec ts;
206         ts.tv_sec = milliseconds / 1000;
207         ts.tv_nsec = (milliseconds % 1000) * 1000000;
208         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
209     }
210
211     void blockingHandler()
212     {
213         delay(2200);
214         senf::scheduler::terminate();
215     }
216
217     unsigned eventCount (0);
218
219     void eventeventhandler()
220     {
221         ++ eventCount;
222     }
223 }
224
225 BOOST_AUTO_UNIT_TEST(testScheduler)
226 {
227     int pid = start_server();
228     BOOST_REQUIRE (pid);
229
230     int sock = socket(PF_UNIX,SOCK_STREAM,0);
231     if (sock<0) {
232         error("socket");
233         BOOST_FAIL("socket");
234     }
235     struct sockaddr_un sun;
236     memset(&sun,0,sizeof(sun));
237     sun.sun_family = AF_UNIX;
238     strcpy(sun.sun_path,SOCK_PATH);
239
240     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
241         error("connect");
242         BOOST_FAIL("connect");
243     }
244
245     ///////////////////////////////////////////////////////////////////////////
246
247     senf::scheduler::EventHook evev ("eventCounter", eventeventhandler, true,
248                                       senf::scheduler::EventHook::PRE);
249
250     {
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 );
257         buffer[size]=0;
258         BOOST_CHECK_EQUAL( buffer, "READ" );
259
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");
264         size=5;
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 );
268
269         SENF_CHECK_NO_THROW( fde2.disable() );
270         event = senf::scheduler::FdEvent::EV_NONE;
271         sleep(1);
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 );
275         buffer[size]=0;
276         BOOST_CHECK_EQUAL( buffer, "OK" );
277     }
278     
279     {
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));
284                                             
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() );
299
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 );
305     }
306
307     {
308         senf::scheduler::TimerEvent timer ("testWatchdog", &timeout,
309                                            ClockService::now()+ClockService::milliseconds(400));
310         senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
311
312         ClockService::clock_type t = ClockService::now();
313         ::kill(::getpid(), SIGUSR1);
314         delay(100);
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() ); 
319     } 
320
321     BOOST_CHECK_EQUAL( eventCount, 8u );
322
323     ///////////////////////////////////////////////////////////////////////////
324
325     close(sock);
326
327     BOOST_CHECK (stop_server(pid));
328 }
329
330 ///////////////////////////////cc.e////////////////////////////////////////
331 #undef prefix_
332
333 \f
334 // Local Variables:
335 // mode: c++
336 // fill-column: 100
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
342 // End: