Skip to content

Commit 40a495a

Browse files
committed
Merge #16975: test: Show debug log on unit test failure
fa37e0a test: Show debug log on unit test failure (MarcoFalke) Pull request description: Often, it is hard to debug unit test failures without the debug log. Especially when the failure happens remotely (e.g. on a ci system). Fix that by printing the log on failure. ACKs for top commit: jamesob: ACK fa37e0a ([`jamesob/ackr/16975.1.MarcoFalke.test_show_debug_log_on_u`](https://github.com/jamesob/bitcoin/tree/ackr/16975.1.MarcoFalke.test_show_debug_log_on_u)) Tree-SHA512: 2ca4150c4ae3d4ad47e03b5e5e70da2baffec928ddef1fdf53a3ebc061f14aee249205387cb1b12ef6d4eb55711ef0080c0b41d9d18000b5da124ca80299793b
2 parents b065df8 + fa37e0a commit 40a495a

File tree

7 files changed

+34
-0
lines changed

7 files changed

+34
-0
lines changed

src/bench/bench.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <regex>
1717

1818
const RegTestingSetup* g_testing_setup = nullptr;
19+
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
1920

2021
void benchmark::ConsolePrinter::header()
2122
{

src/qt/test/test_main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Q_IMPORT_PLUGIN(QCocoaIntegrationPlugin);
3737
#endif
3838
#endif
3939

40+
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
41+
4042
// This is all you need to run all the tests
4143
int main(int argc, char *argv[])
4244
{

src/test/fuzz/fuzz.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
#include <test/fuzz/fuzz.h>
66

7+
#include <test/util/setup_common.h>
8+
79
#include <cstdint>
810
#include <unistd.h>
911
#include <vector>
1012

13+
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
14+
1115
static bool read_stdin(std::vector<uint8_t>& data)
1216
{
1317
uint8_t buffer[1024];

src/test/main.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
/**
6+
* 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
7+
*/
58
#define BOOST_TEST_MODULE Bitcoin Core Test Suite
69

710
#include <boost/test/unit_test.hpp>
11+
12+
#include <test/util/setup_common.h>
13+
14+
/** Redirect debug log to boost log */
15+
const std::function<void(const std::string&)> G_TEST_LOG_FUN = [](const std::string& s) {
16+
if (s.back() == '\n') {
17+
// boost will insert the new line
18+
BOOST_TEST_MESSAGE(s.substr(0, s.size() - 1));
19+
} else {
20+
BOOST_TEST_MESSAGE(s);
21+
}
22+
};

src/test/util/setup_common.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
7171
SelectParams(chainName);
7272
SeedInsecureRand();
7373
gArgs.ForceSetArg("-printtoconsole", "0");
74+
if (G_TEST_LOG_FUN) LogInstance().PushBackCallback(G_TEST_LOG_FUN);
7475
InitLogging();
7576
LogInstance().StartLogging();
7677
SHA256AutoDetect();

src/test/util/setup_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
#include <boost/thread.hpp>
2020

21+
/** This is connected to the logger. Can be used to redirect logs to any other log */
22+
extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
23+
2124
// Enable BOOST_CHECK_EQUAL for enum class types
2225
template <typename T>
2326
std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)

test/sanitizer_suppressions/tsan

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ deadlock:WalletBatch
77
# Intentional deadlock in tests
88
deadlock:TestPotentialDeadLockDetected
99

10+
# Race due to unprotected calls to thread-unsafe BOOST_TEST_MESSAGE from different threads:
11+
# * G_TEST_LOG_FUN in the index thread
12+
# * boost test case invoker (entering a test case) in the main thread
13+
# TODO: get rid of BOOST_ macros, see also https://github.com/bitcoin/bitcoin/issues/8670
14+
race:blockfilter_index_initial_sync_invoker
15+
race:txindex_initial_sync_invoker
16+
race:validation_block_tests::TestSubscriber
17+
1018
# Wildcard for all gui tests, should be replaced with non-wildcard suppressions
1119
race:src/qt/test/*
1220
deadlock:src/qt/test/*

0 commit comments

Comments
 (0)