diff options
| author | Martin Hořeňovský | 2025-11-09 11:07:32 +0100 |
|---|---|---|
| committer | Martin Hořeňovský | 2025-12-01 10:45:07 +0100 |
| commit | e849735e11f23ce9ec819e053b107f2ec3c3c1c4 (patch) | |
| tree | 5b950b653a79e54a76527c7bf0c26efa1f52e06e /tests/ExtraTests/X94-ThreadSafetyTests.cpp | |
| parent | d26f763180c081c8c10d17dee59b2b1139268220 (diff) | |
| download | catch3-e849735e11f23ce9ec819e053b107f2ec3c3c1c4.tar.gz | |
Add tests for assertion thread safety
Diffstat (limited to 'tests/ExtraTests/X94-ThreadSafetyTests.cpp')
| -rw-r--r-- | tests/ExtraTests/X94-ThreadSafetyTests.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/ExtraTests/X94-ThreadSafetyTests.cpp b/tests/ExtraTests/X94-ThreadSafetyTests.cpp new file mode 100644 index 00000000..d391417b --- /dev/null +++ b/tests/ExtraTests/X94-ThreadSafetyTests.cpp @@ -0,0 +1,43 @@ + +// 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 <thread> +#include <vector> + +TEST_CASE( "Failed REQUIRE in the main thread is fine", "[!shouldfail]" ) { + 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 < 100; ++i) { + CAPTURE(i); + CHECK( false ); + CHECK( true ); + } + } ); + } + + for (auto& t : threads) { + t.join(); + } + + REQUIRE( false ); +} |