mirror of
https://github.com/monero-project/monero.git
synced 2026-01-10 12:13:26 -08:00
unit_tests: @j-berman unit tests for #10157
Co-authored-by: j-berman <justinberman@protonmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<bool(std::size_t)> &fail_condition)
|
||||
{
|
||||
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(std::max<std::size_t>(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; }));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user