Scheduler: Implement PollTimerSource
[senf.git] / 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 // Unit tests
24
25 //#include "scheduler.test.hh"
26 //#include "scheduler.test.ih"
27
28 // Custom includes
29 #include <sys/types.h>
30 #include <signal.h>
31 #include <sys/wait.h>
32 #include <unistd.h>
33 #include <sys/socket.h>
34 #include <sys/un.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <iostream>
38 #include <boost/bind.hpp>
39
40 #include "Scheduler.hh"
41
42 #include "../Utils/auto_unit_test.hh"
43 #include <boost/test/test_tools.hpp>
44
45 #define prefix_
46 ///////////////////////////////cc.p////////////////////////////////////////
47
48 using namespace senf;
49
50 namespace {
51
52     char const * SOCK_PATH = "/tmp/sched_test.sock";
53
54     void error(char const * fn, char const * proc="")
55     {
56         std::cerr << "\n" << proc << fn << ": " << strerror(errno) << std::endl;
57     }
58
59     void fail(char const * fn)
60     {
61         error(fn,"server: ");
62         _exit(1);
63     }
64
65     void server()
66     {
67         int sock = socket(PF_UNIX,SOCK_STREAM,0);
68         if (sock<0) fail("socket");
69         struct sockaddr_un sun;
70         memset(&sun,0,sizeof(sun));
71         sun.sun_family = AF_UNIX;
72         strcpy(sun.sun_path,SOCK_PATH);
73         if (bind(sock,(struct sockaddr*)&sun,sizeof(sun))<0) fail("bind");
74         if (listen(sock,1)<0) fail("listen");
75         int conn = accept(sock,0,0);
76         if (conn < 0) fail("accept");
77
78         ///////////////////////////////////////////////////////////////////////////
79
80         if (write(conn,"READ",4)<0) fail("write");
81         char buffer[1024];
82         int size =  read(conn,buffer,1024);
83         if (size<0) fail("read");
84         if (size == 5) {
85             buffer[5] = 0;
86             if (strcmp(buffer,"WRITE")==0) {
87                 if (write(conn,"OK",2)<0) fail("write");
88             } else
89                 if (write(conn,"FAIL",4)<0) fail("write");
90         } else
91             if (write(conn,"FAIL",4)<0) fail("write");
92
93         ///////////////////////////////////////////////////////////////////////////
94
95         close(conn);
96         close(sock);
97     }
98
99     int start_server()
100     {
101         unlink(SOCK_PATH);
102         int pid = fork();
103         if (pid == 0) {
104             signal(SIGCHLD, SIG_IGN);
105             server();
106             _exit(0);
107         }
108         if (pid < 0) {
109             error("fork");
110             return 0;
111         }
112         signal(SIGCHLD, SIG_DFL);
113
114         sleep(1); // Wait for the server socket to be opened
115         return pid;
116     }
117
118     bool stop_server(int pid)
119     {
120         sleep(1); // Wait for the server to terminate
121         if (kill(pid,SIGTERM)<0) {
122             error("kill");
123             return false;
124         }
125         int status = 0;
126         if (waitpid(pid,&status,0)<0) {
127             error("waitpid");
128             return false;
129         }
130         unlink(SOCK_PATH);
131         if (WIFSIGNALED(status)) {
132             std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
133             return false;
134         }
135         if (WEXITSTATUS(status)!=0) {
136             std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
137             return false;
138         }
139         return true;
140     }
141
142     char buffer[1024];
143     int size;
144     int event;
145
146     void callback(int fd, int ev)
147     {
148         event = ev;
149         switch (event & senf::scheduler::FdEvent::EV_ALL) {
150         case senf::scheduler::FdEvent::EV_READ:
151             size = recv(fd,buffer,1024,0);
152             break;
153         case senf::scheduler::FdEvent::EV_PRIO:
154             size = recv(fd,buffer,1024,MSG_OOB);
155             break;
156         case senf::scheduler::FdEvent::EV_WRITE:
157             size = write(fd,buffer,size);
158             break;
159         }
160         senf::scheduler::terminate();
161     }
162
163     bool timeoutCalled = false;
164     void timeout()
165     {
166         timeoutCalled = true;
167         senf::scheduler::terminate();
168     }
169
170     struct HandleWrapper
171     {
172         HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
173         int fd_;
174         std::string tag_;
175     };
176
177     int retrieve_filehandle(HandleWrapper const & handle)
178     {
179         return handle.fd_;
180     }
181
182     void handleCallback(HandleWrapper const & handle, int event)
183     {
184         if (handle.tag_ != "TheTag")
185             return;
186         callback(handle.fd_,event);
187     }
188
189     bool is_close(ClockService::clock_type a, ClockService::clock_type b)
190     {
191         return (a<b ? b-a : a-b) < ClockService::milliseconds(100);
192     }
193     
194     ClockService::clock_type sigtime (0);
195
196     void sigusr(siginfo_t const &)
197     {
198         sigtime = ClockService::now();
199         senf::scheduler::terminate();
200     }
201
202     void delay(unsigned long milliseconds)
203     {
204         struct timespec ts;
205         ts.tv_sec = milliseconds / 1000;
206         ts.tv_nsec = (milliseconds % 1000) * 1000000;
207         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
208     }
209
210     void blockingHandler()
211     {
212         delay(2200);
213         senf::scheduler::terminate();
214     }
215
216     unsigned eventCount (0);
217
218     void eventeventhandler()
219     {
220         ++ eventCount;
221     }
222 }
223
224 BOOST_AUTO_UNIT_TEST(testScheduler)
225 {
226     int pid = start_server();
227     BOOST_REQUIRE (pid);
228
229     int sock = socket(PF_UNIX,SOCK_STREAM,0);
230     if (sock<0) {
231         error("socket");
232         BOOST_FAIL("socket");
233     }
234     struct sockaddr_un sun;
235     memset(&sun,0,sizeof(sun));
236     sun.sun_family = AF_UNIX;
237     strcpy(sun.sun_path,SOCK_PATH);
238
239     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
240         error("connect");
241         BOOST_FAIL("connect");
242     }
243
244     ///////////////////////////////////////////////////////////////////////////
245
246     senf::scheduler::EventHook evev ("eventCounter", eventeventhandler, true,
247                                       senf::scheduler::EventHook::PRE);
248
249     {
250         senf::scheduler::FdEvent fde1 ("testFdEvent", boost::bind(&callback, sock, _1),
251                                       sock, senf::scheduler::FdEvent::EV_READ);
252         event = senf::scheduler::FdEvent::EV_NONE;
253         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
254         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ );
255         BOOST_REQUIRE_EQUAL( size, 4 );
256         buffer[size]=0;
257         BOOST_CHECK_EQUAL( buffer, "READ" );
258
259         HandleWrapper handle(sock,"TheTag");
260         senf::scheduler::FdEvent fde2 ("testFdEvent", boost::bind(&handleCallback,handle,_1),
261                                       handle, senf::scheduler::FdEvent::EV_WRITE);
262         strcpy(buffer,"WRITE");
263         size=5;
264         event = senf::scheduler::FdEvent::EV_NONE;
265         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
266         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_WRITE );
267
268         SENF_CHECK_NO_THROW( fde2.disable() );
269         event = senf::scheduler::FdEvent::EV_NONE;
270         sleep(1);
271         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
272         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_READ|senf::scheduler::FdEvent::EV_HUP );
273         BOOST_REQUIRE_EQUAL( size, 2 );
274         buffer[size]=0;
275         BOOST_CHECK_EQUAL( buffer, "OK" );
276     }
277     
278     {
279         senf::scheduler::TimerEvent timer1 ("testTimer1", &timeout, 
280                                             ClockService::now()+ClockService::milliseconds(200));
281         senf::scheduler::TimerEvent timer2 ("testTimer2", &timeout,
282                                             ClockService::now()+ClockService::milliseconds(400));
283                                             
284         event = senf::scheduler::FdEvent::EV_NONE;
285         ClockService::clock_type t (ClockService::now());
286         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
287         BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(200)) );
288         BOOST_CHECK( timeoutCalled );
289         BOOST_CHECK( ! timer1.enabled() );
290         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
291         BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (senf::scheduler::eventTime()) );
292         timeoutCalled = false;
293         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
294         BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(400)) );
295         BOOST_CHECK( timeoutCalled );
296         BOOST_CHECK_EQUAL( event, senf::scheduler::FdEvent::EV_NONE );
297         BOOST_CHECK( ! timer2.enabled() );
298
299         BOOST_WARN_MESSAGE( false, "A 'Scheduler task hanging' error is expected to be signaled here." );
300         BOOST_CHECK_NO_THROW( timer1.action(&blockingHandler) );
301         BOOST_CHECK_NO_THROW( timer1.timeout(ClockService::now()) );
302         BOOST_CHECK_NO_THROW( senf::scheduler::process() );
303         BOOST_CHECK_EQUAL( senf::scheduler::hangCount(), 1u );
304     }
305
306     {
307         senf::scheduler::TimerEvent timer ("testWatchdog", &timeout,
308                                            ClockService::now()+ClockService::milliseconds(400));
309         senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
310
311         ClockService::clock_type t = ClockService::now();
312         ::kill(::getpid(), SIGUSR1);
313         delay(100);
314         BOOST_CHECK_NO_THROW( senf::scheduler::process() ); 
315         BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
316         BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+ClockService::milliseconds(200)) );
317         BOOST_CHECK_NO_THROW( senf::scheduler::process() ); 
318     } 
319
320     BOOST_CHECK( eventCount >= 8u );
321
322     ///////////////////////////////////////////////////////////////////////////
323
324     close(sock);
325
326     BOOST_CHECK (stop_server(pid));
327 }
328
329 ///////////////////////////////cc.e////////////////////////////////////////
330 #undef prefix_
331
332 \f
333 // Local Variables:
334 // mode: c++
335 // fill-column: 100
336 // c-file-style: "senf"
337 // indent-tabs-mode: nil
338 // ispell-local-dictionary: "american"
339 // compile-command: "scons -u test"
340 // comment-column: 40
341 // End: