9c6b22f998312e8c519936427b4b3a3ecb636668
[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 <boost/filesystem/operations.hpp>
35 #include "Daemon.hh"
36 #include "../Utils/Exception.hh"
37
38 #include <boost/test/auto_unit_test.hpp>
39 #include <boost/test/test_tools.hpp>
40
41 #define prefix_
42 ///////////////////////////////cc.p////////////////////////////////////////
43
44 namespace {
45
46     void delay(unsigned long milliseconds)
47     {
48         struct timespec ts;
49         ts.tv_sec = milliseconds / 1000;
50         ts.tv_nsec = (milliseconds % 1000) * 1000000;
51         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
52     }
53
54     class MyDaemon : public senf::Daemon
55     {
56         void configure() { 
57             std::cout << "Running configure()" << std::endl; 
58             pidFile("testDaemon.pid");
59         }
60
61         void init() { 
62             std::cout << "Running init()" << std::endl; 
63         }
64
65         void run() {
66             std::cout << "Running run()" << std::endl; 
67             delay(1500);
68         }
69     };
70
71     int myMain(int argc, char const ** argv)
72     {
73         MyDaemon instance;
74         return instance.start(argc, argv);
75     }
76
77     int run(int argc, char const ** argv)
78     {
79         int pid (::fork());
80         if (pid < 0) senf::throwErrno("::fork()");
81         if (pid == 0) {
82             ::_exit(myMain(argc, argv));
83         }
84         int status;
85         if (::waitpid(pid, &status, 0) < 0) senf::throwErrno("::waitpid()");
86          return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
87     }
88
89 }
90
91 BOOST_AUTO_UNIT_TEST(testDaemon)
92 {
93     char const * args[] = { "run", 0 };
94     BOOST_CHECK_EQUAL( run(1,args), 0 );
95
96     BOOST_CHECK( boost::filesystem::exists("testDaemon.pid") );
97     delay(1000);
98     BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
99 }
100
101 ///////////////////////////////cc.e////////////////////////////////////////
102 #undef prefix_
103
104 \f
105 // Local Variables:
106 // mode: c++
107 // fill-column: 100
108 // comment-column: 40
109 // c-file-style: "senf"
110 // indent-tabs-mode: nil
111 // ispell-local-dictionary: "american"
112 // compile-command: "scons -u test"
113 // End: