Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
fa07237 to
fa2192d
Compare
|
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 |
fa27694 to
faea07b
Compare
|
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 failuresdiff --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
|
faea07b to
fac40f8
Compare
86f6281 to
fa46ae9
Compare
|
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? |
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.
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? |
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: edit: |
|
Concept ACK |
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. |
fa46ae9 to
30702cc
Compare
-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-
30702cc to
687af6d
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |

Currently, the unit tests use different boost check macros. However, this is problematic:
BOOST_CHECK_GT(-10, 10U);compiles and passes fine, even though it is obviously wrong.assert, but it would be better if the macros just worked under tsan.assertandAssert, and evenCHECK_NONFATALunder 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 asASSERT_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, andAssert). While it may confusing to have three unaryassertmacros that do roughly the same, it should be harmless and any macro that compiles is fine to use. (There is a special edge case toAssert, which allows to be turned into an exception as part ofg_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
assertorAssert, 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.