4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Daemon.test unit tests */
26 //#include "Daemon.test.hh"
27 //#include "Daemon.test.ih"
31 #include <sys/types.h>
35 #include <boost/filesystem/operations.hpp>
37 #include "../Utils/Exception.hh"
38 #include "../Utils/Backtrace.hh"
39 #include "../Scheduler/Scheduler.hh"
41 #include "../Utils/auto_unit_test.hh"
42 #include <boost/test/test_tools.hpp>
45 ///////////////////////////////cc.p////////////////////////////////////////
49 void delay(unsigned long milliseconds)
52 ts.tv_sec = milliseconds / 1000;
53 ts.tv_nsec = (milliseconds % 1000) * 1000000;
54 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
57 class MyDaemon : public senf::Daemon
60 std::cout << "Running configure()" << std::endl;
61 pidFile("invalid.pid");
62 consoleLog("invalid.log");
63 senf::Daemon::configure();
67 std::cout << "Running init()" << std::endl;
68 std::cerr << "(stderr)" << std::endl;
72 std::cout << "Running run()" << std::endl;
77 int myMain(int argc, char const ** argv)
80 return instance.start(argc, argv);
87 senf::backtrace(std::cerr, 100);
88 ::signal(SIGABRT, SIG_DFL);
89 ::kill(::getpid(), SIGABRT);
92 int run(int argc, char const ** argv)
95 if (pid < 0) throw senf::SystemException("::fork()");
97 signal(SIGABRT, &backtrace);
98 signal(SIGCHLD, SIG_IGN);
100 ::_exit(myMain(argc, argv));
101 } catch (std::exception & ex) {
102 std::cerr << "Unexpected exception: " << ex.what() << std::endl;
104 std::cerr << "Unexpected exception" << std::endl;
108 signal(SIGCHLD, SIG_DFL);
110 if (::waitpid(pid, &status, 0) < 0)
111 throw senf::SystemException("::waitpid()");
112 if (WIFSIGNALED(status))
113 std::cerr << "Terminated with signal "
114 << senf::signalName(WTERMSIG(status)) << "(" << WTERMSIG(status) << ")\n";
115 else if (WIFEXITED(status))
116 std::cerr << "Exited normally with exit status " << WEXITSTATUS(status) << "\n";
122 BOOST_AUTO_UNIT_TEST(testDaemon)
124 char const * args[] = { "run",
125 "--console-log=testDaemon.log",
126 "--pid-file=testDaemon.pid" };
128 SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args), args), 0 ) );
130 BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
131 BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
132 BOOST_CHECK( boost::filesystem::exists("testDaemon.pid") );
133 BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
135 boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
137 std::ifstream pidFile ("testDaemon.pid");
139 BOOST_CHECK( pidFile >> pid );
140 BOOST_CHECK( pid != 0 );
146 BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
147 BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
148 BOOST_CHECK( boost::filesystem::exists("testDaemon.log.1") );
150 std::ifstream log ("testDaemon.log.1");
151 std::stringstream data;
153 BOOST_CHECK_EQUAL( data.str(), "Running init()\n(stderr)\nRunning run()\n" );
154 SENF_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
155 SENF_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
158 ///////////////////////////////cc.e////////////////////////////////////////
165 // comment-column: 40
166 // c-file-style: "senf"
167 // indent-tabs-mode: nil
168 // ispell-local-dictionary: "american"
169 // compile-command: "scons -u test"