-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2481 from barton2526/sync_tests
test: Add upstream sync_tests.cpp
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) 2012-2020 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 <mutex> | ||
#include <stdexcept> | ||
|
||
#include <boost/test/unit_test.hpp> | ||
|
||
BOOST_AUTO_TEST_SUITE(sync_tests) | ||
|
||
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); | ||
} | ||
BOOST_CHECK(LockStackEmpty()); | ||
bool error_thrown = false; | ||
try { | ||
LOCK2(mutex2, mutex1); | ||
} catch (const std::logic_error& e) { | ||
BOOST_CHECK_EQUAL(e.what(), "potential deadlock detected: mutex1 -> mutex2 -> mutex1"); | ||
error_thrown = true; | ||
} | ||
BOOST_CHECK(LockStackEmpty()); | ||
#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() |