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