feat add thread_pool test case
Some checks failed
linux-mips64-gcc / linux-gcc-mips64el (Release) (push) Successful in 2m22s
linux-x64-gcc / linux-gcc (Debug) (push) Failing after 1m24s
linux-x64-gcc / linux-gcc (Release) (push) Failing after 1m21s
linux-mips64-gcc / linux-gcc-mips64el (Debug) (push) Successful in 4m3s

This commit is contained in:
tqcq 2024-03-31 22:16:53 +08:00
parent a711d6bfb4
commit 9444c23db2
2 changed files with 38 additions and 16 deletions

View File

@ -18,7 +18,7 @@ public:
static constexpr TimeDelta kForever = ConditionVariable::kForever; static constexpr TimeDelta kForever = ConditionVariable::kForever;
Event(); Event();
Event(bool manual_reset, bool initially_signaled); Event(bool manual_reset, bool initially_signaled);
Event(const Event &) = delete; Event(const Event &) = delete;
Event &operator=(const Event &) = delete; Event &operator=(const Event &) = delete;
~Event(); ~Event();

View File

@ -1,4 +1,5 @@
#include <random> #include <random>
#include <sled/synchronization/event.h>
#include <sled/system/thread_pool.h> #include <sled/system/thread_pool.h>
std::random_device rd; std::random_device rd;
@ -37,26 +38,47 @@ multiply_return(const int a, const int b)
return res; return res;
} }
TEST_CASE("ThreadPool") TEST_SUITE("ThreadPool")
{ {
sled::ThreadPool *tp = new sled::ThreadPool(); TEST_CASE("submit")
REQUIRE_NE(tp, nullptr); {
sled::ThreadPool *tp = new sled::ThreadPool();
REQUIRE_NE(tp, nullptr);
SUBCASE("Output") SUBCASE("Output")
{ {
for (int i = 0; i < 100; ++i) { for (int i = 0; i < 100; ++i) {
int out; int out;
tp->submit(multiply_output, std::ref(out), i, i).get(); tp->submit(multiply_output, std::ref(out), i, i).get();
CHECK_EQ(out, i * i); CHECK_EQ(out, i * i);
}
} }
SUBCASE("Return")
{
for (int i = 0; i < 100; ++i) {
auto f = tp->submit(multiply_return, i, i);
CHECK_EQ(f.get(), i * i);
}
}
delete tp;
} }
SUBCASE("Return") TEST_CASE("PostTask")
{ {
for (int i = 0; i < 100; ++i) { sled::ThreadPool *tp = new sled::ThreadPool();
auto f = tp->submit(multiply_return, i, i); sled::Event waiter;
CHECK_EQ(f.get(), i * i); tp->PostTask([&]() { waiter.Set(); });
} CHECK(waiter.Wait(sled::TimeDelta::Seconds(1)));
delete tp;
} }
delete tp; TEST_CASE("PostDelayedTask")
{
sled::ThreadPool *tp = new sled::ThreadPool();
sled::Event waiter;
tp->PostDelayedTask([&]() { waiter.Set(); }, sled::TimeDelta::Millis(100));
CHECK_FALSE(waiter.Wait(sled::TimeDelta::Millis(50)));
CHECK(waiter.Wait(sled::TimeDelta::Millis(150)));
delete tp;
}
} }