예제 #1
0
파일: unit.c 프로젝트: jorgenpt/cgreen
static void run_test_in_its_own_process(TestSuite *suite, UnitTest *test, TestReporter *reporter) {
#ifdef WIN32
    HANDLE pHandle = NULL;
    CgTestParams* pThreadParams = malloc(sizeof(CgTestParams));
    if(!pThreadParams)
        return;
    pThreadParams->reporter = reporter;
    pThreadParams->suite = suite;
    pThreadParams->test = test;
#endif

    (*reporter->start)(reporter, test->name);

#ifdef WIN32
    pHandle = (VOID *)CreateThread(NULL, 
                                   0, 
                                   (LPTHREAD_START_ROUTINE) &run_test_thread, 
                                   (LPVOID) pThreadParams, 
                                   0, 
                                   NULL);
    WaitForSingleObject(pHandle, INFINITE);
    (*reporter->finish)(reporter, test->name);
#else
    if (in_child_process()) {
        run_the_test_code(suite, test, reporter);
        send_reporter_completion_notification(reporter);
        stop();
    } else {
        wait_for_child_process();
        (*reporter->finish)(reporter, test->name);
    }
#endif
}
void run_test_in_its_own_process(TestSuite *suite, CgreenTest *test, TestReporter *reporter) {
    uint32_t test_starting_milliseconds = cgreen_time_get_current_milliseconds();

    (*reporter->start_test)(reporter, test->name);
    if (test->skip) {
        send_reporter_skipped_notification(reporter);
        (*reporter->finish_test)(reporter, test->filename, test->line, NULL, 0);
    } else if (in_child_process()) {
        run_the_test_code(suite, test, reporter);
        send_reporter_completion_notification(reporter);
        stop();
    } else {
        uint32_t test_duration = cgreen_time_duration_in_milliseconds(test_starting_milliseconds,
                                                                      cgreen_time_get_current_milliseconds());
        const int status = wait_for_child_process();
        if (WIFSIGNALED(status)) {
            /* a C++ exception generates SIGABRT. Only print our special message for different signals. */
            const int sig = WTERMSIG(status);
            if (sig != SIGABRT) {
                char buf[128];
                snprintf(buf, sizeof(buf), "Test terminated with signal: %s", (const char *)strsignal(sig));
                (*reporter->finish_test)(reporter, test->filename, test->line, buf, test_duration);
                return;
            }
        }

        (*reporter->finish_test)(reporter, test->filename, test->line, NULL, test_duration);
    }
}