Changeset 2273 for box/chris/general/lib/common/Test.cpp
- Timestamp:
- 06/09/2008 11:52:56 (4 years ago)
- File:
-
- 1 copied
-
box/chris/general/lib/common/Test.cpp (copied) (copied from box/chris/general/lib/common/Test.h) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
box/chris/general/lib/common/Test.cpp
r1969 r2273 2 2 // 3 3 // File 4 // Name: Test. h4 // Name: Test.cpp 5 5 // Purpose: Useful stuff for tests 6 // Created: 200 3/07/116 // Created: 2007/12/09 7 7 // 8 8 // -------------------------------------------------------------------------- 9 9 10 #ifndef TEST__H 11 #define TEST__H 10 #include "Box.h" 12 11 13 12 #include <errno.h> … … 25 24 #include <string> 26 25 27 #ifdef WIN32 28 #define BBACKUPCTL "..\\..\\bin\\bbackupctl\\bbackupctl.exe" 29 #define BBACKUPD "..\\..\\bin\\bbackupd\\bbackupd.exe" 30 #define BBSTORED "..\\..\\bin\\bbstored\\bbstored.exe" 31 #define BBACKUPQUERY "..\\..\\bin\\bbackupquery\\bbackupquery.exe" 32 #define BBSTOREACCOUNTS "..\\..\\bin\\bbstoreaccounts\\bbstoreaccounts.exe" 33 #define TEST_RETURN(actual, expected) TEST_THAT(actual == expected); 34 #else 35 #define BBACKUPCTL "../../bin/bbackupctl/bbackupctl" 36 #define BBACKUPD "../../bin/bbackupd/bbackupd" 37 #define BBSTORED "../../bin/bbstored/bbstored" 38 #define BBACKUPQUERY "../../bin/bbackupquery/bbackupquery" 39 #define BBSTOREACCOUNTS "../../bin/bbstoreaccounts/bbstoreaccounts" 40 #define TEST_RETURN(actual, expected) TEST_THAT(actual == expected*256); 41 #endif 42 43 extern int failures; 44 extern int first_fail_line; 45 extern std::string first_fail_file; 46 extern std::string bbackupd_args, bbstored_args, bbackupquery_args; 47 48 #define TEST_FAIL_WITH_MESSAGE(msg) \ 49 { \ 50 if (failures == 0) \ 51 { \ 52 first_fail_file = __FILE__; \ 53 first_fail_line = __LINE__; \ 54 } \ 55 failures++; \ 56 printf("FAILURE: " msg " at " __FILE__ "(%d)\n", __LINE__); \ 57 } 58 59 #define TEST_ABORT_WITH_MESSAGE(msg) {TEST_FAIL_WITH_MESSAGE(msg); return 1;} 60 61 #define TEST_THAT(condition) {if(!(condition)) TEST_FAIL_WITH_MESSAGE("Condition [" #condition "] failed")} 62 #define TEST_THAT_ABORTONFAIL(condition) {if(!(condition)) TEST_ABORT_WITH_MESSAGE("Condition [" #condition "] failed")} 63 64 // NOTE: The 0- bit is to allow this to work with stuff which has negative constants for flags (eg ConnectionException) 65 #define TEST_CHECK_THROWS(statement, excepttype, subtype) \ 66 { \ 67 bool didthrow = false; \ 68 try \ 69 { \ 70 statement; \ 71 } \ 72 catch(excepttype &e) \ 73 { \ 74 if(e.GetSubType() != ((unsigned int)excepttype::subtype) \ 75 && e.GetSubType() != (unsigned int)(0-excepttype::subtype)) \ 76 { \ 77 throw; \ 78 } \ 79 didthrow = true; \ 80 } \ 81 catch(...) \ 82 { \ 83 throw; \ 84 } \ 85 if(!didthrow) \ 86 { \ 87 TEST_FAIL_WITH_MESSAGE("Didn't throw exception " #excepttype "(" #subtype ")") \ 88 } \ 89 } 90 91 inline bool TestFileExists(const char *Filename) 26 #include "Test.h" 27 28 bool TestFileExists(const char *Filename) 92 29 { 93 30 struct stat st; … … 95 32 } 96 33 97 inlinebool TestDirExists(const char *Filename)34 bool TestDirExists(const char *Filename) 98 35 { 99 36 struct stat st; … … 102 39 103 40 // -1 if doesn't exist 104 in line int TestGetFileSize(const char *Filename)41 int TestGetFileSize(const char *Filename) 105 42 { 106 43 struct stat st; … … 112 49 } 113 50 114 inlinestd::string ConvertPaths(const std::string& rOriginal)51 std::string ConvertPaths(const std::string& rOriginal) 115 52 { 116 53 #ifdef WIN32 … … 136 73 } 137 74 138 in line int RunCommand(const std::string& rCommandLine)75 int RunCommand(const std::string& rCommandLine) 139 76 { 140 77 return ::system(ConvertPaths(rCommandLine).c_str()); … … 145 82 #endif 146 83 147 inline bool ServerIsAlive(int pid) 148 { 149 #ifdef WIN32 150 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, false, pid); 151 if (hProcess == NULL) 152 { 153 if (GetLastError() != ERROR_INVALID_PARAMETER) 154 { 155 printf("Failed to open process %d: error %d\n", 156 pid, (int)GetLastError()); 157 } 84 bool ServerIsAlive(int pid) 85 { 86 #ifdef WIN32 87 88 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 89 false, pid); 90 if (hProcess == NULL) 91 { 92 if (GetLastError() != ERROR_INVALID_PARAMETER) 93 { 94 BOX_ERROR("Failed to open process " << pid << 95 ": " << 96 GetErrorMessage(GetLastError())); 97 } 98 return false; 99 } 100 101 DWORD exitCode; 102 BOOL result = GetExitCodeProcess(hProcess, &exitCode); 103 CloseHandle(hProcess); 104 105 if (result == 0) 106 { 107 BOX_ERROR("Failed to get exit code for process " << 108 pid << ": " << 109 GetErrorMessage(GetLastError())) 110 return false; 111 } 112 113 if (exitCode == STILL_ACTIVE) 114 { 115 return true; 116 } 117 158 118 return false; 159 } 160 CloseHandle(hProcess);161 return true; 162 #else // !WIN32 163 if(pid == 0) return false;164 return ::kill(pid, 0) != -1; 165 #endif // WIN32166 } 167 168 in line int ReadPidFile(const char *pidFile)119 120 #else // !WIN32 121 122 if(pid == 0) return false; 123 return ::kill(pid, 0) != -1; 124 125 #endif // WIN32 126 } 127 128 int ReadPidFile(const char *pidFile) 169 129 { 170 130 if(!TestFileExists(pidFile)) … … 188 148 } 189 149 190 in line int LaunchServer(const std::string& rCommandLine, const char *pidFile)150 int LaunchServer(const std::string& rCommandLine, const char *pidFile) 191 151 { 192 152 #ifdef WIN32 … … 321 281 } 322 282 323 #define TestRemoteProcessMemLeaks(filename) \ 324 TestRemoteProcessMemLeaksFunc(filename, __FILE__, __LINE__) 325 326 inline void TestRemoteProcessMemLeaksFunc(const char *filename, 283 void TestRemoteProcessMemLeaksFunc(const char *filename, 327 284 const char* file, int line) 328 285 { … … 370 327 } 371 328 372 inlinevoid force_sync()329 void force_sync() 373 330 { 374 331 TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " … … 377 334 } 378 335 379 inlinevoid wait_for_sync_start()336 void wait_for_sync_start() 380 337 { 381 338 TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " … … 384 341 } 385 342 386 inlinevoid wait_for_sync_end()343 void wait_for_sync_end() 387 344 { 388 345 TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " … … 391 348 } 392 349 393 inlinevoid sync_and_wait()350 void sync_and_wait() 394 351 { 395 352 TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " … … 398 355 } 399 356 400 inlinevoid terminate_bbackupd(int pid)357 void terminate_bbackupd(int pid) 401 358 { 402 359 TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " … … 418 375 419 376 // Wait a given number of seconds for something to complete 420 inlinevoid wait_for_operation(int seconds)377 void wait_for_operation(int seconds) 421 378 { 422 379 printf("Waiting: "); … … 432 389 } 433 390 434 inline void safe_sleep(int seconds) 435 { 391 void safe_sleep(int seconds) 392 { 393 BOX_TRACE("sleeping for " << seconds << " seconds"); 394 436 395 #ifdef WIN32 437 396 Sleep(seconds * 1000); 438 397 #else 439 398 struct timespec ts; 399 memset(&ts, 0, sizeof(ts)); 440 400 ts.tv_sec = seconds; 441 401 ts.tv_nsec = 0; 442 402 while (nanosleep(&ts, &ts) == -1 && errno == EINTR) 443 { /* sleep again */ } 403 { 404 BOX_TRACE("safe_sleep interrupted with " << 405 ts.tv_sec << "." << ts.tv_nsec << 406 " secs remaining, sleeping again"); 407 /* sleep again */ 408 } 444 409 #endif 445 } 446 447 #endif // TEST__H 410 411 BOX_TRACE("sleep finished"); 412 }
Note: See TracChangeset
for help on using the changeset viewer.
