4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief Daemon unit tests */
31 //#include "Daemon.test.hh"
32 //#include "Daemon.test.ih"
36 #include <sys/types.h>
40 #include <boost/filesystem/operations.hpp>
42 #include <senf/Utils/Exception.hh>
43 #include <senf/Utils/Backtrace.hh>
44 #include <senf/Scheduler/Scheduler.hh>
46 #include <senf/Utils/auto_unit_test.hh>
47 #include <boost/test/test_tools.hpp>
50 //-/////////////////////////////////////////////////////////////////////////////////////////////////
54 void delay(unsigned long milliseconds)
57 ts.tv_sec = milliseconds / 1000;
58 ts.tv_nsec = (milliseconds % 1000) * 1000000;
59 while (nanosleep(&ts,&ts) < 0 && errno == EINTR) ;
62 class MyDaemon : public senf::Daemon
65 std::cout << "Running configure()" << std::endl;
66 pidFile("invalid.pid");
67 consoleLog("invalid.log");
68 senf::Daemon::configure();
72 std::cout << "Running init()" << std::endl;
73 std::cerr << "(stderr)" << std::endl;
77 std::cout << "Running run()" << std::endl;
82 int myMain(int argc, char const ** argv)
85 return instance.start(argc, argv);
92 senf::backtrace(std::cerr, 100);
93 ::signal(SIGABRT, SIG_DFL);
94 ::kill(::getpid(), SIGABRT);
97 int run(int argc, char const ** argv)
100 if (pid < 0) throw senf::SystemException("::fork()");
102 signal(SIGABRT, &backtrace);
103 signal(SIGCHLD, SIG_IGN);
105 ::_exit(myMain(argc, argv));
106 } catch (std::exception & ex) {
107 std::cerr << "Unexpected exception: " << ex.what() << std::endl;
109 std::cerr << "Unexpected exception" << std::endl;
113 signal(SIGCHLD, SIG_DFL);
115 if (::waitpid(pid, &status, 0) < 0)
116 throw senf::SystemException("::waitpid()");
117 if (WIFSIGNALED(status))
118 std::cerr << "Terminated with signal "
119 << senf::signalName(WTERMSIG(status)) << "(" << WTERMSIG(status) << ")\n";
120 else if (WIFEXITED(status))
121 std::cerr << "Exited normally with exit status " << WEXITSTATUS(status) << "\n";
127 SENF_AUTO_UNIT_TEST(testDaemon)
129 char const * args[] = { "run",
130 "--console-log=testDaemon.log",
131 "--pid-file=testDaemon.pid" };
133 SENF_CHECK_NO_THROW( BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args), args), 0 ) );
135 BOOST_CHECK( ! boost::filesystem::exists("invalid.log") );
136 BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") );
137 BOOST_CHECK( boost::filesystem::exists("testDaemon.pid") );
138 BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
140 boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
142 std::ifstream pidFile ("testDaemon.pid");
144 BOOST_CHECK( pidFile >> pid );
145 BOOST_CHECK( pid != 0 );
151 BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
152 BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
153 BOOST_CHECK( boost::filesystem::exists("testDaemon.log.1") );
155 std::ifstream log ("testDaemon.log.1");
156 std::stringstream data;
158 BOOST_CHECK_EQUAL( data.str(), "Running init()\n(stderr)\nRunning run()\n" );
159 SENF_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
160 SENF_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
163 //-/////////////////////////////////////////////////////////////////////////////////////////////////
170 // comment-column: 40
171 // c-file-style: "senf"
172 // indent-tabs-mode: nil
173 // ispell-local-dictionary: "american"
174 // compile-command: "scons -u test"