From 022fb8e3906abe54b53753e01054faeaaa85d5c5 Mon Sep 17 00:00:00 2001 From: jeffro256 Date: Thu, 16 Oct 2025 11:18:45 -0500 Subject: [PATCH] unit_tests: @j-berman unit tests for #10157 Co-authored-by: j-berman --- tests/unit_tests/CMakeLists.txt | 1 + tests/unit_tests/threadpool.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 44b487e4b..8298cc1ca 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -87,6 +87,7 @@ set(unit_tests_sources test_protocol_pack.cpp threadpool.cpp tx_proof.cpp + tx_verification_utils.cpp hardfork.cpp unbound.cpp uri.cpp diff --git a/tests/unit_tests/threadpool.cpp b/tests/unit_tests/threadpool.cpp index d89f16167..49bec616f 100644 --- a/tests/unit_tests/threadpool.cpp +++ b/tests/unit_tests/threadpool.cpp @@ -145,3 +145,35 @@ TEST(threadpool, leaf_reentrancy) waiter.wait(); ASSERT_EQ(counter, 500000); } + +static bool check_test_and_set(const std::size_t n, const std::function &fail_condition) +{ + std::shared_ptr tpool(tools::threadpool::getNewForUnitTests(std::max(n, 1))); + tools::threadpool::waiter waiter(*tpool); + + std::atomic_flag fail_occurred{}; + for (std::size_t i = 0; i < n; ++i) + { + tpool->submit(&waiter, [&, i](){ if (fail_condition(i)) { fail_occurred.test_and_set(); }}, true); + } + + return waiter.wait() && !fail_occurred.test_and_set(); +} + +TEST(threadpool, test_and_set) +{ + // No failure + ASSERT_TRUE(check_test_and_set(0, [](std::size_t i) -> bool { return false; })); + static const std::size_t N = 4; + ASSERT_TRUE(check_test_and_set(N, [](std::size_t i) -> bool { return false; })); + + // 1 failure + ASSERT_FALSE(check_test_and_set(N, [](std::size_t i) -> bool { return i == 0; })); + ASSERT_FALSE(check_test_and_set(N, [](std::size_t i) -> bool { return i == 1; })); + ASSERT_FALSE(check_test_and_set(N, [](std::size_t i) -> bool { return i == 2; })); + ASSERT_FALSE(check_test_and_set(N, [](std::size_t i) -> bool { return i == 3; })); + + // Multiple failures + ASSERT_FALSE(check_test_and_set(N, [](std::size_t i) -> bool { return i > 0; })); + ASSERT_FALSE(check_test_and_set(N, [](std::size_t i) -> bool { return true; })); +}