runner.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <felspar/test/runner.hpp>

#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <thread>

#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#endif


#ifndef FELSPAR_TEST_RUNNER_TIMEOUT_SECONDS
#define FELSPAR_TEST_RUNNER_TIMEOUT_SECONDS 30
#endif


std::string felspar::test::format_failure_message(
        std::string_view filename,
        std::size_t line,
        std::size_t column,
        std::string_view op,
        std::string_view first,
        std::string_view second) {
    std::stringstream m;
    m << filename << ":" << line << ":" << column << ": Error";
    if (not first.empty()) {
        m << "\n    check(" << first << ") " << op;
        if (not second.empty()) { m << ' ' << second; }
    } else if (not op.empty()) {
        m << "\n    " << op;
    }
    return m.str();
}


namespace {
    std::chrono::seconds constexpr timeout{FELSPAR_TEST_RUNNER_TIMEOUT_SECONDS};

The point in time at which the watchdog will kill the process. Storing a new value extends (or shortens) the deadline — the watchdog polls it every second rather than sleeping for the whole timeout. It is always set to timeout past the moment it was last reset.

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    std::atomic<std::chrono::steady_clock::time_point> deadline;

    inline std::string timestr(std::chrono::nanoseconds ns) {
        using namespace std::literals;
        if (ns < 1us) {
            return std::to_string(ns.count()) + "ns";
        } else if (ns < 1ms) {
            return std::to_string(ns.count() / 1000) + "µs";
        } else if (ns < 1s) {
            return std::to_string(ns.count() / 1000'000) + "ms";
        } else {
            return std::to_string(ns.count() / 1000'000'000) + "s";
        }
    }
}


int main() {
    auto const started = std::chrono::steady_clock::now();
    deadline.store(started + timeout);
    std::thread{[started]() {
        while (std::chrono::steady_clock::now() < deadline.load()) {
            std::this_thread::sleep_for(std::chrono::seconds{1});
        }
        auto const now = std::chrono::steady_clock::now();
        std::cerr << "\n\nTiming out after "
                  << timestr(now - deadline.load() + timeout)
                  << " with total runtime " << timestr(now - started)
                  << std::endl;

A watchdog must terminate without running atexit handlers, static destructors, or stream flushes. That ordered shutdown is unsafe to run from this thread while the main thread is wedged in a blocking call, and on Windows it deadlocks during teardown so the process never exits. Kill the process outright instead. The message above is flushed with std::endl since nothing here will flush it for us.

 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifdef _WIN32
        ::TerminateProcess(::GetCurrentProcess(), 127);
#else
        std::_Exit(127);
#endif
    }}.detach();
    std::size_t number{}, pass{}, fail{};
    for (auto const &test : felspar::test::all_test_cases()) {
        if (test.name.empty()) {
            std::cout << test.suite << ':' << std::to_string(++number);
        } else {
            std::cout << test.suite << ':' << test.name;
        }
        std::cout << std::flush;
        std::stringstream ss;
        if (auto const [eptr, time] = test(ss); not eptr) {
            ++pass;
            std::cout << " ... OK -- " << timestr(time);
        } else {
            ++fail;
            std::cout << " ... FAIL :-( -- " << timestr(time);
            if (auto const str = ss.str(); not str.empty()) {
                std::cout << "\n---output---\n\n" << str << "\n^^^output^^^";
            }
            try {
                std::rethrow_exception(eptr);
            } catch (std::exception const &e) {
                std::cerr << '\n' << e.what();
            } catch (...) { std::cerr << "\nUnknown exception type"; }
        };
        std::cout << '\n';
        deadline.store(std::chrono::steady_clock::now() + timeout);
    }
    std::cout << (pass + fail) << " tests run, " << pass << " passed";
    if (fail) {
        std::cout << " and " << fail << " failed\n";
    } else {
        std::cout << '\n';
    }
    return std::clamp<std::size_t>(fail, 0, 126);
}