[Box Backup-commit] COMMIT r1285 - box/chris/general/lib/common

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Tue, 13 Feb 2007 23:18:48 +0000


Author: chris
Date: 2007-02-13 23:18:48 +0000 (Tue, 13 Feb 2007)
New Revision: 1285

Modified:
   box/chris/general/lib/common/Test.h
Log:
Added additional comments on how LaunchServer works with Win32 
customisations.

Coding style cleanups.

Added HupServer and KillServer from merge branch.


Modified: box/chris/general/lib/common/Test.h
===================================================================
--- box/chris/general/lib/common/Test.h	2007-02-10 01:12:41 UTC (rev 1284)
+++ box/chris/general/lib/common/Test.h	2007-02-13 23:18:48 UTC (rev 1285)
@@ -119,6 +119,7 @@
 inline int LaunchServer(const char *CommandLine, const char *pidFile)
 {
 #ifdef WIN32
+
 	PROCESS_INFORMATION procInfo;
 
 	STARTUPINFO startInfo;
@@ -157,25 +158,30 @@
 
 	CloseHandle(procInfo.hProcess);
 	CloseHandle(procInfo.hThread);
+
 #else // !WIN32
+
 	if(::system(CommandLine) != 0)
 	{
 		printf("Server: %s\n", CommandLine);
 		TEST_FAIL_WITH_MESSAGE("Couldn't start server");
 		return -1;
 	}
+
 #endif // WIN32
 
 	int pid = -1;
 
-#ifdef WIN32
+	#ifdef WIN32
+	// on other platforms there is no other way to get 
+	// the PID, so a NULL pidFile doesn't make sense.
+
 	if (pidFile == NULL)
 	{
-		pid = (int)procInfo.dwProcessId;
+		return (int)procInfo.dwProcessId;
 	}
-	else
-	{
-#endif
+	#endif
+
 	// time for it to start up
 	::fprintf(stdout, "Starting server: %s\n", CommandLine);
 	::fprintf(stdout, "Waiting for server to start: ");
@@ -183,14 +189,18 @@
 	for (int i = 0; i < 15; i++)
 	{
 		if (TestFileExists(pidFile))	
+		{
 			break;
+		}
 
 		#ifdef WIN32
 		if (!ServerIsAlive((int)procInfo.dwProcessId))
 		#else
 		if (!ServerIsAlive(pid))
 		#endif
+		{
 			break;
+		}
 
 		::fprintf(stdout, ".");
 		::fflush(stdout);
@@ -198,14 +208,17 @@
 	}
 
 	#ifdef WIN32
+	// on Win32 we can check whether the process is alive
+	// without even checking the PID file
+
 	if (!ServerIsAlive((int)procInfo.dwProcessId))
 	{
 		::fprintf(stdout, "server died!\n");
 		TEST_FAIL_WITH_MESSAGE("Server died!");	
 		return -1;
 	}
-	else 
 	#endif
+
 	if (!TestFileExists(pidFile))
 	{
 		::fprintf(stdout, "timed out!\n");
@@ -226,7 +239,10 @@
 	}
 	fclose(f);
 
-#ifdef WIN32
+	#ifdef WIN32
+	// On Win32 we can check whether the PID in the pidFile matches
+	// the one returned by the system, which it always should.
+
 	if (pid != (int)procInfo.dwProcessId)
 	{
 		printf("Server wrote wrong pid to file (%s): expected %d "
@@ -235,8 +251,7 @@
 		TEST_FAIL_WITH_MESSAGE("Server wrote wrong pid to file");	
 		return -1;
 	}
-	} // if (pidFile != NULL)
-#endif
+	#endif
 
 	return pid;
 }
@@ -244,10 +259,54 @@
 #define TestRemoteProcessMemLeaks(filename) \
 	TestRemoteProcessMemLeaksFunc(filename, __FILE__, __LINE__)
 
+inline bool HUPServer(int pid)
+{
+	if(pid == 0) return false;
+	return ::kill(pid, SIGHUP) != -1;
+}
+
+inline bool KillServerInternal(int pid)
+{
+	if(pid == 0 || pid == -1) return false;
+	TEST_THAT(::kill(pid, SIGTERM) != -1);
+}
+
+inline bool KillServer(int pid)
+{
+	KillServerInternal(pid);
+
+	for (int i = 0; i < 30; i++)
+	{
+		if (!ServerIsAlive(pid)) break;
+		::sleep(1);
+		if (!ServerIsAlive(pid)) break;
+
+		if (i == 0) 
+		{
+			printf("waiting for server to die");
+		}
+		printf(".");
+		fflush(stdout);
+	}
+
+	if (!ServerIsAlive(pid))
+	{
+		printf("done.\n");
+	}
+	else
+	{
+		printf("failed!\n");
+	}
+	fflush(stdout);
+
+	return !ServerIsAlive(pid);
+}
+
 inline void TestRemoteProcessMemLeaksFunc(const char *filename,
 	const char* file, int line)
 {
-#ifdef BOX_MEMORY_LEAK_TESTING
+	#ifdef BOX_MEMORY_LEAK_TESTING
+
 	// Does the file exist?
 	if(!TestFileExists(filename))
 	{
@@ -285,7 +344,8 @@
 		// Delete it
 		::unlink(filename);
 	}
-#endif
+
+	#endif
 }
 
 #ifdef WIN32