Scheduler: Implement PollTimerSource
[senf.git] / Utils / Daemon / Daemon.test.cc
index 1a5990b..c814ffd 100644 (file)
@@ -35,6 +35,8 @@
 #include <boost/filesystem/operations.hpp>
 #include "Daemon.hh"
 #include "../Utils/Exception.hh"
+#include "../Utils/Backtrace.hh"
+#include "../Scheduler/Scheduler.hh"
 
 #include "../Utils/auto_unit_test.hh"
 #include <boost/test/test_tools.hpp>
@@ -63,11 +65,12 @@ namespace {
 
         void init() { 
             std::cout << "Running init()" << std::endl; 
+            std::cerr << "(stderr)" << std::endl;
         }
 
         void run() {
             std::cout << "Running run()" << std::endl; 
-            delay(1500);
+            delay(2000);
         }
     };
 
@@ -77,16 +80,41 @@ namespace {
         return instance.start(argc, argv);
     }
 
+    int pid;
+
+    void backtrace(int)
+    {
+        senf::backtrace(std::cerr, 100);
+        ::signal(SIGABRT, SIG_DFL);
+        ::kill(::getpid(), SIGABRT);
+    };
+
     int run(int argc, char const ** argv)
     {
-        int pid (::fork());
-        if (pid < 0) senf::throwErrno("::fork()");
+        pid  = ::fork();
+        if (pid < 0) throw senf::SystemException("::fork()");
         if (pid == 0) {
-            ::_exit(myMain(argc, argv));
+            signal(SIGABRT, &backtrace);
+            signal(SIGCHLD, SIG_IGN);
+            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(125);
         }
+        signal(SIGCHLD, SIG_DFL);
         int status;
-        if (::waitpid(pid, &status, 0) < 0) senf::throwErrno("::waitpid()");
-        return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+        if (::waitpid(pid, &status, 0) < 0) 
+            throw senf::SystemException("::waitpid()");
+        if (WIFSIGNALED(status))
+            std::cerr << "Terminated with signal " 
+                      << senf::signalName(WTERMSIG(status)) << "(" << WTERMSIG(status) << ")\n";
+        else if (WIFEXITED(status))
+            std::cerr << "Exited normally with exit status " << WEXITSTATUS(status) << "\n";
+        return status;
     }
 
 }
@@ -94,22 +122,37 @@ namespace {
 BOOST_AUTO_UNIT_TEST(testDaemon)
 {
     char const * args[] = { "run", 
-                            "--console-log=testDaemon.log,none", 
+                            "--console-log=testDaemon.log", 
                             "--pid-file=testDaemon.pid" };
-    BOOST_CHECK_EQUAL( run(sizeof(args)/sizeof(*args),args), 0 );
+
+    SENF_CHECK_NO_THROW( 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") );
-    delay(1000);
-    BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
     BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
     
-    std::ifstream log ("testDaemon.log");
+    boost::filesystem::rename("testDaemon.log", "testDaemon.log.1");
+    {
+        std::ifstream pidFile ("testDaemon.pid");
+        int pid (0);
+        BOOST_CHECK( pidFile >> pid );
+        BOOST_CHECK( pid != 0 );
+        if (pid != 0)
+            ::kill(pid, SIGHUP);
+    }
+
+    delay(2000);
+    BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
+    BOOST_CHECK( boost::filesystem::exists("testDaemon.log") );
+    BOOST_CHECK( boost::filesystem::exists("testDaemon.log.1") );
+    
+    std::ifstream log ("testDaemon.log.1");
     std::stringstream data;
     data << log.rdbuf();
-    BOOST_CHECK_EQUAL( data.str(), "Running init()\nRunning run()\n" );
+    BOOST_CHECK_EQUAL( data.str(), "Running init()\n(stderr)\nRunning run()\n" );
     BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log") );
+    BOOST_CHECK_NO_THROW( boost::filesystem::remove("testDaemon.log.1") );
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////