Utils/Daemon: Add instance() and logReopen() member and bind SIGHUP to logReopen()
[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
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("invalid.pid");
60             consoleLog("invalid.log");
61             senf::Daemon::configure();
62         }
63
64         void init() { 
65             std::cout << "Running init()" << std::endl; 
66         }
67
68         void run() {
69             std::cout << "Running run()" << std::endl; 
70             delay(1500);
71         }
72     };
73
74     int myMain(int argc, char ** argv)
75     {
76         MyDaemon instance;
77         return instance.start(argc, argv);
78     }
79
80     int pid;
81
82     int run(int argc, char ** argv)
83     {
84         pid  = ::fork();
85         if (pid < 0) throw senf::SystemException("::fork()");
86         if (pid == 0) {
87             ::_exit(myMain(argc, argv));
88         }
89         int status;
90         if (::waitpid(pid, &status, 0) < 0) throw senf::SystemException("::waitpid()");
91         return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
92     }
93
94 }
95
96 BOOST_AUTO_UNIT_TEST(testDaemon)
97 {
98     char * args[] = { "run", 
99                       "--console-log=testDaemon.log,none", 
100                       "--pid-file=testDaemon.pid" };
101     BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args),args), 0 );
102
103     BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
104     BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
105     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.pid") );
106     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
107     
108     boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
109     {
110         std::ifstream pidFile ("testDaemon.pid");
111         int pid (0);
112         BOOST_REQUIRE( pidFile >> pid );
113         BOOST_REQUIRE( pid != 0 );
114         ::kill(pid, SIGHUP);
115     }
116
117     delay(1000);
118     BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
119     BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
120     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log.1") );
121     
122     std::ifstream log ("testDaemon.log.1");
123     std::stringstream data;
124     data << log.rdbuf();
125     BOOST_CHECK_EQUAL( data.str(), "Running init()\nRunning run()\n" );
126     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
127     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
128 }
129
130 ///////////////////////////////cc.e////////////////////////////////////////
131 #undef prefix_
132
133 \f
134 // Local Variables:
135 // mode: c++
136 // fill-column: 100
137 // comment-column: 40
138 // c-file-style: "senf"
139 // indent-tabs-mode: nil
140 // ispell-local-dictionary: "american"
141 // compile-command: "scons -u test"
142 // End: