0501762eeb925e45523907497db8069e76c292ee
[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
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             server();
105             _exit(0);
106         }
107         if (pid < 0) {
108             error("fork");
109             return 0;
110         }
111
112         sleep(1); // Wait for the server socket to be opened
113         return pid;
114     }
115
116     bool stop_server(int pid)
117     {
118         sleep(1); // Wait for the server to terminate
119         if (kill(pid,SIGTERM)<0) {
120             error("kill");
121             return false;
122         }
123         int status = 0;
124         if (waitpid(pid,&status,0)<0) {
125             error("waitpid");
126             return false;
127         }
128         unlink(SOCK_PATH);
129         if (WIFSIGNALED(status)) {
130             std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
131             return false;
132         }
133         if (WEXITSTATUS(status)!=0) {
134             std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
135             return false;
136         }
137         return true;
138     }
139
140     char buffer[1024];
141     int size;
142     int event;
143
144     void callback(int fd, int ev)
145     {
146         event = ev;
147         switch (event & Scheduler::EV_ALL) {
148         case Scheduler::EV_READ:
149             size = recv(fd,buffer,1024,0);
150             break;
151         case Scheduler::EV_PRIO:
152             size = recv(fd,buffer,1024,MSG_OOB);
153             Scheduler::instance().terminate();
154             break;
155         case Scheduler::EV_WRITE:
156             size = write(fd,buffer,size);
157             Scheduler::instance().terminate();
158             break;
159         }
160         Scheduler::instance().terminate();
161     }
162
163     bool timeoutCalled = false;
164     void timeout()
165     {
166         timeoutCalled = true;
167         Scheduler::instance().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         Scheduler::instance().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(1200);
213         Scheduler::instance().terminate();
214     }
215
216 }
217
218 BOOST_AUTO_UNIT_TEST(testScheduler)
219 {
220     int pid = start_server();
221     BOOST_REQUIRE (pid);
222
223     int sock = socket(PF_UNIX,SOCK_STREAM,0);
224     if (sock<0) {
225         error("socket");
226         BOOST_FAIL("socket");
227     }
228     struct sockaddr_un sun;
229     memset(&sun,0,sizeof(sun));
230     sun.sun_family = AF_UNIX;
231     strcpy(sun.sun_path,SOCK_PATH);
232
233     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
234         error("connect");
235         BOOST_FAIL("connect");
236     }
237
238     ///////////////////////////////////////////////////////////////////////////
239
240     BOOST_CHECK_NO_THROW( Scheduler::instance() );
241
242     BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,boost::bind(&callback, sock, _1),
243                                                     Scheduler::EV_READ) );
244     event = Scheduler::EV_NONE;
245     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
246     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
247     BOOST_REQUIRE_EQUAL( size, 4 );
248     buffer[size]=0;
249     BOOST_CHECK_EQUAL( buffer, "READ" );
250
251     event = Scheduler::EV_NONE;
252     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
253                               ClockService::now()+ClockService::milliseconds(200),&timeout) );
254     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
255                               ClockService::now()+ClockService::milliseconds(400),&timeout) );
256     ClockService::clock_type t (ClockService::now());
257     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
258     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(200)) );
259     BOOST_CHECK( timeoutCalled );
260     BOOST_CHECK_EQUAL( event, Scheduler::EV_NONE );
261     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (Scheduler::instance().eventTime()) );
262     timeoutCalled = false;
263     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
264     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()-t) (ClockService::milliseconds(400)) );
265     BOOST_CHECK( timeoutCalled );
266     BOOST_CHECK_EQUAL( event, Scheduler::EV_NONE );
267
268     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(ClockService::now(), &blockingHandler) );
269     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
270     BOOST_CHECK_EQUAL( Scheduler::instance().hangCount(), 1u );
271
272     HandleWrapper handle(sock,"TheTag");
273     BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,
274                                                     boost::bind(&handleCallback,handle,_1),
275                                                     Scheduler::EV_WRITE) );
276     strcpy(buffer,"WRITE");
277     size=5;
278     event = Scheduler::EV_NONE;
279     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
280     BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
281
282     BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
283     event = Scheduler::EV_NONE;
284     sleep(1);
285     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
286     BOOST_CHECK_EQUAL( event, Scheduler::EventId(Scheduler::EV_READ|Scheduler::EV_HUP) );
287     BOOST_REQUIRE_EQUAL( size, 2 );
288     buffer[size]=0;
289     BOOST_CHECK_EQUAL( buffer, "OK" );
290     BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle) );
291
292     unsigned tid (Scheduler::instance().timeout(
293                       ClockService::now()+ClockService::milliseconds(400),&timeout));
294     BOOST_CHECK_NO_THROW( Scheduler::instance().registerSignal(SIGUSR1, &sigusr) );
295     t = ClockService::now();
296     ::kill(::getpid(), SIGUSR1);
297     delay(100);
298     BOOST_CHECK_NO_THROW( Scheduler::instance().process() ); 
299     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
300     BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+ClockService::milliseconds(200)) );
301     Scheduler::instance().cancelTimeout(tid);
302     BOOST_CHECK_NO_THROW( Scheduler::instance().unregisterSignal(SIGUSR1) );
303
304     ///////////////////////////////////////////////////////////////////////////
305
306     close(sock);
307
308     BOOST_CHECK (stop_server(pid));
309 }
310
311 ///////////////////////////////cc.e////////////////////////////////////////
312 #undef prefix_
313
314 \f
315 // Local Variables:
316 // mode: c++
317 // fill-column: 100
318 // c-file-style: "senf"
319 // indent-tabs-mode: nil
320 // ispell-local-dictionary: "american"
321 // compile-command: "scons -u test"
322 // comment-column: 40
323 // End: