From 933522451a25054188f2c676b36d94ee0a641b1d Mon Sep 17 00:00:00 2001 From: Nick Bolton Date: Fri, 6 May 2011 21:13:07 +0000 Subject: [PATCH] win32: Task #2933 Prevent multiple integ tests from running in parallel (had to expose some arch misc windows functions) --- src/lib/arch/CArchMiscWindows.h | 9 ++--- src/test/integtests/Main.cpp | 59 ++++++++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/src/lib/arch/CArchMiscWindows.h b/src/lib/arch/CArchMiscWindows.h index 6eaab71f..b86e486a 100644 --- a/src/lib/arch/CArchMiscWindows.h +++ b/src/lib/arch/CArchMiscWindows.h @@ -178,6 +178,11 @@ public: static HINSTANCE instanceWin32(); static void setInstanceWin32(HINSTANCE instance); + + static BOOL WINAPI getProcessEntry(PROCESSENTRY32& entry, DWORD processID); + static BOOL WINAPI getProcessEntry(PROCESSENTRY32& entry, std::string processName); + static BOOL WINAPI getSelfProcessEntry(PROCESSENTRY32& entry); + static BOOL WINAPI getParentProcessEntry(PROCESSENTRY32& entry); private: //! Open and return a registry key, closing the parent key @@ -195,10 +200,6 @@ private: static DWORD WINAPI dummySetThreadExecutionState(DWORD); - static BOOL WINAPI getProcessEntry(PROCESSENTRY32& entry, DWORD processID); - static BOOL WINAPI getSelfProcessEntry(PROCESSENTRY32& entry); - static BOOL WINAPI getParentProcessEntry(PROCESSENTRY32& entry); - private: typedef std::set CDialogs; typedef DWORD (WINAPI *STES_t)(DWORD); diff --git a/src/test/integtests/Main.cpp b/src/test/integtests/Main.cpp index 7de40cce..97588ba3 100644 --- a/src/test/integtests/Main.cpp +++ b/src/test/integtests/Main.cpp @@ -23,10 +23,20 @@ #include "CArchMiscWindows.h" #endif +#define ERROR_ALREADY_RUNNING 1 + +using namespace std; + +void +ensureSingleInstance(); + int main(int argc, char **argv) { - std::cout << "Synergy integration tests\n"; + // make sure integ tests don't run in parallel. + int err = ensureSingleInstance(); + if (err != 0) + return err; #if SYSAPI_WIN32 // record window instance for tray icon, etc @@ -39,3 +49,50 @@ main(int argc, char **argv) return RUN_ALL_TESTS(); } + +int +ensureSingleInstance() +{ +#if SYSAPI_WIN32 + + // get info for current process (we'll use the name later). + PROCESSENTRY32 selfEntry; + if (!CArchMiscWindows::getSelfProcessEntry(selfEntry)) + cerr << "could not process info for self " + << "(error: " << GetLastError() << ")" << endl; + + // get current task list. + HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); + if (snapshot == INVALID_HANDLE_VALUE) + cerr << "could not get process snapshot " + << "(error: " << GetLastError() << ")" << endl; + + PROCESSENTRY32 entry; + BOOL gotEntry = Process32First(snapshot, &entry); + if (!gotEntry) + cerr << "could not get first process entry " + << "(error: " << GetLastError() << ")" << endl; + + while (gotEntry) + { + string checkName(entry.szExeFile); + + // if entry has the same name as this process, but is not + // the current process... + if ((checkName.find(selfEntry.szExeFile) != string::npos) && + (entry.th32ProcessID != selfEntry.th32ProcessID)) + { + cerr << "error: process already running: " + << entry.th32ProcessID << " -> " << entry.szExeFile << endl; + + return ERROR_ALREADY_RUNNING; + } + + gotEntry = Process32Next(snapshot, &entry); + } +#elif SYSAPI_UNIX + // TODO +#endif + + return 0; +}