Utils/Console: Fix singleton instantiation order (ServerManager / Scheduler)
[senf.git] / Utils / Daemon / Daemon.test.cc
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
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 #include "../Utils/Backtrace.hh"
39
40 #include "../Utils/auto_unit_test.hh"
41 #include <boost/test/test_tools.hpp>
42
43 #define prefix_
44 ///////////////////////////////cc.p////////////////////////////////////////
45
46 namespace {
47
48     void delay(unsigned long milliseconds)
49     {
50         struct timespec ts;
51         ts.tv_sec = milliseconds / 1000;
52         ts.tv_nsec = (milliseconds % 1000) * 1000000;
53         while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
54     }
55
56     class MyDaemon : public senf::Daemon
57     {
58         void configure() { 
59             std::cout << "Running configure()" << std::endl; 
60             pidFile("invalid.pid");
61             consoleLog("invalid.log");
62             senf::Daemon::configure();
63         }
64
65         void init() { 
66             std::cout << "Running init()" << std::endl; 
67         }
68
69         void run() {
70             std::cout << "Running run()" << std::endl; 
71             delay(1500);
72         }
73     };
74
75     int myMain(int argc, char ** argv)
76     {
77         MyDaemon instance;
78         return instance.start(argc, argv);
79     }
80
81     int pid;
82
83     void backtrace(int)
84     {
85         senf::backtrace(std::cerr, 100);
86         ::signal(SIGABRT, SIG_DFL);
87         ::kill(::getpid(), SIGABRT);
88     };
89
90     int run(int argc, char ** argv)
91     {
92         pid  = ::fork();
93         if (pid < 0) throw senf::SystemException("::fork()");
94         if (pid == 0) {
95             signal(SIGABRT, &backtrace);
96             try {
97                 ::_exit(myMain(argc, argv));
98             } catch (std::exception & ex) {
99                 std::cerr << "Unexpected exception: " << ex.what() << std::endl;
100             } catch (...) {
101                 std::cerr << "Unexpected exception" << std::endl;
102             }
103             ::_exit(2);
104         }
105         int status;
106         if (::waitpid(pid, &status, 0) < 0) throw senf::SystemException("::waitpid()");
107         return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
108     }
109
110 }
111
112 BOOST_AUTO_UNIT_TEST(testDaemon)
113 {
114     char * args[] = { "run", 
115                       "--console-log=testDaemon.log,none", 
116                       "--pid-file=testDaemon.pid" };
117     BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args),args), 0 );
118
119     BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
120     BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
121     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.pid") );
122     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
123     
124     boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
125     {
126         std::ifstream pidFile ("testDaemon.pid");
127         int pid (0);
128         BOOST_REQUIRE( pidFile >> pid );
129         BOOST_REQUIRE( pid != 0 );
130         ::kill(pid, SIGHUP);
131     }
132
133     delay(1000);
134     BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
135     BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
136     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log.1") );
137     
138     std::ifstream log ("testDaemon.log.1");
139     std::stringstream data;
140     data << log.rdbuf();
141     BOOST_CHECK_EQUAL( data.str(), "Running init()\nRunning run()\n" );
142     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
143     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
144 }
145
146 ///////////////////////////////cc.e////////////////////////////////////////
147 #undef prefix_
148
149 \f
150 // Local Variables:
151 // mode: c++
152 // fill-column: 100
153 // comment-column: 40
154 // c-file-style: "senf"
155 // indent-tabs-mode: nil
156 // ispell-local-dictionary: "american"
157 // compile-command: "scons -u test"
158 // End: