Line data Source code
1 : // Copyright (c) 2012-2020 The Bitcoin Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include "sync.h" 6 : #include "test/test_pivx.h" 7 : 8 : #include <boost/test/unit_test.hpp> 9 : 10 : namespace { 11 : template <typename MutexType> 12 2 : void TestPotentialDeadLockDetected(MutexType& mutex1, MutexType& mutex2) 13 : { 14 : { 15 4 : LOCK2(mutex1, mutex2); 16 : } 17 2 : bool error_thrown = false; 18 : try { 19 6 : LOCK2(mutex2, mutex1); 20 0 : } catch (const std::logic_error& e) { 21 0 : BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected"); 22 0 : error_thrown = true; 23 : } 24 : #ifdef DEBUG_LOCKORDER 25 : BOOST_CHECK(error_thrown); 26 : #else 27 4 : BOOST_CHECK(!error_thrown); 28 : #endif 29 2 : } 30 : } // namespace 31 : 32 : BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup) 33 : 34 2 : BOOST_AUTO_TEST_CASE(potential_deadlock_detected) 35 : { 36 : #ifdef DEBUG_LOCKORDER 37 : bool prev = g_debug_lockorder_abort; 38 : g_debug_lockorder_abort = false; 39 : #endif 40 : 41 2 : RecursiveMutex rmutex1, rmutex2; 42 1 : TestPotentialDeadLockDetected(rmutex1, rmutex2); 43 : 44 2 : Mutex mutex1, mutex2; 45 1 : TestPotentialDeadLockDetected(mutex1, mutex2); 46 : 47 : #ifdef DEBUG_LOCKORDER 48 : g_debug_lockorder_abort = prev; 49 : #endif 50 1 : } 51 : 52 : BOOST_AUTO_TEST_SUITE_END()