a46aa18da2e44aaa36523d2219c4a0378d326763
[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             server();
106             _exit(0);
107         }
108         if (pid < 0) {
109             error("fork");
110             return 0;
111         }
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(ClockService::clock_type a, ClockService::clock_type b)
189     {
190         return (a<b ? b-a : a-b) < ClockService::milliseconds(100);
191     }
192     
193     ClockService::clock_type sigtime (0);
194
195     void sigusr(siginfo_t const &)
196     {
197         sigtime = 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 BOOST_AUTO_UNIT_TEST(testScheduler)
224 {
225     int pid = start_server();
226     BOOST_REQUIRE (pid);
227
228     int sock = socket(PF_UNIX,SOCK_STREAM,0);
229     if (sock<0) {
230         error("socket");
231         BOOST_FAIL("socket");
232     }
233     struct sockaddr_un sun;
234     memset(&sun,0,sizeof(sun));
235     sun.sun_family = AF_UNIX;
236     strcpy(sun.sun_path,SOCK_PATH);
237
238     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
239         error("connect");
240         BOOST_FAIL("connect");
241     }
242
243     ///////////////////////////////////////////////////////////////////////////
244
245     senf::scheduler::EventEvent evev ("eventCounter", eventeventhandler, true,
246                                       senf::scheduler::EventEvent::PRIORITY_HIGH);
247
248     {
249         senf::scheduler::FdEvent fde1 ("testFdEvent", boost::bind(&callback, sock, _1),
250                                       sock, senf::scheduler::FdEvent::EV_READ);
251         event = senf::scheduler::FdEvent::EV_NONE;
252         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
253         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ );
254         BOOST_REQUIRE_EQUAL( size, 4 );
255         buffer[size]=0;
256         BOOST_CHECK_EQUAL( buffer, "READ" );
257
258         HandleWrapper handle(sock,"TheTag");
259         senf::scheduler::FdEvent fde2 ("testFdEvent", boost::bind(&handleCallback,handle,_1),
260                                       handle, senf::scheduler::FdEvent::EV_WRITE);
261         strcpy(buffer,"WRITE");
262         size=5;
263         event = senf::scheduler::FdEvent::EV_NONE;
264         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
265         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_WRITE );
266
267         SENF_CHECK_NO_THROW( fde2.disable() );
268         event = senf::scheduler::FdEvent::EV_NONE;
269         sleep(1);
270         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
271         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ|senf::scheduler::FdEvent::EV_HUP );
272         BOOST_REQUIRE_EQUAL( size, 2 );
273         buffer[size]=0;
274         BOOST_CHECK_EQUAL( buffer, "OK" );
275     }
276     
277     {
278         senf::scheduler::TimerEvent timer1 ("testTimer1", &timeout, 
279                                             ClockService::now()+ClockService::milliseconds(200));
280         senf::scheduler::TimerEvent timer2 ("testTimer2", &timeout,
281                                             ClockService::now()+ClockService::milliseconds(400));
282                                             
283         event = senf::scheduler::FdEvent::EV_NONE;
284         ClockService::clock_type t (ClockService::now());
285         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
286         BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(200)) );
287         BOOST_CHECK( timeoutCalled );
288         BOOST_CHECK( ! timer1.enabled() );
289         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
290         BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (senf::scheduler::eventTime()) );
291         timeoutCalled = false;
292         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
293         BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(400)) );
294         BOOST_CHECK( timeoutCalled );
295         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
296         BOOST_CHECK( ! timer2.enabled() );
297
298         BOOST_WARN_MESSAGE( false, "A 'Scheduler task hanging' error is expected to be signaled here." );
299         BOOST_CHECK_NO_THROW( timer1.action(&blockingHandler) );
300         BOOST_CHECK_NO_THROW( timer1.timeout(ClockService::now()) );
301         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
302         BOOST_CHECK_EQUAL( senf::scheduler::hangCount(), 1u );
303     }
304
305     {
306         senf::scheduler::TimerEvent timer ("testWatchdog", &timeout,
307                                            ClockService::now()+ClockService::milliseconds(400));
308         senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
309
310         ClockService::clock_type t = ClockService::now();
311         ::kill(::getpid(), SIGUSR1);
312         delay(100);
313         BOOST_CHECK_NO_THROW( senf::scheduler::process() ); 
314         BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
315         BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+ClockService::milliseconds(200)) );
316         BOOST_CHECK_NO_THROW( senf::scheduler::process() ); 
317     } 
318
319     BOOST_CHECK_EQUAL( eventCount, 8u );
320
321     ///////////////////////////////////////////////////////////////////////////
322
323     close(sock);
324
325     BOOST_CHECK (stop_server(pid));
326 }
327
328 ///////////////////////////////cc.e////////////////////////////////////////
329 #undef prefix_
330
331 \f
332 // Local Variables:
333 // mode: c++
334 // fill-column: 100
335 // c-file-style: "senf"
336 // indent-tabs-mode: nil
337 // ispell-local-dictionary: "american"
338 // compile-command: "scons -u test"
339 // comment-column: 40
340 // End: