Scheduler: Remove obsolete 'Scheduler' class
[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 }
216
217 BOOST_AUTO_UNIT_TEST(testScheduler)
218 {
219     int pid = start_server();
220     BOOST_REQUIRE (pid);
221
222     int sock = socket(PF_UNIX,SOCK_STREAM,0);
223     if (sock<0) {
224         error("socket");
225         BOOST_FAIL("socket");
226     }
227     struct sockaddr_un sun;
228     memset(&sun,0,sizeof(sun));
229     sun.sun_family = AF_UNIX;
230     strcpy(sun.sun_path,SOCK_PATH);
231
232     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
233         error("connect");
234         BOOST_FAIL("connect");
235     }
236
237     ///////////////////////////////////////////////////////////////////////////
238
239     {
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 );
246         buffer[size]=0;
247         BOOST_CHECK_EQUAL( buffer, "READ" );
248
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");
253         size=5;
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 );
257
258         SENF_CHECK_NO_THROW( fde2.disable() );
259         event = senf::scheduler::FdEvent::EV_NONE;
260         sleep(1);
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 );
264         buffer[size]=0;
265         BOOST_CHECK_EQUAL( buffer, "OK" );
266     }
267     
268     {
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));
273                                             
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() );
288
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 );
294     }
295
296     {
297         senf::scheduler::TimerEvent timer ("testWatchdog", &timeout,
298                                            ClockService::now()+ClockService::milliseconds(400));
299         senf::scheduler::SignalEvent sig (SIGUSR1, &sigusr);
300
301         ClockService::clock_type t = ClockService::now();
302         ::kill(::getpid(), SIGUSR1);
303         delay(100);
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() ); 
308     } 
309
310     ///////////////////////////////////////////////////////////////////////////
311
312     close(sock);
313
314     BOOST_CHECK (stop_server(pid));
315 }
316
317 ///////////////////////////////cc.e////////////////////////////////////////
318 #undef prefix_
319
320 \f
321 // Local Variables:
322 // mode: c++
323 // fill-column: 100
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
329 // End: