Skip to content

test: [NOMERGE,SKETCH] Add thread-safe fast-failing test macros - #35139

Draft
maflcko wants to merge 3 commits into
bitcoin:masterfrom
maflcko:2604-test-fail-fast-thread-safe-macros
Draft

maflcko wants to merge 3 commits into
bitcoin:masterfrom
maflcko:2604-test-fail-fast-thread-safe-macros

Conversation

@maflcko

@maflcko maflcko commented Apr 22, 2026 •

Copy link
Copy Markdown
Member

Currently, the unit tests use different boost check macros. However, this is problematic:

  • The check macros are tied to the boost test framework and can not be used in other tests, such as the fuzz tests.
  • The check macros are not overflow-safe. For example BOOST_CHECK_GT(-10, 10U); compiles and passes fine, even though it is obviously wrong.
  • The check macros are not thread-safe. Using them in a thread will compile fine, but then fail tsan later on. Usually it is trivial to replace them by a plain assert, but it would be better if the macros just worked under tsan.
  • The check macros are not fast-failing. This is confusing, as the test continues to run and will print more logs, making it harder to spot the first failure. Also, it is contrary to the internal check macros assert and Assert, and even CHECK_NONFATAL under debug mode, which all lead to an immediate abort.

Fix all issues by adding new check macros: ASSERT, ASSERT_EQ (and friends), ASSERT_EQ_COLLECTIONS (C++20 range-based), as well as ASSERT_EXCEPTION. They can be used as a drop-in replacement for the corresponding boost macro.

The implementation for all macros is only ~150 LOC, so it should be preferable to any external dependency addressing the above issues (which I couldn't find anyway).

(Tracking issue: #34666)

Note on naming (why ASSERT?)

The test macros follow the naming and semantics of the internal Bitcoin Core macros (assert, and Assert). While it may confusing to have three unary assert macros that do roughly the same, it should be harmless and any macro that compiles is fine to use. (There is a special edge case to Assert, which allows to be turned into an exception as part of g_detail_test_only_CheckFailuresAreExceptionsNotAborts, but this shouldn't matter much).

In theory, if devs didn't care about nice diagnostic messages on a failure (printing the left and right side), the macros could even be a direct alias of assert or Assert, reducing the LOC even more. Though, I think it is fine and preferable to have slightly better failure messages in tests.

Also, the naming and semantics follow the Bitcoin Core functional test macros, which also start with assert_.

Later optimizations

The macros are currently sitting in the header, but once they are applied to the whole codebase, the pretty-printing implementation can move to a cpp file, which reduces the weight of the header and makes it possible to change or add pretty-printing features without a whole re-compilation of all tests.

@DrahtBot DrahtBot changed the title test: Add thread-safe fast-failing test macros test: Add thread-safe fast-failing test macros Apr 22, 2026
@DrahtBot DrahtBot added the Tests label Apr 22, 2026
@DrahtBot

DrahtBot commented Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Concept ACK sedited

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #35003 (validation: improve block data I/O error handling in P2P paths by furszy)
  • #33324 (blocks: add resumable reobfuscation for existing block files by l0rinc)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@maflcko
maflcko force-pushed the 2604-test-fail-fast-thread-safe-macros branch from fa07237 to fa2192d Compare April 22, 2026 15:04
@maflcko

maflcko commented Apr 22, 2026

Copy link
Copy Markdown
Member Author

To test the Tsan error, one can use this diff:

diff --git a/src/test/btcsignals_tests.cpp b/src/test/btcsignals_tests.cpp
index b3203ebeb2..43a68607a3 100644
--- a/src/test/btcsignals_tests.cpp
+++ b/src/test/btcsignals_tests.cpp
@@ -5,2 +5,3 @@
 #include <btcsignals.h>
+#include <test/util/common.h>
 #include <test/util/setup_common.h>
@@ -162,3 +163,3 @@ BOOST_AUTO_TEST_CASE(thread_safety)
         // a race inside of BOOST_CHECK itself (writing to the log).
-        assert(!sig0.empty());
+        ASSERT(!sig0.empty());
         assert(conn0.connected());

Tsan passes with ASSSERT or assert, or Assert, but fails with BOOST_CHECK when running this test.

@maflcko
maflcko force-pushed the 2604-test-fail-fast-thread-safe-macros branch 2 times, most recently from fa27694 to faea07b Compare April 22, 2026 20:03
@maflcko

maflcko commented Apr 22, 2026

Copy link
Copy Markdown
Member Author

To test the overall behavior, and compare it with the boost behavior, one can use this diff (it includes recommended instructions to run)

a diff to trigger different failures
diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp
index 9bf7f6f6fd..e6a4049328 100644
--- a/src/test/streams_tests.cpp
+++ b/src/test/streams_tests.cpp
@@ -17,6 +17,154 @@
 using namespace std::string_literals;
 using namespace util::hex_literals;
 
+// All suites below will fail
+// For testing, run them via Bash:
+// clang-format off
+// cmake --build ./bld-cmake --parallel && for s in Suite_Bool Suite_Equality Suite_Null Suite_Null2 Suite_Inequality Suite_LessThan Suite_GreaterThan Suite_LessEqual Suite_GreaterEqual Suite_Collections Suite_CollectionsEnd Suite_Exceptions Suite_ExceptionsWrongCatch Suite_ExceptionsWrongThrow; do valgrind ./bld-cmake/bin/test_bitcoin --catch_system_error=no -t $s; echo "Exit code: $?"; done
+// clang-format on
+BOOST_AUTO_TEST_SUITE(Suite_Bool)
+BOOST_AUTO_TEST_CASE(Test_Assert)
+{
+    short* condition{};
+    BOOST_CHECK(condition);
+    ASSERT(condition);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_Equality)
+BOOST_AUTO_TEST_CASE(Test_AssertEq)
+{
+    const char* a{"hi"};
+    std::cout << "Passes on both:\n";
+    BOOST_CHECK_EQUAL(a, "hi");
+    ASSERT_EQ(a, "hi");
+
+    std::cout << "Fails on the new macro (This check is stricter and will do pointer-compare)\n";
+    char b[3] = {'h', 'i', '\0'};
+    BOOST_CHECK_EQUAL(b, "hi");
+    using namespace std::literals;
+    ASSERT_EQ(b, "hi"); // Use "hi"sv or "hi"s instead
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_Null)
+BOOST_AUTO_TEST_CASE(Test_AssertNull)
+{
+    const char* a{"hi"};
+    BOOST_CHECK_EQUAL(a, nullptr);
+    ASSERT_EQ(a, nullptr);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_Null2)
+BOOST_AUTO_TEST_CASE(Test_AssertNull2)
+{
+    const char* a{};
+    BOOST_CHECK_EQUAL(a, "hi");
+    ASSERT_EQ(a, "hi");
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_Inequality)
+BOOST_AUTO_TEST_CASE(Test_AssertNe)
+{
+    bool a{true};
+    BOOST_CHECK_NE(a, true);
+    ASSERT_NE(a, true);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_LessThan)
+BOOST_AUTO_TEST_CASE(Test_AssertLt)
+{
+    BOOST_CHECK_LT(10, 5U);
+    ASSERT_LT(10U, 5U); // Checks at compile time that the same signdness is provided!
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_GreaterThan)
+BOOST_AUTO_TEST_CASE(Test_AssertGt)
+{
+    std::cout << "This should obviously fail, but boost passes!\n";
+    BOOST_CHECK_GT(-10, 10U);
+    // ASSERT_GT(-10, 10U); // does not compile
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_LessEqual)
+BOOST_AUTO_TEST_CASE(Test_AssertLe)
+{
+    BOOST_CHECK_LE(10.1, 5.1);
+    ASSERT_LE(10.1, 5.1);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_GreaterEqual)
+BOOST_AUTO_TEST_CASE(Test_AssertGe)
+{
+    BOOST_CHECK_GE(char{12}, 'a');
+    ASSERT_GE(char{12}, 'a');
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_Collections)
+BOOST_AUTO_TEST_CASE(Test_AssertCollections)
+{
+    std::vector<int> v1{1, 2, 3}, v2{1, 99, 3};
+    BOOST_CHECK_EQUAL_COLLECTIONS(v1.begin(), v1.end(), v2.begin(), v2.end());
+    ASSERT_EQ_COLLECTIONS(v1, v2);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_CollectionsEnd)
+BOOST_AUTO_TEST_CASE(Test_AssertCollectionsEnd)
+{
+    std::vector<NodeSeconds> v1{
+        NodeSeconds{1s},
+    },
+        v2{};
+    BOOST_CHECK_EQUAL_COLLECTIONS(v1.begin(), v1.end(), v2.begin(), v2.end());
+    ASSERT_EQ_COLLECTIONS(v1, v2);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_Exceptions)
+BOOST_AUTO_TEST_CASE(Test_AssertException)
+{
+    auto thrower = []() { throw std::runtime_error("test error"); };
+    auto predicate = [](const std::runtime_error&) { return false; };
+
+    BOOST_CHECK_EXCEPTION(thrower(), std::runtime_error, predicate);
+    ASSERT_EXCEPTION(thrower(), std::runtime_error, predicate);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_ExceptionsWrongCatch)
+BOOST_AUTO_TEST_CASE(Test_AssertExceptionWrongCatch)
+{
+    auto thrower = []() { throw std::runtime_error("test error"); };
+    auto predicate = [](const int&) { return false; };
+
+    try {
+        BOOST_CHECK_EXCEPTION(thrower(), int, predicate);
+    } catch (...) {
+        std::cout << "boost lets the exception through\n";
+    }
+    ASSERT_EXCEPTION(thrower(), int, predicate);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
+BOOST_AUTO_TEST_SUITE(Suite_ExceptionsWrongThrow)
+BOOST_AUTO_TEST_CASE(Test_AssertExceptionWrongThrow)
+{
+    auto thrower = []() {};
+    auto predicate = [](const int&) { return false; };
+
+    BOOST_CHECK_EXCEPTION(thrower(), int, predicate);
+    ASSERT_EXCEPTION(thrower(), int, predicate);
+}
+BOOST_AUTO_TEST_SUITE_END()
+
 BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
 
 // Check optimized obfuscation with random offsets and sizes to ensure proper

Comment thread src/test/util/common.h Resolved
@maflcko
maflcko force-pushed the 2604-test-fail-fast-thread-safe-macros branch 2 times, most recently from 86f6281 to fa46ae9 Compare May 11, 2026 07:31
@sedited

sedited commented May 26, 2026

Copy link
Copy Markdown
Contributor

Not sure about these changes. I've found it useful in the past that the tests are not failing fast. Both to have more context when debugging, and to see all failing cases at once when patching them. Judged by the lack of engagement here, I'm also not sure if others would find that valuable too. The value I could see is in the shared utilities between the fuzz and unit tests, but even there we could just use plain asserts, since they are not actual test cases?

@maflcko

maflcko commented May 28, 2026 •

Copy link
Copy Markdown
Member Author

Not sure about these changes. I've found it useful in the past that the tests are not failing fast. Both to have more context when debugging, and to see all failing cases at once when patching them.

I guess this may be subjective. The context before a failure is certainly useful, but the context after a failure is often counter-productive (think about a 10'000 line github log, where the failure happens in the first 1000 lines, or think about a test failure that sets off an avalanche of other failures, where only the first one is the relevant one to fix). Again, this may be subjective (I've heard preferences either way), so I am happy to add an env var to control the behavior here.

Judged by the lack of engagement here, I'm also not sure if others would find that valuable too.

Yeah, I think the main issue is that all the annoyances are not worth it when looked at in a vacuum. However, while the boost test framework was the right choice in 2011, I am not sure it is the right choice in a modern C++ codebase:

I understand the review bottleneck, and I don't want to force this on anyone, but I want to highlight the issues and ask if we want to address them here, not at all, or upstream?

@maflcko

maflcko commented Jul 7, 2026 •

Copy link
Copy Markdown
Member Author

The context before a failure is certainly useful, but the context after a failure is often counter-productive (think about a 10'000 line github log

I keep running into this, and it is annoying. E.g. try to find the failure in https://github.com/bitcoin/bitcoin/actions/runs/28867247659/job/85623740486?pr=35676#step:9:2171. The full log doesn't show the failure in the tail of the log:

https://gist.github.com/maflcko/4b2b6c7e1a0d23841cbe7d962ed61525

edit:

@sedited

sedited commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@maflcko
maflcko marked this pull request as draft July 7, 2026 13:59
@maflcko

maflcko commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

Reviewers, this pull request conflicts with the following ones:

* [#35587](https://github.com/bitcoin/bitcoin/pull/35587) (Remove boost as a unit test runner by rustaceanrob)

Turned to draft into now, as a conflicting pull was opened in the meantime. Probably good to keep the number of non-draft pulls to a minimum.

MarcoFalke added 3 commits November 20, 2026 07:32
-BEGIN VERIFY SCRIPT-

 ren() { sed --in-place --regexp-extended "s|\<$1\>|$2|g" src/test/streams_tests.cpp ; }

 # "Boring" replacements:
 ren BOOST_CHECK           ASSERT
 ren BOOST_CHECK_EQUAL     ASSERT_EQ
 ren BOOST_REQUIRE_EQUAL   ASSERT_EQ
 ren BOOST_CHECK_EXCEPTION ASSERT_EXCEPTION

 # "Advanced" replacements:
 # Append empty HasReason operator
 sed --in-place --regexp-extended 's/BOOST_CHECK_THROW\((.+)\)/ASSERT_EXCEPTION(\1, HasReason{""})/g' src/test/streams_tests.cpp

-END VERIFY SCRIPT-
@maflcko
maflcko force-pushed the 2604-test-fail-fast-thread-safe-macros branch from 30702cc to 687af6d Compare November 20, 2026 06:33
@DrahtBot

Copy link
Copy Markdown
Contributor

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/44688592584/job/136128723071
LLM reason (✨ experimental): CI failed because the include linter detected a duplicate #include <utility> in src/test/util/common.h.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@maflcko maflcko changed the title test: Add thread-safe fast-failing test macros test: [NOMERGE,SKETCH] Add thread-safe fast-failing test macros Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants