418ea9f59a7f3afb001a6bf51de2c41dc85a8943
[senf.git] / Scheduler / Daemon.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer NETwork research (NET)
6 //     Stefan Bund <g0dil@berlios.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 /** \file
24     \brief Daemon.test unit tests */
25
26 //#include "Daemon.test.hh"
27 //#include "Daemon.test.ih"
28
29 // Custom includes
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <iostream>
34 #include "Daemon.hh"
35 #include "../Utils/Exception.hh"
36
37 #include <boost/test/auto_unit_test.hpp>
38 #include <boost/test/test_tools.hpp>
39
40 #define prefix_
41 ///////////////////////////////cc.p////////////////////////////////////////
42
43 namespace {
44
45     void delay(unsigned long milliseconds)
46     {
47         struct timespec ts;
48         ts.tv_sec = milliseconds / 1000;
49         ts.tv_nsec = (milliseconds % 1000) * 1000000;
50         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
51     }
52
53     class MyDaemon : public senf::Daemon
54     {
55         void configure() { std::cout << "Running configure()" << std::endl; }
56         void init() { std::cout << "Running init()" << std::endl; }
57         void run() {
58             delay(2000);
59             std::cout << "Running run()" << std::endl; 
60         }
61     };
62
63     int myMain(int argc, char const ** argv)
64     {
65         MyDaemon instance;
66         return instance.start(argc, argv);
67     }
68
69     int run(int argc, char const ** argv)
70     {
71         int pid (::fork());
72         if (pid < 0) senf::throwErrno("::fork()");
73         if (pid == 0) {
74             ::_exit(myMain(argc, argv));
75         }
76         int status;
77         if (::waitpid(pid, &status, 0) < 0) senf::throwErrno("::waitpid()");
78          return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
79     }
80
81 }
82
83 BOOST_AUTO_UNIT_TEST(testDaemon)
84 {
85     char const * args[] = { "run", 0 };
86     BOOST_CHECK_EQUAL( run(1,args), 0 );
87 }
88
89 ///////////////////////////////cc.e////////////////////////////////////////
90 #undef prefix_
91
92 \f
93 // Local Variables:
94 // mode: c++
95 // fill-column: 100
96 // comment-column: 40
97 // c-file-style: "senf"
98 // indent-tabs-mode: nil
99 // ispell-local-dictionary: "american"
100 // compile-command: "scons -u test"
101 // End: