diff --git a/src/avalanche/test/fixture.cpp b/src/avalanche/test/fixture.cpp --- a/src/avalanche/test/fixture.cpp +++ b/src/avalanche/test/fixture.cpp @@ -4,3 +4,16 @@ #define BOOST_TEST_MODULE Avalanche Test Suite #include + +#include + +/** Redirect debug log to boost log */ +const std::function G_TEST_LOG_FUN = + [](const std::string &s) { + if (s.back() == '\n') { + // boost will insert the new line + BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1)); + } else { + BOOST_TEST_MESSAGE(s); + } + }; diff --git a/src/bench/bench.cpp b/src/bench/bench.cpp --- a/src/bench/bench.cpp +++ b/src/bench/bench.cpp @@ -11,6 +11,8 @@ #include +const std::function G_TEST_LOG_FUN{}; + namespace { void GenerateTemplateResults( diff --git a/src/pow/test/fixture.cpp b/src/pow/test/fixture.cpp --- a/src/pow/test/fixture.cpp +++ b/src/pow/test/fixture.cpp @@ -4,3 +4,16 @@ #define BOOST_TEST_MODULE Difficulty Adjustement Test Suite #include + +#include + +/** Redirect debug log to boost log */ +const std::function G_TEST_LOG_FUN = + [](const std::string &s) { + if (s.back() == '\n') { + // boost will insert the new line + BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1)); + } else { + BOOST_TEST_MESSAGE(s); + } + }; diff --git a/src/qt/test/test_main.cpp b/src/qt/test/test_main.cpp --- a/src/qt/test/test_main.cpp +++ b/src/qt/test/test_main.cpp @@ -45,6 +45,8 @@ #endif #endif +const std::function G_TEST_LOG_FUN{}; + // This is all you need to run all the tests int main(int argc, char *argv[]) { // Initialize persistent globals with the testing setup state for sanity. diff --git a/src/seeder/test/fixture.cpp b/src/seeder/test/fixture.cpp --- a/src/seeder/test/fixture.cpp +++ b/src/seeder/test/fixture.cpp @@ -5,3 +5,16 @@ #define BOOST_TEST_MODULE Seeder Test Suite #include + +#include + +/** Redirect debug log to boost log */ +const std::function G_TEST_LOG_FUN = + [](const std::string &s) { + if (s.back() == '\n') { + // boost will insert the new line + BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1)); + } else { + BOOST_TEST_MESSAGE(s); + } + }; diff --git a/src/test/fixture.cpp b/src/test/fixture.cpp --- a/src/test/fixture.cpp +++ b/src/test/fixture.cpp @@ -2,6 +2,10 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +/** + * See + * https://www.boost.org/doc/libs/1_71_0/libs/test/doc/html/boost_test/utf_reference/link_references/link_boost_test_module_macro.html + */ #define BOOST_TEST_MODULE Bitcoin ABC unit tests #include @@ -12,6 +16,17 @@ namespace utf = boost::unit_test::framework; +/** Redirect debug log to boost log */ +const std::function G_TEST_LOG_FUN = + [](const std::string &s) { + if (s.back() == '\n') { + // boost will insert the new line + BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1)); + } else { + BOOST_TEST_MESSAGE(s); + } + }; + /** * Global fixture for passing custom arguments, and clearing them all after each * test case. diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp --- a/src/test/fuzz/fuzz.cpp +++ b/src/test/fuzz/fuzz.cpp @@ -4,10 +4,14 @@ #include +#include + #include #include #include +const std::function G_TEST_LOG_FUN{}; + // Decide if main(...) should be provided: // * AFL needs main(...) regardless of platform. // * macOS handles __attribute__((weak)) main(...) poorly when linking @@ -18,6 +22,7 @@ #endif #if defined(PROVIDE_MAIN_FUNCTION) + static bool read_stdin(std::vector &data) { uint8_t buffer[1024]; ssize_t length = 0; diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -30,6 +30,10 @@ */ #define NULLPTR(T) static_cast(nullptr) +/** This is connected to the logger. Can be used to redirect logs to any other + * log */ +extern const std::function G_TEST_LOG_FUN; + // Enable BOOST_CHECK_EQUAL for enum class types template std::ostream &operator<<( diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -119,6 +119,9 @@ } SelectParams(chainName); SeedInsecureRand(); + if (G_TEST_LOG_FUN) { + LogInstance().PushBackCallback(G_TEST_LOG_FUN); + } InitLogging(*m_node.args); AppInitParameterInteraction(config, *m_node.args); LogInstance().StartLogging(); diff --git a/test/sanitizer_suppressions/tsan b/test/sanitizer_suppressions/tsan --- a/test/sanitizer_suppressions/tsan +++ b/test/sanitizer_suppressions/tsan @@ -41,6 +41,14 @@ # Intentional deadlock in tests deadlock:TestPotentialDeadLockDetected +# Race due to unprotected calls to thread-unsafe BOOST_TEST_MESSAGE from different threads: +# * G_TEST_LOG_FUN in the index thread +# * boost test case invoker (entering a test case) in the main thread +# TODO: get rid of BOOST_ macros, see also https://github.com/bitcoin/bitcoin/issues/8670 +race:blockfilter_index_initial_sync_invoker +race:txindex_initial_sync_invoker +race:validation_block_tests::TestSubscriber + # Wildcard for all gui tests, should be replaced with non-wildcard suppressions mutex:src/qt/test/* race:src/qt/test/*