When writing multi-threaded test cases the following pitfalls needs to be avoided:
-
Uncaught assertions (thrown by
cuteor from your code) in separate threads are leading to callstd::terminate(): it must be ensured that all exceptions are caught on the separate thread.cute::threadcan be used for this purpose. It is just a simple wrapper aroundstd::threadand wraps the execution with acatch-all-exceptionshandler signalling an unhandled exception as a test failure. -
std::threads need to be joined before their lifetime ends otherwise it leads to callingstd::terminate():cute::threadcan be used for this purpose as well, since it ensures proper joining of the wrappedstd::threadprior to it's destruction. -
Test validation calls are thread safe:
cutehas been designed right from it's beginning with multi-threading in mind and all test validation calls can be safely used in arbitrary threads.
CUTE_TEST("A passed check in a separate thread is detected") {
auto t = cute::thread([&]() { CUTE_ASSERT(true); });
}
// this test will fail (as expected)
CUTE_TEST("A failed check in a separate thread is detected") {
auto t = cute::thread([&]() { CUTE_ASSERT(false); });
}
// this test will fail (as expected)
CUTE_TEST("An unhandled exception in a cute::thread is detected") {
auto t = cute::thread([&]() { throw std::runtime_error("forced exception"); });
}- describe usage of
cute::tick