I’m using googletest to test multi-threading code with std::thread
. In broken code, it often happens that a std::thread
is destroyed before the thread has joined, causing a program termination. Unfortunately, the testing framework does not catch this case and therefore stops execution. The test case is not marked as failed.
For example:
<code>#include <thread>
using namespace std::literals::chrono_literals;
TEST( ThreadDeathTest, death )
{
std::thread t([] {
std::this_thread::sleep_for(2s);
});
// exiting scope destroys thread, causing terminate()
}
</code>
<code>#include <thread>
using namespace std::literals::chrono_literals;
TEST( ThreadDeathTest, death )
{
std::thread t([] {
std::this_thread::sleep_for(2s);
});
// exiting scope destroys thread, causing terminate()
}
</code>
#include <thread>
using namespace std::literals::chrono_literals;
TEST( ThreadDeathTest, death )
{
std::thread t([] {
std::this_thread::sleep_for(2s);
});
// exiting scope destroys thread, causing terminate()
}
Output:
<code>$ctmltest --gtest_filter=ThreadDeathTest.death
Note: Google Test filter = ThreadDeathTest.death
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from ThreadDeathTest
[ RUN ] ThreadDeathTest.death
$
</code>
<code>$ctmltest --gtest_filter=ThreadDeathTest.death
Note: Google Test filter = ThreadDeathTest.death
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from ThreadDeathTest
[ RUN ] ThreadDeathTest.death
$
</code>
$ctmltest --gtest_filter=ThreadDeathTest.death
Note: Google Test filter = ThreadDeathTest.death
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from ThreadDeathTest
[ RUN ] ThreadDeathTest.death
$
I’ve read about death tests, but I don’t think this applies to my case: I am expecting no death for all my test cases.
Is there a way to tell googletest to watch out for termination, like it does for death tests?