6665f6e5dc13d3873b4d37fd475c400ea0e865d6
[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) {
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         case Scheduler::EV_HUP:
159         case Scheduler::EV_ERR:
160         case Scheduler::EV_NONE:
161         case Scheduler::EV_ALL:
162             ;
163         }
164         Scheduler::instance().terminate();
165     }
166
167     void timeout() 
168     {
169         Scheduler::instance().terminate();
170     }
171      
172     struct HandleWrapper
173     {
174         HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
175         int fd_;
176         std::string tag_;
177     };
178
179     int retrieve_filehandle(HandleWrapper const & handle)
180     {
181         return handle.fd_;
182     }
183
184     void handleCallback(HandleWrapper const & handle, Scheduler::EventId event)
185     {
186         if (handle.tag_ != "TheTag")
187             return;
188         callback(handle.fd_,event);
189     }
190
191     bool is_close(MicroTime a, MicroTime b)
192     {
193         return (a<b ? b-a : a-b) < 10100; // a little bit over 10ms
194     }
195             
196 }
197
198 BOOST_AUTO_UNIT_TEST(scheduler)
199 {
200     int pid = start_server();
201     BOOST_REQUIRE (pid);
202
203     int sock = socket(PF_UNIX,SOCK_STREAM,0);
204     if (sock<0) {
205         error("socket");
206         BOOST_FAIL("socket");
207     }
208     struct sockaddr_un sun;
209     memset(&sun,0,sizeof(sun));
210     sun.sun_family = AF_UNIX;
211     strcpy(sun.sun_path,SOCK_PATH);
212     
213     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
214         error("connect");
215         BOOST_FAIL("connect");
216     }
217
218     ///////////////////////////////////////////////////////////////////////////
219
220     BOOST_CHECK_NO_THROW( Scheduler::instance() );
221
222     BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,&callback,Scheduler::EV_READ) );
223     event = Scheduler::EV_NONE;
224     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
225     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
226     BOOST_REQUIRE_EQUAL( size, 4 );
227     buffer[size]=0;
228     BOOST_CHECK_EQUAL( buffer, "READ" );
229
230     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(100,&timeout) );
231     BOOST_CHECK_NO_THROW( Scheduler::instance().timeout(200,&timeout) );
232     MicroTime t (now());
233     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
234     BOOST_CHECK_PREDICATE( is_close, (now()) (t+100*1000) );
235     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
236     BOOST_CHECK_PREDICATE( is_close, (now()) (t+200*1000) );
237     
238     HandleWrapper handle(sock,"TheTag");
239     BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,&handleCallback,Scheduler::EV_WRITE) );
240     strcpy(buffer,"WRITE");
241     size=5;
242     event = Scheduler::EV_NONE;
243     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
244     BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
245
246     BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
247     event = Scheduler::EV_NONE;
248     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
249     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
250     BOOST_REQUIRE_EQUAL( size, 2 );
251     buffer[size]=0;
252     BOOST_CHECK_EQUAL( buffer, "OK" );
253
254     ///////////////////////////////////////////////////////////////////////////
255
256     close(sock);
257
258     BOOST_CHECK (stop_server(pid));
259 }
260
261 ///////////////////////////////cc.e////////////////////////////////////////
262 #undef prefix_
263
264 \f
265 // Local Variables:
266 // mode: c++
267 // c-file-style: "senf"
268 // End: