Add new file-variable 'comment-column'
[senf.git] / Scheduler / Scheduler.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2006
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 //     Stefan Bund <stefan.bund@fokus.fraunhofer.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
39 #include "Scheduler.hh"
40
41 #include <boost/test/auto_unit_test.hpp>
42 #include <boost/test/test_tools.hpp>
43
44 #define prefix_
45 ///////////////////////////////cc.p////////////////////////////////////////
46
47 using namespace senf;
48
49 namespace {
50
51     char const * SOCK_PATH = "/tmp/sched_test.sock";
52
53     void error(char const * fn, char const * proc="")
54     {
55         std::cerr << "\n" << proc << fn << ": " << strerror(errno) << std::endl;
56     }
57
58     void fail(char const * fn)
59     {
60         error(fn,"server: ");
61         _exit(1);
62     }
63
64     void server()
65     {
66         int sock = socket(PF_UNIX,SOCK_STREAM,0);
67         if (sock<0) fail("socket");
68         struct sockaddr_un sun;
69         memset(&sun,0,sizeof(sun));
70         sun.sun_family = AF_UNIX;
71         strcpy(sun.sun_path,SOCK_PATH);
72         if (bind(sock,(struct sockaddr*)&sun,sizeof(sun))<0) fail("bind");
73         if (listen(sock,1)<0) fail("listen");
74         int conn = accept(sock,0,0);
75         if (conn < 0) fail("accept");
76
77         ///////////////////////////////////////////////////////////////////////////
78
79         if (write(conn,"READ",4)<0) fail("write");
80         char buffer[1024];
81         int size =  read(conn,buffer,1024);
82         if (size<0) fail("read");
83         if (size == 5) {
84             buffer[5] = 0;
85             if (strcmp(buffer,"WRITE")==0) {
86                 if (write(conn,"OK",2)<0) fail("write");
87             } else
88                 if (write(conn,"FAIL",4)<0) fail("write");
89         } else
90             if (write(conn,"FAIL",4)<0) fail("write");
91
92         ///////////////////////////////////////////////////////////////////////////
93
94         close(conn);
95         close(sock);
96     }
97
98     int start_server()
99     {
100         unlink(SOCK_PATH);
101         int pid = fork();
102         if (pid == 0) {
103             server();
104             _exit(0);
105         }
106         if (pid < 0) {
107             error("fork");
108             return 0;
109         }
110
111         sleep(1); // Wait for the server socket to be opened
112         return pid;
113     }
114
115     bool stop_server(int pid)
116     {
117         sleep(1); // Wait for the server to terminate
118         if (kill(pid,SIGTERM)<0) {
119             error("kill");
120             return false;
121         }
122         int status = 0;
123         if (waitpid(pid,&status,0)<0) {
124             error("waitpid");
125             return false;
126         }
127         unlink(SOCK_PATH);
128         if (WIFSIGNALED(status)) {
129             std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
130             return false;
131         }
132         if (WEXITSTATUS(status)!=0) {
133             std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
134             return false;
135         }
136         return true;
137     }
138
139     char buffer[1024];
140     int size;
141     int event;
142
143     void callback(int fd, Scheduler::EventId ev)
144     {
145         event = ev;
146         switch (event & Scheduler::EV_ALL) {
147         case Scheduler::EV_READ:
148             size = recv(fd,buffer,1024,0);
149             break;
150         case Scheduler::EV_PRIO:
151             size = recv(fd,buffer,1024,MSG_OOB);
152             Scheduler::instance().terminate();
153             break;
154         case Scheduler::EV_WRITE:
155             size = write(fd,buffer,size);
156             Scheduler::instance().terminate();
157             break;
158         }
159         Scheduler::instance().terminate();
160     }
161
162     void timeout()
163     {
164         Scheduler::instance().terminate();
165     }
166
167     struct HandleWrapper
168     {
169         HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
170         int fd_;
171         std::string tag_;
172     };
173
174     int retrieve_filehandle(HandleWrapper const & handle)
175     {
176         return handle.fd_;
177     }
178
179     void handleCallback(HandleWrapper const & handle, Scheduler::EventId event)
180     {
181         if (handle.tag_ != "TheTag")
182             return;
183         callback(handle.fd_,event);
184     }
185
186     bool is_close(MicroTime a, MicroTime b)
187     {
188         return (a<b ? b-a : a-b) < 10100; // a little bit over 10ms
189     }
190
191 }
192
193 BOOST_AUTO_UNIT_TEST(scheduler)
194 {
195     int pid = start_server();
196     BOOST_REQUIRE (pid);
197
198     int sock = socket(PF_UNIX,SOCK_STREAM,0);
199     if (sock<0) {
200         error("socket");
201         BOOST_FAIL("socket");
202     }
203     struct sockaddr_un sun;
204     memset(&sun,0,sizeof(sun));
205     sun.sun_family = AF_UNIX;
206     strcpy(sun.sun_path,SOCK_PATH);
207
208     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
209         error("connect");
210         BOOST_FAIL("connect");
211     }
212
213     ///////////////////////////////////////////////////////////////////////////
214
215     BOOST_CHECK_NO_THROW( Scheduler::instance() );
216
217     BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,&callback,Scheduler::EV_READ) );
218     event = Scheduler::EV_NONE;
219     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
220     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
221     BOOST_REQUIRE_EQUAL( size, 4 );
222     buffer[size]=0;
223     BOOST_CHECK_EQUAL( buffer, "READ" );
224
225     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(100,&timeout) );
226     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(200,&timeout) );
227     MicroTime t (now());
228     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
229     BOOST_CHECK_PREDICATE( is_close, (now()) (t+100*1000) );
230     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
231     BOOST_CHECK_PREDICATE( is_close, (now()) (t+200*1000) );
232
233     HandleWrapper handle(sock,"TheTag");
234     BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,&handleCallback,Scheduler::EV_WRITE) );
235     strcpy(buffer,"WRITE");
236     size=5;
237     event = Scheduler::EV_NONE;
238     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
239     BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
240
241     BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
242     event = Scheduler::EV_NONE;
243     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
244     BOOST_CHECK_EQUAL( event, Scheduler::EventId(Scheduler::EV_READ|Scheduler::EV_HUP) );
245     BOOST_REQUIRE_EQUAL( size, 2 );
246     buffer[size]=0;
247     BOOST_CHECK_EQUAL( buffer, "OK" );
248
249     ///////////////////////////////////////////////////////////////////////////
250
251     close(sock);
252
253     BOOST_CHECK (stop_server(pid));
254 }
255
256 ///////////////////////////////cc.e////////////////////////////////////////
257 #undef prefix_
258
259 \f
260 // Local Variables:
261 // mode: c++
262 // fill-column: 100
263 // c-file-style: "senf"
264 // indent-tabs-mode: nil
265 // ispell-local-dictionary: "american"
266 // compile-command: "scons -u test"
267 // comment-column: 40
268 // End: