Skip to content

Commit

Permalink
isolate tests in child processes
Browse files Browse the repository at this point in the history
  • Loading branch information
Peyman-EsteghamatRad committed May 27, 2024
1 parent bc61349 commit 75df150
Showing 1 changed file with 39 additions and 13 deletions.
52 changes: 39 additions & 13 deletions utest.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
#ifndef SHEREDOM_UTEST_H_INCLUDED
#define SHEREDOM_UTEST_H_INCLUDED

#include <cstdlib>
#include <sys/wait.h>
#include <unistd.h>

#ifdef _MSC_VER
/*
Disable warning about not inlining 'inline' functions.
Expand Down Expand Up @@ -653,7 +657,7 @@ utest_type_printer(long long unsigned int i) {
: _Generic((val - val), ptrdiff_t \
: "%p", default \
: "undef")), \
(val))
(val))
#else
/*
we don't have the ability to print the values we got, so we create a macro
Expand Down Expand Up @@ -1554,21 +1558,43 @@ int utest_main(int argc, const char *const argv[]) {

ns = utest_ns();
errno = 0;

pid_t pid = fork();

if (pid == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
#if defined(UTEST_HAS_EXCEPTIONS)
UTEST_SURPRESS_WARNING_BEGIN
try {
utest_state.tests[index].func(&result, utest_state.tests[index].index);
} catch (const std::exception &err) {
printf(" Exception : %s\n", err.what());
result = UTEST_TEST_FAILURE;
} catch (...) {
printf(" Exception : Unknown\n");
result = UTEST_TEST_FAILURE;
}
UTEST_SURPRESS_WARNING_END
UTEST_SURPRESS_WARNING_BEGIN
try {
utest_state.tests[index].func(&result, utest_state.tests[index].index);
} catch (const std::exception &err) {
printf(" Exception : %s\n", err.what());
result = UTEST_TEST_FAILURE;
} catch (...) {
printf(" Exception : Unknown\n");
result = UTEST_TEST_FAILURE;
}
UTEST_SURPRESS_WARNING_END
#else
utest_state.tests[index].func(&result, utest_state.tests[index].index);
utest_state.tests[index].func(&result, utest_state.tests[index].index);
#endif
exit(result);
} else {
int status;
waitpid(pid, &status, 0);
if (WIFSIGNALED(status)) {
result = UTEST_TEST_FAILURE;
printf("Test terminated due to signal %i", WTERMSIG(status));
} else if (WIFEXITED(status)) {
result = WEXITSTATUS(status);
} else {
result = UTEST_TEST_FAILURE;
printf("Test terminated abnormally");
}
}

ns = utest_ns() - ns;

if (utest_state.output) {
Expand Down

0 comments on commit 75df150

Please sign in to comment.