mirror of
https://github.com/dogecoin/dogecoin.git
synced 2026-01-31 10:30:52 +00:00
Add unit test for DEBUG_LOCKORDER code
Cherry-picked from: 41b88e933
This commit is contained in:
parent
a9129315f5
commit
11f5dd6ea9
@ -127,6 +127,7 @@ BITCOIN_TESTS =\
|
||||
test/sigopcount_tests.cpp \
|
||||
test/skiplist_tests.cpp \
|
||||
test/streams_tests.cpp \
|
||||
test/sync_tests.cpp \
|
||||
test/test_bitcoin.cpp \
|
||||
test/test_bitcoin.h \
|
||||
test/test_random.h \
|
||||
|
||||
@ -103,7 +103,11 @@ static void potential_deadlock_detected(const LockPair& mismatch, const LockStac
|
||||
}
|
||||
LogPrintf(" %s\n", i.second.ToString());
|
||||
}
|
||||
assert(false);
|
||||
if (g_debug_lockorder_abort) {
|
||||
fprintf(stderr, "Assertion failed: detected inconsistent lock order at %s:%i, details in debug log.\n", __FILE__, __LINE__);
|
||||
abort();
|
||||
}
|
||||
throw std::logic_error("potential deadlock detected");
|
||||
}
|
||||
|
||||
static void push_lock(void* c, const CLockLocation& locklocation, bool fTry)
|
||||
@ -206,4 +210,6 @@ void DeleteLock(void* cs)
|
||||
}
|
||||
}
|
||||
|
||||
bool g_debug_lockorder_abort = true;
|
||||
|
||||
#endif /* DEBUG_LOCKORDER */
|
||||
|
||||
@ -77,6 +77,13 @@ void LeaveCritical();
|
||||
std::string LocksHeld();
|
||||
void AssertLockHeldInternal(const char* pszName, const char* pszFile, int nLine, void* cs);
|
||||
void DeleteLock(void* cs);
|
||||
|
||||
/**
|
||||
* Call abort() if a potential lock order deadlock bug is detected, instead of
|
||||
* just logging information and throwing a logic_error. Defaults to true, and
|
||||
* set to false in DEBUG_LOCKORDER unit tests.
|
||||
*/
|
||||
extern bool g_debug_lockorder_abort;
|
||||
#else
|
||||
void static inline EnterCritical(const char* pszName, const char* pszFile, int nLine, void* cs, bool fTry = false) {}
|
||||
void static inline LeaveCritical() {}
|
||||
|
||||
41
src/test/sync_tests.cpp
Normal file
41
src/test/sync_tests.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2012-2017 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <sync.h>
|
||||
#include <test/test_bitcoin.h>
|
||||
|
||||
#include <boost/test/unit_test.hpp>
|
||||
|
||||
BOOST_FIXTURE_TEST_SUITE(sync_tests, BasicTestingSetup)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(potential_deadlock_detected)
|
||||
{
|
||||
#ifdef DEBUG_LOCKORDER
|
||||
bool prev = g_debug_lockorder_abort;
|
||||
g_debug_lockorder_abort = false;
|
||||
#endif
|
||||
|
||||
CCriticalSection mutex1, mutex2;
|
||||
{
|
||||
LOCK2(mutex1, mutex2);
|
||||
}
|
||||
bool error_thrown = false;
|
||||
try {
|
||||
LOCK2(mutex2, mutex1);
|
||||
} catch (const std::logic_error& e) {
|
||||
BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected");
|
||||
error_thrown = true;
|
||||
}
|
||||
#ifdef DEBUG_LOCKORDER
|
||||
BOOST_CHECK(error_thrown);
|
||||
#else
|
||||
BOOST_CHECK(!error_thrown);
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG_LOCKORDER
|
||||
g_debug_lockorder_abort = prev;
|
||||
#endif
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
Loading…
x
Reference in New Issue
Block a user