f3f498edf96df3cb91172ab2b803c9749a0deaa9
[senf.git] / Scheduler / Scheduler.test.cc
1
2 // $Id$
3 //
4 // Copyright (C) 2006
5 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
6 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
7 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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) < 10100000UL; // a little bit over 10ms
190     }
191
192 }
193
194 BOOST_AUTO_UNIT_TEST(scheduler)
195 {
196     int pid = start_server();
197     BOOST_REQUIRE (pid);
198
199     int sock = socket(PF_UNIX,SOCK_STREAM,0);
200     if (sock<0) {
201         error("socket");
202         BOOST_FAIL("socket");
203     }
204     struct sockaddr_un sun;
205     memset(&sun,0,sizeof(sun));
206     sun.sun_family = AF_UNIX;
207     strcpy(sun.sun_path,SOCK_PATH);
208
209     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
210         error("connect");
211         BOOST_FAIL("connect");
212     }
213
214     ///////////////////////////////////////////////////////////////////////////
215
216     BOOST_CHECK_NO_THROW( Scheduler::instance() );
217
218     BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,&callback,Scheduler::EV_READ) );
219     event = Scheduler::EV_NONE;
220     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
221     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
222     BOOST_REQUIRE_EQUAL( size, 4 );
223     buffer[size]=0;
224     BOOST_CHECK_EQUAL( buffer, "READ" );
225
226     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
227                               ClockService::now()+ClockService::milliseconds(100),&timeout) );
228     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(
229                               ClockService::now()+ClockService::milliseconds(200),&timeout) );
230     ClockService::clock_type t (ClockService::now());
231     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
232     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(100)) );
233     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
234     BOOST_CHECK_PREDICATE( is_close, (ClockService::now()) (t+ClockService::milliseconds(200)) );
235
236     HandleWrapper handle(sock,"TheTag");
237     BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,&handleCallback,Scheduler::EV_WRITE) );
238     strcpy(buffer,"WRITE");
239     size=5;
240     event = Scheduler::EV_NONE;
241     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
242     BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
243
244     BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
245     event = Scheduler::EV_NONE;
246     sleep(1);
247     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
248     BOOST_CHECK_EQUAL( event, Scheduler::EventId(Scheduler::EV_READ|Scheduler::EV_HUP) );
249     BOOST_REQUIRE_EQUAL( size, 2 );
250     buffer[size]=0;
251     BOOST_CHECK_EQUAL( buffer, "OK" );
252
253     ///////////////////////////////////////////////////////////////////////////
254
255     close(sock);
256
257     BOOST_CHECK (stop_server(pid));
258 }
259
260 ///////////////////////////////cc.e////////////////////////////////////////
261 #undef prefix_
262
263 \f
264 // Local Variables:
265 // mode: c++
266 // fill-column: 100
267 // c-file-style: "senf"
268 // indent-tabs-mode: nil
269 // ispell-local-dictionary: "american"
270 // compile-command: "scons -u test"
271 // comment-column: 40
272 // End: