Ignore:
Timestamp:
06/09/2008 11:52:56 (4 years ago)
Author:
chris
Message:

Move inline test functions out of line to make debugging easier and
reduce size of include file.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • box/chris/general/lib/common/Test.cpp

    r1969 r2273  
    22// 
    33// File 
    4 //              Name:    Test.h 
     4//              Name:    Test.cpp 
    55//              Purpose: Useful stuff for tests 
    6 //              Created: 2003/07/11 
     6//              Created: 2007/12/09 
    77// 
    88// -------------------------------------------------------------------------- 
    99 
    10 #ifndef TEST__H 
    11 #define TEST__H 
     10#include "Box.h" 
    1211 
    1312#include <errno.h> 
     
    2524#include <string> 
    2625 
    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 
     28bool TestFileExists(const char *Filename) 
    9229{ 
    9330        struct stat st; 
     
    9532} 
    9633 
    97 inline bool TestDirExists(const char *Filename) 
     34bool TestDirExists(const char *Filename) 
    9835{ 
    9936        struct stat st; 
     
    10239 
    10340// -1 if doesn't exist 
    104 inline int TestGetFileSize(const char *Filename) 
     41int TestGetFileSize(const char *Filename) 
    10542{ 
    10643        struct stat st; 
     
    11249} 
    11350 
    114 inline std::string ConvertPaths(const std::string& rOriginal) 
     51std::string ConvertPaths(const std::string& rOriginal) 
    11552{ 
    11653#ifdef WIN32 
     
    13673} 
    13774 
    138 inline int RunCommand(const std::string& rCommandLine) 
     75int RunCommand(const std::string& rCommandLine) 
    13976{ 
    14077        return ::system(ConvertPaths(rCommandLine).c_str()); 
     
    14582#endif 
    14683 
    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                 } 
     84bool 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                 
    158118                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 // WIN32 
    166 } 
    167  
    168 inline 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 
     128int ReadPidFile(const char *pidFile) 
    169129{ 
    170130        if(!TestFileExists(pidFile)) 
     
    188148} 
    189149 
    190 inline int LaunchServer(const std::string& rCommandLine, const char *pidFile) 
     150int LaunchServer(const std::string& rCommandLine, const char *pidFile) 
    191151{ 
    192152#ifdef WIN32 
     
    321281} 
    322282 
    323 #define TestRemoteProcessMemLeaks(filename) \ 
    324         TestRemoteProcessMemLeaksFunc(filename, __FILE__, __LINE__) 
    325  
    326 inline void TestRemoteProcessMemLeaksFunc(const char *filename, 
     283void TestRemoteProcessMemLeaksFunc(const char *filename, 
    327284        const char* file, int line) 
    328285{ 
     
    370327} 
    371328 
    372 inline void force_sync() 
     329void force_sync() 
    373330{ 
    374331        TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " 
     
    377334} 
    378335 
    379 inline void wait_for_sync_start() 
     336void wait_for_sync_start() 
    380337{ 
    381338        TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " 
     
    384341} 
    385342 
    386 inline void wait_for_sync_end() 
     343void wait_for_sync_end() 
    387344{ 
    388345        TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " 
     
    391348} 
    392349 
    393 inline void sync_and_wait() 
     350void sync_and_wait() 
    394351{ 
    395352        TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " 
     
    398355} 
    399356 
    400 inline void terminate_bbackupd(int pid) 
     357void terminate_bbackupd(int pid) 
    401358{ 
    402359        TEST_THAT(::system(BBACKUPCTL " -q -c testfiles/bbackupd.conf " 
     
    418375 
    419376// Wait a given number of seconds for something to complete 
    420 inline void wait_for_operation(int seconds) 
     377void wait_for_operation(int seconds) 
    421378{ 
    422379        printf("Waiting: "); 
     
    432389} 
    433390 
    434 inline void safe_sleep(int seconds) 
    435 { 
     391void safe_sleep(int seconds) 
     392{ 
     393        BOX_TRACE("sleeping for " << seconds << " seconds"); 
     394 
    436395#ifdef WIN32 
    437396        Sleep(seconds * 1000); 
    438397#else 
    439398        struct timespec ts; 
     399        memset(&ts, 0, sizeof(ts)); 
    440400        ts.tv_sec  = seconds; 
    441401        ts.tv_nsec = 0; 
    442402        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        } 
    444409#endif 
    445 } 
    446  
    447 #endif // TEST__H 
     410 
     411        BOX_TRACE("sleep finished"); 
     412} 
Note: See TracChangeset for help on using the changeset viewer.