aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorluigi1111 <luigi1111w@gmail.com>2020-09-03 12:25:59 -0500
committerluigi1111 <luigi1111w@gmail.com>2020-09-03 12:25:59 -0500
commitee0b02d0dbdafebd28aa535baae086f8d481d191 (patch)
tree7c188abaf0956567f56a6c44cf3adf0b397823f6 /tests
parentMerge pull request #6789 (diff)
parentthreadpool: guard against exceptions in jobs, and armour plating (diff)
downloadmonero-ee0b02d0dbdafebd28aa535baae086f8d481d191.tar.xz
Merge pull request #6757
6a37da8 threadpool: guard against exceptions in jobs, and armour plating (moneromooo-monero)
Diffstat (limited to 'tests')
-rw-r--r--tests/unit_tests/threadpool.cpp40
1 files changed, 20 insertions, 20 deletions
diff --git a/tests/unit_tests/threadpool.cpp b/tests/unit_tests/threadpool.cpp
index 1307cd738..1017f04ff 100644
--- a/tests/unit_tests/threadpool.cpp
+++ b/tests/unit_tests/threadpool.cpp
@@ -34,46 +34,46 @@
TEST(threadpool, wait_nothing)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
- tools::threadpool::waiter waiter;
- waiter.wait(tpool.get());
+ tools::threadpool::waiter waiter(*tpool);;
+ waiter.wait();
}
TEST(threadpool, wait_waits)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
std::atomic<bool> b(false);
tpool->submit(&waiter, [&b](){ epee::misc_utils::sleep_no_w(1000); b = true; });
ASSERT_FALSE(b);
- waiter.wait(tpool.get());
+ waiter.wait();
ASSERT_TRUE(b);
}
TEST(threadpool, one_thread)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(1));
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
std::atomic<unsigned int> counter(0);
for (size_t n = 0; n < 4096; ++n)
{
tpool->submit(&waiter, [&counter](){++counter;});
}
- waiter.wait(tpool.get());
+ waiter.wait();
ASSERT_EQ(counter, 4096);
}
TEST(threadpool, many_threads)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(256));
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
std::atomic<unsigned int> counter(0);
for (size_t n = 0; n < 4096; ++n)
{
tpool->submit(&waiter, [&counter](){++counter;});
}
- waiter.wait(tpool.get());
+ waiter.wait();
ASSERT_EQ(counter, 4096);
}
@@ -82,44 +82,44 @@ static uint64_t fibonacci(std::shared_ptr<tools::threadpool> tpool, uint64_t n)
if (n <= 1)
return n;
uint64_t f1, f2;
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
tpool->submit(&waiter, [&tpool, &f1, n](){ f1 = fibonacci(tpool, n-1); });
tpool->submit(&waiter, [&tpool, &f2, n](){ f2 = fibonacci(tpool, n-2); });
- waiter.wait(tpool.get());
+ waiter.wait();
return f1 + f2;
}
TEST(threadpool, reentrency)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
uint64_t f = fibonacci(tpool, 13);
- waiter.wait(tpool.get());
+ waiter.wait();
ASSERT_EQ(f, 233);
}
TEST(threadpool, reentrancy)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
uint64_t f = fibonacci(tpool, 13);
- waiter.wait(tpool.get());
+ waiter.wait();
ASSERT_EQ(f, 233);
}
TEST(threadpool, leaf_throws)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests());
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
bool thrown = false, executed = false;
tpool->submit(&waiter, [&](){
try { tpool->submit(&waiter, [&](){ executed = true; }); }
catch(const std::exception &e) { thrown = true; }
}, true);
- waiter.wait(tpool.get());
+ waiter.wait();
ASSERT_TRUE(thrown);
ASSERT_FALSE(executed);
}
@@ -127,20 +127,20 @@ TEST(threadpool, leaf_throws)
TEST(threadpool, leaf_reentrancy)
{
std::shared_ptr<tools::threadpool> tpool(tools::threadpool::getNewForUnitTests(4));
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
std::atomic<int> counter(0);
for (int i = 0; i < 1000; ++i)
{
tpool->submit(&waiter, [&](){
- tools::threadpool::waiter waiter;
+ tools::threadpool::waiter waiter(*tpool);
for (int j = 0; j < 500; ++j)
{
tpool->submit(&waiter, [&](){ ++counter; }, true);
}
- waiter.wait(tpool.get());
+ waiter.wait();
});
}
- waiter.wait(tpool.get());
+ waiter.wait();
ASSERT_EQ(counter, 500000);
}