5 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
6 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
7 // Stefan Bund <stefan.bund@fokus.fraunhofer.de>
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.
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.
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.
26 //#include "scheduler.test.hh"
27 //#include "scheduler.test.ih"
30 #include <sys/types.h>
34 #include <sys/socket.h>
40 #include "Scheduler.hh"
42 #include <boost/test/auto_unit_test.hpp>
43 #include <boost/test/test_tools.hpp>
46 ///////////////////////////////cc.p////////////////////////////////////////
52 char const * SOCK_PATH = "/tmp/sched_test.sock";
54 void error(char const * fn, char const * proc="")
56 std::cerr << "\n" << proc << fn << ": " << strerror(errno) << std::endl;
59 void fail(char const * fn)
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");
78 ///////////////////////////////////////////////////////////////////////////
80 if (write(conn,"READ",4)<0) fail("write");
82 int size = read(conn,buffer,1024);
83 if (size<0) fail("read");
86 if (strcmp(buffer,"WRITE")==0) {
87 if (write(conn,"OK",2)<0) fail("write");
89 if (write(conn,"FAIL",4)<0) fail("write");
91 if (write(conn,"FAIL",4)<0) fail("write");
93 ///////////////////////////////////////////////////////////////////////////
112 sleep(1); // Wait for the server socket to be opened
116 bool stop_server(int pid)
118 sleep(1); // Wait for the server to terminate
119 if (kill(pid,SIGTERM)<0) {
124 if (waitpid(pid,&status,0)<0) {
129 if (WIFSIGNALED(status)) {
130 std::cerr << "\nserver terminated with signal " << WTERMSIG(status) << std::endl;
133 if (WEXITSTATUS(status)!=0) {
134 std::cerr << "\nserver terminated with exit status " << WEXITSTATUS(status) << std::endl;
144 void callback(int fd, Scheduler::EventId ev)
147 switch (event & Scheduler::EV_ALL) {
148 case Scheduler::EV_READ:
149 size = recv(fd,buffer,1024,0);
151 case Scheduler::EV_PRIO:
152 size = recv(fd,buffer,1024,MSG_OOB);
153 Scheduler::instance().terminate();
155 case Scheduler::EV_WRITE:
156 size = write(fd,buffer,size);
157 Scheduler::instance().terminate();
160 Scheduler::instance().terminate();
165 Scheduler::instance().terminate();
170 HandleWrapper(int fd,std::string const & tag) : fd_(fd), tag_(tag) {}
175 int retrieve_filehandle(HandleWrapper const & handle)
180 void handleCallback(HandleWrapper const & handle, Scheduler::EventId event)
182 if (handle.tag_ != "TheTag")
184 callback(handle.fd_,event);
187 bool is_close(ClockService::clock_type a, ClockService::clock_type b)
189 return (a<b ? b-a : a-b) < 10100000UL; // a little bit over 10ms
194 BOOST_AUTO_UNIT_TEST(scheduler)
196 int pid = start_server();
199 int sock = socket(PF_UNIX,SOCK_STREAM,0);
202 BOOST_FAIL("socket");
204 struct sockaddr_un sun;
205 memset(&sun,0,sizeof(sun));
206 sun.sun_family = AF_UNIX;
207 strcpy(sun.sun_path,SOCK_PATH);
209 if (connect(sock,(struct sockaddr*)&sun,sizeof(sun))<0) {
211 BOOST_FAIL("connect");
214 ///////////////////////////////////////////////////////////////////////////
216 BOOST_CHECK_NO_THROW( Scheduler::instance() );
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 );
224 BOOST_CHECK_EQUAL( buffer, "READ" );
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)) );
236 HandleWrapper handle(sock,"TheTag");
237 BOOST_CHECK_NO_THROW( Scheduler::instance().add(handle,&handleCallback,Scheduler::EV_WRITE) );
238 strcpy(buffer,"WRITE");
240 event = Scheduler::EV_NONE;
241 BOOST_CHECK_NO_THROW( Scheduler::instance().process() );
242 BOOST_CHECK_EQUAL( event, Scheduler::EV_WRITE );
244 BOOST_CHECK_NO_THROW( Scheduler::instance().remove(handle,Scheduler::EV_WRITE) );
245 event = Scheduler::EV_NONE;
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 );
251 BOOST_CHECK_EQUAL( buffer, "OK" );
253 ///////////////////////////////////////////////////////////////////////////
257 BOOST_CHECK (stop_server(pid));
260 ///////////////////////////////cc.e////////////////////////////////////////
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