From 762af2a462607258817fde3dc919ee7db5d1bcf0 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Fri, 31 Jul 2015 06:39:54 +0000 Subject: Refactor to allow sharing code for starting and stopping daemons in tests. The new StartDaemon() and StopDaemon() in ServerControl.cpp/h are generic, and used by StartClient() and StartServer() in StoreTestUtils.cpp/h. --- lib/backupstore/StoreTestUtils.cpp | 76 +++++++------------------------------- lib/server/ServerControl.cpp | 38 +++++++++++++++++++ lib/server/ServerControl.h | 3 ++ 3 files changed, 55 insertions(+), 62 deletions(-) (limited to 'lib') diff --git a/lib/backupstore/StoreTestUtils.cpp b/lib/backupstore/StoreTestUtils.cpp index 1c98ef98..fc219325 100644 --- a/lib/backupstore/StoreTestUtils.cpp +++ b/lib/backupstore/StoreTestUtils.cpp @@ -268,80 +268,32 @@ bool check_reference_counts() bool StartServer() { - TEST_THAT_OR(bbstored_pid == 0, return false); - - std::string cmd = BBSTORED " " + bbstored_args + - " testfiles/bbstored.conf"; - bbstored_pid = LaunchServer(cmd.c_str(), "testfiles/bbstored.pid"); - - TEST_THAT_OR(bbstored_pid != -1 && bbstored_pid != 0, return false); - - ::sleep(1); - TEST_THAT_OR(ServerIsAlive(bbstored_pid), bbstored_pid = 0; return false); - return true; + bbstored_pid = StartDaemon(bbstored_pid, + BBSTORED " " + bbstored_args + " testfiles/bbstored.conf", + "testfiles/bbstored.pid"); + return bbstored_pid != 0; } bool StopServer(bool wait_for_process) { - TEST_THAT_OR(bbstored_pid != 0, return false); - TEST_THAT_OR(ServerIsAlive(bbstored_pid), - bbstored_pid = 0; return false); - TEST_THAT_OR(KillServer(bbstored_pid, wait_for_process), - bbstored_pid = 0; return false); - ::sleep(1); - - TEST_THAT_OR(!ServerIsAlive(bbstored_pid), - bbstored_pid = 0; return false); + bool result = StopDaemon(bbstored_pid, "testfiles/bbstored.pid", + "bbstored.memleaks", wait_for_process); bbstored_pid = 0; - - #ifdef WIN32 - int unlink_result = unlink("testfiles/bbstored.pid"); - TEST_EQUAL_LINE(0, unlink_result, "unlink testfiles/bbstored.pid"); - if(unlink_result != 0) - { - return false; - } - #else - TestRemoteProcessMemLeaks("bbstored.memleaks"); - #endif - - return true; + return result; } bool StartClient(const std::string& bbackupd_conf_file) { - TEST_THAT_OR(bbackupd_pid == 0, FAIL); - - std::string cmd = BBACKUPD " " + bbackupd_args + " " + bbackupd_conf_file; - bbackupd_pid = LaunchServer(cmd.c_str(), "testfiles/bbackupd.pid"); - - TEST_THAT_OR(bbackupd_pid != -1 && bbackupd_pid != 0, FAIL); - ::sleep(1); - TEST_THAT_OR(ServerIsAlive(bbackupd_pid), FAIL); - - return true; + bbstored_pid = StartDaemon(bbackupd_pid, + BBACKUPD " " + bbackupd_args + " " + bbackupd_conf_file, + "testfiles/bbackupd.pid"); + return bbackupd_pid != 0; } bool StopClient(bool wait_for_process) { - TEST_THAT_OR(bbackupd_pid != 0, FAIL); - TEST_THAT_OR(ServerIsAlive(bbackupd_pid), FAIL); - TEST_THAT_OR(KillServer(bbackupd_pid, wait_for_process), FAIL); - ::sleep(1); - - TEST_THAT_OR(!ServerIsAlive(bbackupd_pid), FAIL); + bool result = StopDaemon(bbackupd_pid, "testfiles/bbackupd.pid", + "bbackupd.memleaks", wait_for_process); bbackupd_pid = 0; - - #ifdef WIN32 - int unlink_result = unlink("testfiles/bbackupd.pid"); - TEST_EQUAL_LINE(0, unlink_result, "unlink testfiles/bbackupd.pid"); - if(unlink_result != 0) - { - FAIL; - } - #else - TestRemoteProcessMemLeaks("bbackupd.memleaks"); - #endif - - return true; + return result; } diff --git a/lib/server/ServerControl.cpp b/lib/server/ServerControl.cpp index b9650cee..c698319f 100644 --- a/lib/server/ServerControl.cpp +++ b/lib/server/ServerControl.cpp @@ -226,3 +226,41 @@ bool KillServer(int pid, bool WaitForProcess) return !ServerIsAlive(pid); } +int StartDaemon(int current_pid, const std::string& cmd_line, const char* pid_file) +{ + TEST_THAT_OR(current_pid == 0, return false); + + int new_pid = LaunchServer(cmd_line, pid_file); + + TEST_THAT_OR(new_pid != -1 && new_pid != 0, return false); + + ::sleep(1); + TEST_THAT_OR(ServerIsAlive(new_pid), return 0); + return new_pid; +} + +bool StopDaemon(int current_pid, const std::string& pid_file, + const std::string& memleaks_file, bool wait_for_process) +{ + TEST_THAT_OR(current_pid != 0, return false); + TEST_THAT_OR(ServerIsAlive(current_pid), return false); + TEST_THAT_OR(KillServer(current_pid, wait_for_process), return false); + ::sleep(1); + + TEST_THAT_OR(!ServerIsAlive(current_pid), return false); + + #ifdef WIN32 + int unlink_result = unlink(pid_file.c_str()); + TEST_EQUAL_LINE(0, unlink_result, std::string("unlink ") + pid_file); + if(unlink_result != 0) + { + return false; + } + #else + TestRemoteProcessMemLeaks(memleaks_file.c_str()); + #endif + + return true; +} + + diff --git a/lib/server/ServerControl.h b/lib/server/ServerControl.h index b2e51864..430f03a3 100644 --- a/lib/server/ServerControl.h +++ b/lib/server/ServerControl.h @@ -5,6 +5,9 @@ bool HUPServer(int pid); bool KillServer(int pid, bool WaitForProcess = false); +int StartDaemon(int current_pid, const std::string& cmd_line, const char* pid_file); +bool StopDaemon(int current_pid, const std::string& pid_file, + const std::string& memleaks_file, bool wait_for_process); #ifdef WIN32 #include "WinNamedPipeStream.h" -- cgit v1.2.3