test: cleanup, use HasReason in threadpool_tests.cpp
What changed, and why it matters
This is a minor cleanup of a test file. It changes how one unit test checks that error messages from background tasks are correctly passed back to the caller. There is no change to the actual Bitcoin Core program that users run, only to the test code, and no security relevance.
No action needed. This is a non-security test-only refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors src/test/threadpool_tests.cpp to use the existing HasReason helper from test/util/common.h instead of an inline lambda that manually compares exception messages. It also builds per-task error messages with strprintf rather than string concatenation. The behavior under test is unchanged; this is purely a readability/maintainability cleanup in the test suite.
Changed components
src/test/threadpool_tests.cppInspect captured patch +6 / −9
diff --git a/src/test/threadpool_tests.cpp b/src/test/threadpool_tests.cpp
index c7878c35..647b0f7c 100644
--- a/src/test/threadpool_tests.cpp
+++ b/src/test/threadpool_tests.cpp
@@ -5,6 +5,7 @@
#include <common/system.h>
#include <logging.h>
#include <random.h>
+#include <test/util/common.h>
#include <util/string.h>
#include <util/threadpool.h>
#include <util/time.h>
@@ -164,21 +165,17 @@ BOOST_AUTO_TEST_CASE(task_exception_propagates_to_future)
ThreadPool threadPool(POOL_NAME);
threadPool.Start(NUM_WORKERS_DEFAULT);
- int num_tasks = 5;
- std::string err_msg{"something wrong happened"};
+ const auto make_err{[&](size_t n) { return strprintf("error on thread #%s", n); }};
+
+ const int num_tasks = 5;
std::vector<std::future<void>> futures;
futures.reserve(num_tasks);
for (int i = 0; i < num_tasks; i++) {
- futures.emplace_back(Submit(threadPool, [err_msg, i]() {
- throw std::runtime_error(err_msg + util::ToString(i));
- }));
+ futures.emplace_back(Submit(threadPool, [&make_err, i] { throw std::runtime_error(make_err(i)); }));
}
for (int i = 0; i < num_tasks; i++) {
- BOOST_CHECK_EXCEPTION(futures.at(i).get(), std::runtime_error, [&](const std::runtime_error& e) {
- BOOST_CHECK_EQUAL(e.what(), err_msg + util::ToString(i));
- return true;
- });
+ BOOST_CHECK_EXCEPTION(futures[i].get(), std::runtime_error, HasReason{make_err(i)});
}
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.