I’m working on unit tests for my little daemon but I’m having trouble getting the right exit code from the forked process. If I only run one of the testcases they work fine, but if I run two in a row the second one fail since it doesn’t get EXIT_SUCCESS as a return code. I’ve checked and the second test case does call exit(EXIT_SUCCESS) so it should return that, but somehow I get another child process.
What am I missing?
bool test_setup() {
pid_t pid = fork();
if(pid < 0) {
fail("Failed to fork", PLACE);
}
else if(pid > 0) { //main thread
//act as client to server, this code might call exit(EXIT_FAILURE)
exit(EXIT_SUCCESS);
}
//child thread
//run server
int retval; //return value from child process
wait(&retval);
return WEXITSTATUS(retval) == EXIT_SUCCESS;
}
bool test_send_one() {
pid_t pid = fork();
if(pid < 0) {
fail("Failed to fork", PLACE);
}
else if(pid > 0) { //main thread
//act as client to server, this code might call exit(EXIT_FAILURE)
cout <<"exit success" <<endl;
exit(EXIT_SUCCESS);
}
//child thread
//run server
int retval; //return value from child process
wait(&retval);
return WEXITSTATUS(retval) == EXIT_SUCCESS;
}
int main(int argc, char** argv) {
test_setup();
test_send_one();
}
I had swapped the if(pid > 0) and if(pid == 0).