Scheduler: Daemon class: Better IPC in daemonize()
[senf.git] / Scheduler / Daemon.test.cc
index 418ea9f..aceed1c 100644 (file)
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <iostream>
+#include <fstream>
+#include <boost/filesystem/operations.hpp>
 #include "Daemon.hh"
 #include "../Utils/Exception.hh"
 
-#include <boost/test/auto_unit_test.hpp>
+#include "../Utils/auto_unit_test.hh"
 #include <boost/test/test_tools.hpp>
 
 #define prefix_
@@ -52,11 +54,20 @@ namespace {
 
     class MyDaemon : public senf::Daemon
     {
-        void configure() { std::cout << "Running configure()" << std::endl; }
-        void init() { std::cout << "Running init()" << std::endl; }
+        void configure() { 
+            std::cout << "Running configure()" << std::endl; 
+            pidFile("invalid.pid");
+            consoleLog("invalid.log");
+            senf::Daemon::configure();
+        }
+
+        void init() { 
+            std::cout << "Running init()" << std::endl; 
+        }
+
         void run() {
-            delay(2000);
             std::cout << "Running run()" << std::endl; 
+            delay(1500);
         }
     };
 
@@ -75,15 +86,30 @@ namespace {
         }
         int status;
         if (::waitpid(pid, &status, 0) < 0) senf::throwErrno("::waitpid()");
-         return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
+        return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
     }
 
 }
 
 BOOST_AUTO_UNIT_TEST(testDaemon)
 {
-    char const * args[] = { "run", 0 };
-    BOOST_CHECK_EQUAL( run(1,args), 0 );
+    char const * 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") );
+    delay(1000);
+    BOOST_CHECK( ! boost::filesystem::exists("testDaemon.pid") );
+    BOOST_REQUIRE( boost::filesystem::exists("testDaemon.log") );
+    
+    std::ifstream log ("testDaemon.log");
+    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") );
 }
 
 ///////////////////////////////cc.e////////////////////////////////////////