Changeset View
Changeset View
Standalone View
Standalone View
src/test/sync_tests.cpp
// Copyright (c) 2012-2019 The Bitcoin Core developers | // Copyright (c) 2012-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <sync.h> | #include <sync.h> | ||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
namespace { | namespace { | ||||
template <typename MutexType> | template <typename MutexType> | ||||
void TestPotentialDeadLockDetected(MutexType &mutex1, MutexType &mutex2) { | void TestPotentialDeadLockDetected(MutexType &mutex1, MutexType &mutex2) { | ||||
{ LOCK2(mutex1, mutex2); } | { LOCK2(mutex1, mutex2); } | ||||
BOOST_CHECK(LockStackEmpty()); | |||||
bool error_thrown = false; | bool error_thrown = false; | ||||
try { | try { | ||||
LOCK2(mutex2, mutex1); | LOCK2(mutex2, mutex1); | ||||
} catch (const std::logic_error &e) { | } catch (const std::logic_error &e) { | ||||
BOOST_CHECK_EQUAL( | BOOST_CHECK_EQUAL( | ||||
e.what(), | e.what(), | ||||
"potential deadlock detected: mutex1 -> mutex2 -> mutex1"); | "potential deadlock detected: mutex1 -> mutex2 -> mutex1"); | ||||
error_thrown = true; | error_thrown = true; | ||||
} | } | ||||
BOOST_CHECK(LockStackEmpty()); | |||||
#ifdef DEBUG_LOCKORDER | #ifdef DEBUG_LOCKORDER | ||||
BOOST_CHECK(error_thrown); | BOOST_CHECK(error_thrown); | ||||
#else | #else | ||||
BOOST_CHECK(!error_thrown); | BOOST_CHECK(!error_thrown); | ||||
#endif | #endif | ||||
} | } | ||||
} // namespace | } // namespace | ||||
BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup) | ||||
BOOST_AUTO_TEST_CASE(potential_deadlock_detected) { | BOOST_AUTO_TEST_CASE(potential_deadlock_detected) { | ||||
#ifdef DEBUG_LOCKORDER | #ifdef DEBUG_LOCKORDER | ||||
bool prev = g_debug_lockorder_abort; | bool prev = g_debug_lockorder_abort; | ||||
g_debug_lockorder_abort = false; | g_debug_lockorder_abort = false; | ||||
#endif | #endif | ||||
RecursiveMutex rmutex1, rmutex2; | RecursiveMutex rmutex1, rmutex2; | ||||
TestPotentialDeadLockDetected(rmutex1, rmutex2); | TestPotentialDeadLockDetected(rmutex1, rmutex2); | ||||
// The second test ensures that lock tracking data have not been broken by | |||||
// exception. | |||||
TestPotentialDeadLockDetected(rmutex1, rmutex2); | |||||
Mutex mutex1, mutex2; | Mutex mutex1, mutex2; | ||||
TestPotentialDeadLockDetected(mutex1, mutex2); | TestPotentialDeadLockDetected(mutex1, mutex2); | ||||
// The second test ensures that lock tracking data have not been broken by | |||||
// exception. | |||||
TestPotentialDeadLockDetected(mutex1, mutex2); | |||||
#ifdef DEBUG_LOCKORDER | #ifdef DEBUG_LOCKORDER | ||||
g_debug_lockorder_abort = prev; | g_debug_lockorder_abort = prev; | ||||
#endif | #endif | ||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |