[Box Backup-dev] COMMIT r578 - in box/chris/general: . lib/common lib/server test/basicserver

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sun, 21 May 2006 12:55:59 +0000 (GMT)


Author: chris
Date: 2006-05-21 12:55:17 +0000 (Sun, 21 May 2006)
New Revision: 578

Modified:
   box/chris/general/lib/common/Test.h
   box/chris/general/lib/server/ServerStream.h
   box/chris/general/modules.txt
   box/chris/general/test/basicserver/TestCommands.cpp
   box/chris/general/test/basicserver/testbasicserver.cpp
Log:
* modules.txt
- Enable basicserver tests on Win32

* lib/server/ServerStream.h
- Work around lack of fork() on Win32

* lib/common/Test.h
- Work around lack of getpid() and kill() on Win32

* test/basicserver/testbasicserver.cpp
- Work around lack of nanosleep() on Win32

* test/basicserver/TestCommands.cpp
- Don't include syslog.h unless configure detected that we have one


Modified: box/chris/general/lib/common/Test.h
===================================================================
--- box/chris/general/lib/common/Test.h	2006-05-21 03:45:48 UTC (rev 577)
+++ box/chris/general/lib/common/Test.h	2006-05-21 12:55:17 UTC (rev 578)
@@ -81,12 +81,45 @@
 
 inline int LaunchServer(const char *CommandLine, const char *pidFile)
 {
+#ifdef WIN32
+	PROCESS_INFORMATION procInfo;
+
+	CHAR* tempCmd = strdup(CommandLine);
+
+	DWORD result = CreateProcess
+	(
+		NULL,        // lpApplicationName, naughty!
+		tempCmd,     // lpCommandLine
+		NULL,        // lpProcessAttributes
+		NULL,        // lpThreadAttributes
+		false,       // bInheritHandles
+		0,           // dwCreationFlags
+		NULL,        // lpEnvironment
+		NULL,        // lpCurrentDirectory
+		NULL,        // lpStartupInfo
+		&procInfo    // lpProcessInformation
+	);
+
+	free(tempCmd);
+
+	if (result != 0)
+	{
+		DWORD err = GetLastError();
+		printf("Launch failed: %s: error %d\n", CommandLine, (int)err);
+		return -1;
+	}
+
+	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
+
 	// time for it to start up
 	::sleep(1);
 	
@@ -107,17 +140,50 @@
 		return -1;
 	}
 	fclose(f);
-	
+
+#ifdef WIN32
+	if (pid != (int)procInfo.dwProcessId)
+	{
+		printf("Server wrote wrong pid to file (%s): expected %d "
+			"but found %d\n", pidFile, 
+			(int)procInfo.dwProcessId, pid);
+		TEST_FAIL_WITH_MESSAGE("Server wrote wrong pid to file");	
+		return -1;
+	}
+#endif
+
 	return pid;
 }
 
 #ifdef WIN32
+#include <windows.h>
+#endif
 
+inline bool ServerIsAlive(int pid)
+{
+#ifdef WIN32
+	HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid);
+	if (hProcess == NULL)
+	{
+		printf("Failed to open process %d: error %d\n",
+			pid, (int)GetLastError());
+		return false;
+	}
+	CloseHandle(hProcess);
+	return true;
+#else // !WIN32
+	if(pid == 0) return false;
+	return ::kill(pid, 0) != -1;
+#endif // WIN32
+}
+
+#ifdef WIN32
+
 #include "WinNamedPipeStream.h"
 #include "IOStreamGetLine.h"
 #include "BoxPortsAndFiles.h"
 
-bool SendCommands(const std::string& rCmd)
+inline bool SendCommands(const std::string& rCmd)
 {
 	WinNamedPipeStream connection;
 
@@ -202,11 +268,6 @@
 	return statusOk;
 }
 
-inline bool ServerIsAlive()
-{
-	return SendCommands("");
-}
-
 inline bool HUPServer(int pid)
 {
 	return SendCommands("reload");
@@ -216,17 +277,11 @@
 {
 	TEST_THAT(SendCommands("terminate"));
 	::sleep(1);
-	return !ServerIsAlive();
+	return !ServerIsAlive(pid);
 }
 
 #else // !WIN32
 
-inline bool ServerIsAlive(int pid)
-{
-	if(pid == 0) return false;
-	return ::kill(pid, 0) != -1;
-}
-
 inline bool HUPServer(int pid)
 {
 	if(pid == 0) return false;

Modified: box/chris/general/lib/server/ServerStream.h
===================================================================
--- box/chris/general/lib/server/ServerStream.h	2006-05-21 03:45:48 UTC (rev 577)
+++ box/chris/general/lib/server/ServerStream.h	2006-05-21 12:55:17 UTC (rev 578)
@@ -215,6 +215,7 @@
 					if(connection.get())
 					{
 						// Since this is a template parameter, the if() will be optimised out by the compiler
+#ifndef WIN32 // no fork on Win32
 						if(ForkToHandleRequests)
 						{
 							pid_t pid = ::fork();
@@ -255,14 +256,18 @@
 						}
 						else
 						{
+#endif // !WIN32
 							// Just handle in this connection
 							SetProcessTitle("handling");
 							HandleConnection(*connection);
 							SetProcessTitle("idle");										
+#ifndef WIN32
 						}
+#endif // !WIN32
 					}
 				}
-				
+
+#ifndef WIN32				
 				// Clean up child processes (if forking daemon)
 				if(ForkToHandleRequests)
 				{
@@ -277,6 +282,7 @@
 						}
 					} while(p > 0);
 				}
+#endif // !WIN32
 			}
 		}
 		catch(...)
@@ -301,7 +307,11 @@
 	// depends on the forking model in case someone changes it later.
 	bool WillForkToHandleRequests()
 	{
+#ifdef WIN32
+		return false;
+#else
 		return ForkToHandleRequests;
+#endif // WIN32
 	}
 
 private:

Modified: box/chris/general/modules.txt
===================================================================
--- box/chris/general/modules.txt	2006-05-21 03:45:48 UTC (rev 577)
+++ box/chris/general/modules.txt	2006-05-21 12:55:17 UTC (rev 578)
@@ -18,11 +18,7 @@
 test/crypto		lib/crypto	lib/win32
 test/compress		lib/compress	lib/win32
 test/raidfile		lib/raidfile	lib/win32
-
-OMIT:mingw32
-OMIT:mingw32msvc
 test/basicserver	lib/server	lib/win32
-END-OMIT
 
 # IF_DISTRIBUTION(boxbackup)
 

Modified: box/chris/general/test/basicserver/TestCommands.cpp
===================================================================
--- box/chris/general/test/basicserver/TestCommands.cpp	2006-05-21 03:45:48 UTC (rev 577)
+++ box/chris/general/test/basicserver/TestCommands.cpp	2006-05-21 12:55:17 UTC (rev 578)
@@ -1,7 +1,9 @@
 
 #include "Box.h"
 
+#ifdef HAVE_SYSLOG_H
 #include <syslog.h>
+#endif
 
 #include "autogen_TestProtocolServer.h"
 #include "CollectInBufferStream.h"

Modified: box/chris/general/test/basicserver/testbasicserver.cpp
===================================================================
--- box/chris/general/test/basicserver/testbasicserver.cpp	2006-05-21 03:45:48 UTC (rev 577)
+++ box/chris/general/test/basicserver/testbasicserver.cpp	2006-05-21 12:55:17 UTC (rev 578)
@@ -62,10 +62,14 @@
 
 void testservers_pause_before_reply()
 {
-	 struct timespec t;
-	 t.tv_sec = 0;
-	 t.tv_nsec = COMMS_SERVER_WAIT_BEFORE_REPLYING * 1000 * 1000;	// convert to ns
-	 ::nanosleep(&t, NULL);
+#ifdef WIN32
+	Sleep(COMMS_SERVER_WAIT_BEFORE_REPLYING * 1000);
+#else
+	struct timespec t;
+	t.tv_sec = 0;
+	t.tv_nsec = COMMS_SERVER_WAIT_BEFORE_REPLYING * 1000 * 1000;	// convert to ns
+	::nanosleep(&t, NULL);
+#endif
 }
 
 #define LARGE_DATA_BLOCK_SIZE 19870