git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@129 270642c3-0616-0410...
[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 satcom::lib;
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     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
187 BOOST_AUTO_UNIT_TEST(scheduler)
188 {
189     int pid = start_server();
190     BOOST_REQUIRE (pid);
191
192     int sock = socket(PF_UNIX,SOCK_STREAM,0);
193     if (sock<0) {
194         error("socket");
195         BOOST_FAIL("socket");
196     }
197     struct sockaddr_un sun;
198     memset(&sun,0,sizeof(sun));
199     sun.sun_family = AF_UNIX;
200     strcpy(sun.sun_path,SOCK_PATH);
201     
202     if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
203         error("connect");
204         BOOST_FAIL("connect");
205     }
206
207     ///////////////////////////////////////////////////////////////////////////
208
209     BOOST_CHECK_NO_THROW( Scheduler::instance() );
210
211     BOOST_CHECK_NO_THROW( Scheduler::instance().add(sock,&callback,Scheduler::EV_READ) );
212     event = Scheduler::EV_NONE;
213     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
214     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
215     BOOST_REQUIRE_EQUAL( size, 4 );
216     buffer[size]=0;
217     BOOST_CHECK_EQUAL( buffer, "READ" );
218
219     HandleWrapper handle(sock,"TheTag");
220     BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,&handleCallback,Scheduler::EV_WRITE) );
221     strcpy(buffer,"WRITE");
222     size=5;
223     event = Scheduler::EV_NONE;
224     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
225     BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
226
227     BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
228     event = Scheduler::EV_NONE;
229     BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
230     BOOST_CHECK_EQUAL( event, Scheduler::EV_READ );
231     BOOST_REQUIRE_EQUAL( size, 2 );
232     buffer[size]=0;
233     BOOST_CHECK_EQUAL( buffer, "OK" );
234
235     ///////////////////////////////////////////////////////////////////////////
236
237     close(sock);
238
239     BOOST_CHECK (stop_server(pid));
240 }
241
242 ///////////////////////////////cc.e////////////////////////////////////////
243 #undef prefix_
244
245 \f
246 // Local Variables:
247 // mode: c++
248 // c-file-style: "satcom"
249 // End: