[Box Backup-commit] COMMIT r3594 - in box/trunk/lib: backupstore server

subversion at boxbackup.org subversion at boxbackup.org
Fri Jul 31 07:39:54 BST 2015


Author: chris
Date: 2015-07-31 06:39:54 +0000 (Fri, 31 Jul 2015)
New Revision: 3594

Modified:
   box/trunk/lib/backupstore/StoreTestUtils.cpp
   box/trunk/lib/server/ServerControl.cpp
   box/trunk/lib/server/ServerControl.h
Log:
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.

Modified: box/trunk/lib/backupstore/StoreTestUtils.cpp
===================================================================
--- box/trunk/lib/backupstore/StoreTestUtils.cpp	2015-07-31 06:39:41 UTC (rev 3593)
+++ box/trunk/lib/backupstore/StoreTestUtils.cpp	2015-07-31 06:39:54 UTC (rev 3594)
@@ -268,80 +268,32 @@
 
 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;
 }

Modified: box/trunk/lib/server/ServerControl.cpp
===================================================================
--- box/trunk/lib/server/ServerControl.cpp	2015-07-31 06:39:41 UTC (rev 3593)
+++ box/trunk/lib/server/ServerControl.cpp	2015-07-31 06:39:54 UTC (rev 3594)
@@ -226,3 +226,41 @@
 	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;
+}
+
+

Modified: box/trunk/lib/server/ServerControl.h
===================================================================
--- box/trunk/lib/server/ServerControl.h	2015-07-31 06:39:41 UTC (rev 3593)
+++ box/trunk/lib/server/ServerControl.h	2015-07-31 06:39:54 UTC (rev 3594)
@@ -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"




More information about the Boxbackup-commit mailing list