mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-11-04 14:09:33 +01:00 
			
		
		
		
	Make assertions thread-safe
This commit is contained in:
		@@ -479,6 +479,25 @@ set_tests_properties(
 | 
			
		||||
    LABELS "uses-signals"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
add_executable(Multithreading ${TESTS_DIR}/X37-Multithreading.cpp)
 | 
			
		||||
target_link_libraries(Multithreading PRIVATE Catch2::Catch2WithMain)
 | 
			
		||||
add_test(
 | 
			
		||||
  NAME Reporters::Multithreading
 | 
			
		||||
  COMMAND ${CMAKE_COMMAND} -E env $<TARGET_FILE:Multithreading>
 | 
			
		||||
)
 | 
			
		||||
set_tests_properties(
 | 
			
		||||
    Reporters::Multithreading
 | 
			
		||||
  PROPERTIES
 | 
			
		||||
    PASS_REGULAR_EXPRESSION "passed"
 | 
			
		||||
    FAIL_REGULAR_EXPRESSION "ThreadSanitizer"
 | 
			
		||||
)
 | 
			
		||||
if (NOT WIN32)
 | 
			
		||||
  target_compile_options( Reporters::Multithreading
 | 
			
		||||
    PUBLIC
 | 
			
		||||
      $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:GNU>,$<CXX_COMPILER_ID:AppleClang>>:-fsanitize=thread>
 | 
			
		||||
  )
 | 
			
		||||
endif()
 | 
			
		||||
 | 
			
		||||
add_executable(AssertionStartingEventGoesBeforeAssertionIsEvaluated
 | 
			
		||||
  X20-AssertionStartingEventGoesBeforeAssertionIsEvaluated.cpp
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										34
									
								
								tests/ExtraTests/X37-Multithreading.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								tests/ExtraTests/X37-Multithreading.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
//              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
 | 
			
		||||
 | 
			
		||||
#include <catch2/catch_test_macros.hpp>
 | 
			
		||||
#include <catch2/internal/catch_textflow.hpp>
 | 
			
		||||
#include <catch2/benchmark/catch_benchmark.hpp>
 | 
			
		||||
 | 
			
		||||
#include <thread>
 | 
			
		||||
#include <stop_token>
 | 
			
		||||
 | 
			
		||||
TEST_CASE( "ThreadAssertionTest",
 | 
			
		||||
           "[Multithreading]" ) {
 | 
			
		||||
    SECTION( "Basic" ) {
 | 
			
		||||
        std::jthread a([] (const std::stop_token& token) {
 | 
			
		||||
            while (!token.stop_requested()) {
 | 
			
		||||
                FAIL_CHECK(false);
 | 
			
		||||
                CHECK(true);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        std::jthread b([] (const std::stop_token& token) {
 | 
			
		||||
            while (!token.stop_requested()) {
 | 
			
		||||
                FAIL_CHECK(false);
 | 
			
		||||
                CHECK(true);
 | 
			
		||||
            }
 | 
			
		||||
        });
 | 
			
		||||
        std::this_thread::sleep_for( std::chrono::milliseconds( 1'000 ) );
 | 
			
		||||
        a.get_stop_source().request_stop();
 | 
			
		||||
        b.get_stop_source().request_stop();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user