0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-11 15:56:58 -04:00
bitcoin-core/src/test
Wladimir J. van der Laan 9996b1806a
Merge #21064: refactor: use std::shared_mutex & remove Boost Thread
060a2a64d4 ci: remove boost thread installation (fanquake)
06e1d7d81d build: don't build or use Boost Thread (fanquake)
7097add83c refactor: replace Boost shared_mutex with std shared_mutex in sigcache (fanquake)
8e55981ef8 refactor: replace Boost shared_mutex with std shared_mutex in cuckoocache tests (fanquake)

Pull request description:

  This replaces `boost::shared_mutex` and `boost::unique_lock` with [`std::shared_mutex`](https://en.cppreference.com/w/cpp/thread/shared_mutex) & [`std::unique_lock`](https://en.cppreference.com/w/cpp/thread/unique_lock).

  Even though [some concerns were raised](https://github.com/bitcoin/bitcoin/issues/16684#issuecomment-726214696) in #16684 with regard to `std::shared_mutex` being unsafe to use across some glibc versions, I still think this change is an improvement. As I mentioned in #21022, I also think trying to restrict standard library feature usage based on bugs in glibc is not only hard to do, but it's not currently clear exactly how we do that in practice (does it also extend to patching out use in our dependencies, should we be implementing more runtime checks for features we are using, when do we consider an affected glibc "old enough" not to worry about? etc). If you take a look through the [glibc bug tracker](https://sourceware.org/bugzilla/describecomponents.cgi?product=glibc) you'll no doubt find plenty of (active) bug reports for standard library code we already using. Obviously not to say we shouldn't try and avoid buggy code where possible.

  Two other points:

  [Cory mentioned in #21022](https://github.com/bitcoin/bitcoin/pull/21022#issuecomment-769274179):
  > It also seems reasonable to me to worry that boost hits the same underlying glibc bug, and we've just not happened to trigger the right conditions yet.

  Moving away from Boost to the standard library also removes the potential for differences related to Boosts configuration. Boost has multiple versions of `shared_mutex`, and what you end up using, and what it's backed by depends on:
  * The version of Boost.
  * The platform you're building for.
  * Which version of `BOOST_THREAD_VERSION` is defined: (2,3,4 or 5) default=2. (see [here](https://www.boost.org/doc/libs/1_70_0/doc/html/thread/build.html#thread.build.configuration) for some of the differences).
  * Is `BOOST_THREAD_V2_SHARED_MUTEX` defined? (not by default). If so, you might get the ["less performant, but more robust"](https://github.com/boostorg/thread/issues/230#issuecomment-475937761) version of `shared_mutex`.

  A lot of these factors are eliminated by our use of depends, but users will have varying configurations. It's also not inconceivable to think that a distro, or some package manager might start defining something like `BOOST_THREAD_VERSION=3`. Boost tried to change the default from 2 to 3 at one point.

  With this change, we no longer use Boost Thread, so this PR also removes it from depends, the build system, CI etc.

  Previous similar PRs were #19183 & #20922. The authors are included in the commits here.
  Also related to #21022 - pthread sanity checking.

ACKs for top commit:
  laanwj:
    Code review ACK 060a2a64d4
  vasild:
    ACK 060a2a64d4

Tree-SHA512: 572d14d8c9de20bc434511f20d3f431836393ff915b2fe9de5a47a02dca76805ad5c3fc4cceecb4cd43f3ba939a0508178c4e60e62abdbaaa6b3e8db20b75b03
2021-02-12 11:39:36 +01:00
..
data
fuzz fuzz: version handshake 2021-02-11 12:45:57 +01:00
util Merge #21016: refactor: remove boost::thread_group usage 2021-02-01 13:27:28 +01:00
addrman_tests.cpp Don't make "in" parameters look like "out"/"in-out" parameters: pass by ref to const instead of ref to non-const 2020-12-06 00:22:40 +00:00
allocator_tests.cpp
amount_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
arith_uint256_tests.cpp
base32_tests.cpp Merge #20000: test: fix creation of "std::string"s with \0s 2020-11-20 04:39:37 +01:00
base58_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
base64_tests.cpp test: fix creation of std::string objects with \0s 2020-10-31 16:02:49 +01:00
bech32_tests.cpp
bip32_tests.cpp
blockchain_tests.cpp
blockencodings_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
blockfilter_index_tests.cpp scripted-diff: Use BlockManager::LookupBlockIndex 2021-01-28 14:15:26 -05:00
blockfilter_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
bloom_tests.cpp Treat CDataStream bytes as uint8_t 2020-11-23 21:19:50 +01:00
bswap_tests.cpp
checkqueue_tests.cpp refactor: remove boost::thread_group usage 2021-01-29 15:39:44 +08:00
coins_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
compilerbug_tests.cpp
compress_tests.cpp
crypto_tests.cpp test: Add MuHash3072 unit tests 2020-12-21 19:57:28 +01:00
cuckoocache_tests.cpp refactor: replace Boost shared_mutex with std shared_mutex in cuckoocache tests 2021-02-02 12:35:40 +08:00
dbwrapper_tests.cpp
denialofservice_tests.cpp net_processing: split PeerManager into interface and implementation classes 2021-01-09 23:27:45 +10:00
descriptor_tests.cpp
flatfile_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
fs_tests.cpp Introduce GetUniquePath(base) helper method to replace boost::filesystem::unique_path() which is not available in std::filesystem. 2021-02-04 11:38:09 +01:00
getarg_tests.cpp
hash_tests.cpp
interfaces_tests.cpp test: Remove no longer needed MakeChain calls 2020-12-07 20:46:03 -05:00
key_io_tests.cpp test: signet network selection tests 2020-09-18 10:19:42 +09:00
key_tests.cpp Fix length of R check in test/key_tests.cpp:key_signature_tests 2020-11-25 18:07:37 +05:00
logging_tests.cpp
main.cpp
Makefile
mempool_tests.cpp
merkle_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
merkleblock_tests.cpp
miner_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
multisig_tests.cpp
net_tests.cpp refactor: Remove nMyStartingHeight from CNode/Connman 2021-01-02 10:24:45 +01:00
netbase_tests.cpp net: allow CSubNet of non-IP networks 2021-01-10 15:51:15 +01:00
pmt_tests.cpp
policy_fee_tests.cpp
policyestimator_tests.cpp
pow_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
prevector_tests.cpp
raii_event_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
random_tests.cpp
README.md
ref_tests.cpp
reverselock_tests.cpp sync: Improve CheckLastCritical() 2020-12-10 20:46:29 +02:00
rpc_tests.cpp
sanity_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
scheduler_tests.cpp refactor: remove boost::thread_group usage 2021-01-29 15:39:44 +08:00
script_p2sh_tests.cpp Only relay Taproot spends if next block has it active 2020-10-30 15:52:19 -07:00
script_standard_tests.cpp Merge #20211: Use -Wswitch for TxoutType where possible 2021-02-11 11:48:12 +01:00
script_tests.cpp test: Replace ARRAYLEN with C++11 ranged for loop 2020-10-26 14:27:33 +01:00
scriptnum10.h
scriptnum_tests.cpp
serialize_tests.cpp Treat CDataStream bytes as uint8_t 2020-11-23 21:19:50 +01:00
settings_tests.cpp refactor: Make HexStr take a span 2020-08-06 19:41:43 +02:00
sighash_tests.cpp scripted-diff: Replace UINT256_ONE() with uint256::ONE 2020-09-28 12:14:19 +10:00
sigopcount_tests.cpp Replace usage of GetScriptForWitness with GetScriptForDestination 2020-08-14 08:44:42 +12:00
skiplist_tests.cpp
sock_tests.cpp test: add Sock unit tests 2021-02-11 10:44:53 +01:00
streams_tests.cpp Merge #20464: refactor: Treat CDataStream bytes as uint8_t 2021-02-01 15:17:28 +01:00
sync_tests.cpp refactor: remove straggling boost::mutex usage 2021-01-26 15:57:28 +08:00
system_tests.cpp scripted-diff: Bump copyright headers 2020-12-31 09:45:41 +01:00
timedata_tests.cpp
torcontrol_tests.cpp
transaction_tests.cpp Merge bitcoin-core/gui#183: Add include for std::bind. 2021-01-26 11:14:27 +01:00
txindex_tests.cpp
txrequest_tests.cpp Report and verify expirations 2020-10-12 12:14:53 -07:00
txvalidation_tests.cpp [refactor] return MempoolAcceptResult 2021-02-09 07:01:52 -08:00
txvalidationcache_tests.cpp [refactor] return MempoolAcceptResult 2021-02-09 07:01:52 -08:00
uint256_tests.cpp uint256: Update constructors to c++11, make ONE static 2020-09-28 12:14:19 +10:00
util_tests.cpp Introduce GetUniquePath(base) helper method to replace boost::filesystem::unique_path() which is not available in std::filesystem. 2021-02-04 11:38:09 +01:00
util_threadnames_tests.cpp
validation_block_tests.cpp [refactor] return MempoolAcceptResult 2021-02-09 07:01:52 -08:00
validation_chainstate_tests.cpp sync: Use decltype(auto) return type for WITH_LOCK 2020-12-04 12:23:05 -05:00
validation_chainstatemanager_tests.cpp Merge #20495: sync: Use decltype(auto) return type for WITH_LOCK 2021-01-12 15:56:19 +08:00
validation_flush_tests.cpp Pass mempool reference to chainstate constructor 2020-08-28 10:42:04 +02:00
validation_tests.cpp doc: fix various typos 2021-01-04 12:31:31 +08:00
validationinterface_tests.cpp
versionbits_tests.cpp Remove gArgs global from CreateChainParams to aid testing 2020-09-29 10:20:05 +02:00

Unit tests

The sources in this directory are unit test cases. Boost includes a unit testing framework, and since Bitcoin Core already uses Boost, it makes sense to simply use this framework rather than require developers to configure some other framework (we want as few impediments to creating unit tests as possible).

The build system is set up to compile an executable called test_bitcoin that runs all of the unit tests. The main source file for the test library is found in util/setup_common.cpp.

Compiling/running unit tests

Unit tests will be automatically compiled if dependencies were met in ./configure and tests weren't explicitly disabled.

After configuring, they can be run with make check.

To run the unit tests manually, launch src/test/test_bitcoin. To recompile after a test file was modified, run make and then run the test again. If you modify a non-test file, use make -C src/test to recompile only what's needed to run the unit tests.

To add more unit tests, add BOOST_AUTO_TEST_CASE functions to the existing .cpp files in the test/ directory or add new .cpp files that implement new BOOST_AUTO_TEST_SUITE sections.

To run the GUI unit tests manually, launch src/qt/test/test_bitcoin-qt

To add more GUI unit tests, add them to the src/qt/test/ directory and the src/qt/test/test_main.cpp file.

Running individual tests

test_bitcoin has some built-in command-line arguments; for example, to run just the getarg_tests verbosely:

test_bitcoin --log_level=all --run_test=getarg_tests -- DEBUG_LOG_OUT

log_level controls the verbosity of the test framework, which logs when a test case is entered, for example. The DEBUG_LOG_OUT after the two dashes redirects the debug log, which would normally go to a file in the test datadir (BasicTestingSetup::m_path_root), to the standard terminal output.

... or to run just the doubledash test:

test_bitcoin --run_test=getarg_tests/doubledash

Run test_bitcoin --help for the full list.

Adding test cases

To add a new unit test file to our test suite you need to add the file to src/Makefile.test.include. The pattern is to create one test file for each class or source file for which you want to create unit tests. The file naming convention is <source_filename>_tests.cpp and such files should wrap their tests in a test suite called <source_filename>_tests. For an example of this pattern, see uint256_tests.cpp.

Logging and debugging in unit tests

make check will write to a log file foo_tests.cpp.log and display this file on failure. For running individual tests verbosely, refer to the section above.

To write to logs from unit tests you need to use specific message methods provided by Boost. The simplest is BOOST_TEST_MESSAGE.

For debugging you can launch the test_bitcoin executable with gdbor lldb and start debugging, just like you would with any other program:

gdb src/test/test_bitcoin