Commit a767588130da8b3af11a582f4b5b191d8651485f
1 parent
b8ea001d
Use fork instead of pthread for VO Hub unit testing.
We should avoid using threads as this has portability issues.
Showing
1 changed file
with
35 additions
and
46 deletions
test/test_GVO.cpp
| ... | ... | @@ -30,31 +30,11 @@ |
| 30 | 30 | #endif |
| 31 | 31 | #include <cstdlib> // for system() function |
| 32 | 32 | #include <unistd.h> // for sleep() function |
| 33 | -#include <pthread.h> | |
| 34 | -#include <signal.h> // for pthread_kill() function | |
| 35 | 33 | #include "test_GVO.hpp" |
| 36 | 34 | #include "GTools.hpp" |
| 37 | 35 | |
| 38 | 36 | |
| 39 | 37 | /***********************************************************************//** |
| 40 | - * @brief Thread start routine to test GVOHub class | |
| 41 | - * | |
| 42 | - * @param[in] ptr Start routine argument. | |
| 43 | - **************************************************************************/ | |
| 44 | -void* vo_thread(void* ptr) | |
| 45 | -{ | |
| 46 | - // Create VO Hub | |
| 47 | - GVOHub hub; | |
| 48 | - | |
| 49 | - // Start Hub | |
| 50 | - hub.start(); | |
| 51 | - | |
| 52 | - // Return | |
| 53 | - return NULL; | |
| 54 | -} | |
| 55 | - | |
| 56 | - | |
| 57 | -/***********************************************************************//** | |
| 58 | 38 | * @brief Set parameters and tests |
| 59 | 39 | **************************************************************************/ |
| 60 | 40 | void TestGVO::set(void) |
| ... | ... | @@ -88,40 +68,49 @@ TestGVO* TestGVO::clone(void) const |
| 88 | 68 | **************************************************************************/ |
| 89 | 69 | void TestGVO::test_GVOHub(void) |
| 90 | 70 | { |
| 91 | - // Declare thread | |
| 92 | - pthread_t thread; | |
| 71 | + // Create child process | |
| 72 | + int pid = fork(); | |
| 73 | + | |
| 74 | + // If we have a PID of 0 we are in the child process | |
| 75 | + if (pid == 0) { | |
| 76 | + | |
| 77 | + // Create VO Hub | |
| 78 | + GVOHub hub; | |
| 79 | + | |
| 80 | + // Start Hub | |
| 81 | + hub.start(); | |
| 82 | + | |
| 83 | + // Exit child process | |
| 84 | + exit(0); | |
| 93 | 85 | |
| 94 | - // Start Hub in a thread | |
| 95 | - test_try("Start hub"); | |
| 96 | - try { | |
| 97 | - int rc = pthread_create(&thread, NULL, vo_thread, NULL); | |
| 98 | - test_try_success(); | |
| 99 | - } | |
| 100 | - catch (std::exception &e) { | |
| 101 | - test_try_failure(e); | |
| 102 | 86 | } |
| 103 | 87 | |
| 104 | - // Test Hub connection | |
| 105 | - GVOClient client; | |
| 106 | - for (int i = 0; i < 3; ++i) { | |
| 107 | - sleep(1); | |
| 108 | - client.connect(); | |
| 109 | - if (client.is_connected()) { | |
| 110 | - break; | |
| 88 | + // ... otherwise if PID is >0 then we are in the parent process | |
| 89 | + else if (pid > 0) { | |
| 90 | + | |
| 91 | + // Test Hub connection | |
| 92 | + GVOClient client; | |
| 93 | + for (int i = 0; i < 3; ++i) { | |
| 94 | + sleep(1); | |
| 95 | + client.connect(); | |
| 96 | + if (client.is_connected()) { | |
| 97 | + break; | |
| 98 | + } | |
| 111 | 99 | } |
| 112 | - } | |
| 113 | 100 | |
| 114 | - // Test ping | |
| 115 | - test_assert(client.ping_hub(), "Ping Hub."); | |
| 101 | + // Test ping | |
| 102 | + test_assert(client.ping_hub(), "Ping Hub."); | |
| 116 | 103 | |
| 117 | - // Disconnect client | |
| 118 | - client.disconnect(); | |
| 104 | + // Disconnect client | |
| 105 | + client.disconnect(); | |
| 119 | 106 | |
| 120 | - // We now shutdown the Hub via the client | |
| 121 | - client.shutdown_hub(); | |
| 107 | + // We now shutdown the Hub via the client | |
| 108 | + client.shutdown_hub(); | |
| 122 | 109 | |
| 123 | - // Sleep a bit to make sure that we exit without a Hub | |
| 124 | - sleep(1); | |
| 110 | + // Sleep a bit to make sure that we exit without a Hub | |
| 111 | + sleep(1); | |
| 112 | + | |
| 113 | + } // endelse: parent process | |
| 125 | 114 | |
| 126 | 115 | // Return |
| 127 | 116 | return; | ... | ... |