Skip to content

Commit

Permalink
Merge pull request #2481 from barton2526/sync_tests
Browse files Browse the repository at this point in the history
test: Add upstream sync_tests.cpp
  • Loading branch information
jamescowens authored Apr 10, 2022
2 parents 59cae6d + 690db2b commit 3de763d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ GRIDCOIN_TESTS =\
test/script_tests.cpp \
test/serialize_tests.cpp \
test/sigopcount_tests.cpp \
test/sync_tests.cpp \
test/test_gridcoin.cpp \
test/test_gridcoin.h \
test/transaction_tests.cpp \
Expand Down
45 changes: 45 additions & 0 deletions src/test/sync_tests.cpp
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()

0 comments on commit 3de763d

Please sign in to comment.