ae95d780aaaeeb6275bfe4856d6354fc580ef04d
[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, Scheduler::EventId 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     void timeout()
164     {
165         Scheduler::instance().terminate();
166     }
167
168     struct HandleWrapper
169     {
170         HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
171         int fd_;
172         std::string tag_;
173     };
174
175     int retrieve_filehandle(HandleWrapper const & handle)
176     {
177         return handle.fd_;
178     }
179
180     void handleCallback(HandleWrapper const & handle, Scheduler::EventId event)
181     {
182         if (handle.tag_ != "TheTag")
183             return;
184         callback(handle.fd_,event);
185     }
186
187     bool is_close(ClockService::clock_type a, ClockService::clock_type b)
188     {
189         return (a<b ? b-a : a-b) < ClockService::milliseconds(100);
190     }
191     
192     ClockService::clock_type sigtime (0);
193
194     void sigusr()
195     {
196         sigtime = ClockService::now();
197         Scheduler::instance().terminate();
198     }
199         
200     void delay(unsigned long milliseconds)
201     {
202         struct timespec ts;
203         ts.tv_sec = milliseconds / 1000;
204         ts.tv_nsec = (milliseconds % 1000) * 1000000;
205         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
206     }
207 }
208
209 BOOST_AUTO_UNIT_TEST(scheduler)
210 {
211     int pid = start_server();
212     BOOST_REQUIRE (pid);
213
214     int sock = socket(PF_UNIX,SOCK_STREAM,0);
215     if (sock<0) {
216         error("socket");
217         BOOST_FAIL("socket");
218     }
219     struct sockaddr_un sun;
220     memset(&sun,0,sizeof(sun));
221     sun.sun_family = AF_UNIX;
222     strcpy(sun.sun_path,SOCK_PATH);
223
224     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
225         error("connect");
226         BOOST_FAIL("connect");
227     }
228
229     ///////////////////////////////////////////////////////////////////////////
230
231     BOOST_CHECK_NO_THROW( Scheduler::instance() );
232
233     BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,boost::bind(&callback, sock, _1),
234                                                     Scheduler::EV_READ) );
235     event = Scheduler::EV_NONE;
236     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
237     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
238     BOOST_REQUIRE_EQUAL( size, 4 );
239     buffer[size]=0;
240     BOOST_CHECK_EQUAL( buffer, "READ" );
241
242     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
243                               ClockService::now()+ClockService::milliseconds(200),&timeout) );
244     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
245                               ClockService::now()+ClockService::milliseconds(400),&timeout) );
246     ClockService::clock_type t (ClockService::now());
247     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
248     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
249     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
250     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(400)) );
251
252     HandleWrapper handle(sock,"TheTag");
253     BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,
254                                                     boost::bind(&handleCallback,handle,_1),
255                                                     Scheduler::EV_WRITE) );
256     strcpy(buffer,"WRITE");
257     size=5;
258     event = Scheduler::EV_NONE;
259     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
260     BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
261
262     BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
263     event = Scheduler::EV_NONE;
264     sleep(1);
265     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
266     BOOST_CHECK_EQUAL( event, Scheduler::EventId(Scheduler::EV_READ|Scheduler::EV_HUP) );
267     BOOST_REQUIRE_EQUAL( size, 2 );
268     buffer[size]=0;
269     BOOST_CHECK_EQUAL( buffer, "OK" );
270     BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle) );
271
272     unsigned tid (Scheduler::instance().timeout(
273                       ClockService::now()+ClockService::milliseconds(400),&timeout));
274     BOOST_CHECK_NO_THROW( Scheduler::instance().registerSignal(SIGUSR1, &sigusr) );
275     t = ClockService::now();
276     ::kill(::getpid(), SIGUSR1);
277     delay(100);
278     BOOST_CHECK_NO_THROW( Scheduler::instance().process() ); 
279     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
280     BOOST_CHECK_PREDICATE( is_close, (sigtime) (t+ClockService::milliseconds(200)) );
281     Scheduler::instance().cancelTimeout(tid);
282     BOOST_CHECK_NO_THROW( Scheduler::instance().unregisterSignal(SIGUSR1) );
283
284     ///////////////////////////////////////////////////////////////////////////
285
286     close(sock);
287
288     BOOST_CHECK (stop_server(pid));
289 }
290
291 ///////////////////////////////cc.e////////////////////////////////////////
292 #undef prefix_
293
294 \f
295 // Local Variables:
296 // mode: c++
297 // fill-column: 100
298 // c-file-style: "senf"
299 // indent-tabs-mode: nil
300 // ispell-local-dictionary: "american"
301 // compile-command: "scons -u test"
302 // comment-column: 40
303 // End: