mirror of
https://github.com/catchorg/Catch2.git
synced 2025-11-13 09:59:32 +01:00
Add tests for assertion thread safety
This commit is contained in:
44
tests/ExtraTests/X94-ThreadSafetyTests.cpp
Normal file
44
tests/ExtraTests/X94-ThreadSafetyTests.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
|
||||
// Copyright Catch2 Authors
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (See accompanying file LICENSE.txt or copy at
|
||||
// https://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// SPDX-License-Identifier: BSL-1.0
|
||||
|
||||
/**\file
|
||||
* Test that assertions and messages are thread-safe.
|
||||
*
|
||||
* This is done by spamming assertions and messages on multiple subthreads.
|
||||
* In manual, this reliably causes segfaults if the test is linked against
|
||||
* a non-thread-safe version of Catch2.
|
||||
*
|
||||
* The CTest test definition should also verify that the final assertion
|
||||
* count is correct.
|
||||
*/
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
TEST_CASE( "Failed REQUIRE in the main thread is fine" ) {
|
||||
std::vector<std::thread> threads;
|
||||
for ( size_t t = 0; t < 4; ++t) {
|
||||
threads.emplace_back( [t]() {
|
||||
CAPTURE(t);
|
||||
for (size_t i = 0; i < 1'000; ++i) {
|
||||
CAPTURE(i);
|
||||
CHECK( false );
|
||||
CHECK( true );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
for (auto& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
REQUIRE( false );
|
||||
}
|
||||
Reference in New Issue
Block a user