X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FDaemon%2FDaemon.test.cc;h=57a511539405550187aed48837f4fa6340ee73b1;hb=456ee576285b76aa46240f8001f426757810dcc1;hp=e456939f4624b42a764ff13b4f632d6f657229ca;hpb=1d247d12d1759ffd77f456efe3a52f03dd289994;p=senf.git diff --git a/Utils/Daemon/Daemon.test.cc b/Utils/Daemon/Daemon.test.cc index e456939..57a5115 100644 --- a/Utils/Daemon/Daemon.test.cc +++ b/Utils/Daemon/Daemon.test.cc @@ -71,18 +71,27 @@ namespace { } }; - int myMain(int argc, char const ** argv) + int myMain(int argc, char ** argv) { MyDaemon instance; return instance.start(argc, argv); } - int run(int argc, char const ** argv) + int pid; + + int run(int argc, char ** argv) { - int pid (::fork()); + pid = ::fork(); if (pid < 0) throw senf::SystemException("::fork()"); if (pid == 0) { - ::_exit(myMain(argc, argv)); + try { + ::_exit(myMain(argc, argv)); + } catch (std::exception & ex) { + std::cerr << "Unexpected exception: " << ex.what() << std::endl; + } catch (...) { + std::cerr << "Unexpected exception" << std::endl; + } + ::_exit(2); } int status; if (::waitpid(pid, &status, 0) < 0) throw senf::SystemException("::waitpid()"); @@ -93,23 +102,36 @@ namespace { BOOST_AUTO_UNIT_TEST(testDaemon) { - char const * args[] = { "run", - "--console-log=testDaemon.log,none", - "--pid-file=testDaemon.pid" }; + char * args[] = { "run", + "--console-log=testDaemon.log,none", + "--pid-file=testDaemon.pid" }; BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args),args), 0 ); BOOST_CHECK( ! boost::filesystem::exists("invalid.log") ); BOOST_CHECK( ! boost::filesystem::exists("invalid.pid") ); - BOOST_CHECK( boost::filesystem::exists("testDaemon.pid") ); + BOOST_REQUIRE( boost::filesystem::exists("testDaemon.pid") ); + BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") ); + + boost::filesystem::rename("testDaemon.log", "testDaemon.log.1"); + { + std::ifstream pidFile ("testDaemon.pid"); + int pid (0); + BOOST_REQUIRE( pidFile >> pid ); + BOOST_REQUIRE( pid != 0 ); + ::kill(pid, SIGHUP); + } + delay(1000); BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") ); - BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") ); + BOOST_CHECK( boost::filesystem::exists("testDaemon.log") ); + BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log.1") ); - std::ifstream log ("testDaemon.log"); + std::ifstream log ("testDaemon.log.1"); std::stringstream data; data << log.rdbuf(); BOOST_CHECK_EQUAL( data.str(), "Running init()\nRunning run()\n" ); BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") ); + BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") ); } ///////////////////////////////cc.e////////////////////////////////////////