2021-09-06 06:54:24 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <EC/ThreadPool.hpp>
|
|
|
|
|
2021-09-07 02:46:38 +00:00
|
|
|
using OneThreadPool = EC::ThreadPool<1>;
|
2021-09-06 06:54:24 +00:00
|
|
|
using ThreeThreadPool = EC::ThreadPool<3>;
|
|
|
|
|
2021-09-07 02:46:38 +00:00
|
|
|
TEST(ECThreadPool, CannotCompile) {
|
|
|
|
OneThreadPool p;
|
|
|
|
std::atomic_int data;
|
|
|
|
data.store(0);
|
|
|
|
const auto fn = [](void *ud) {
|
|
|
|
auto *data = static_cast<std::atomic_int*>(ud);
|
|
|
|
data->fetch_add(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
p.queueFn(fn, &data);
|
|
|
|
|
|
|
|
p.wakeThreads();
|
|
|
|
|
|
|
|
do {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
} while(!p.isQueueEmpty() && !p.isAllThreadsWaiting());
|
|
|
|
|
|
|
|
ASSERT_EQ(data.load(), 1);
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < 10; ++i) {
|
|
|
|
p.queueFn(fn, &data);
|
|
|
|
}
|
|
|
|
p.wakeThreads();
|
|
|
|
|
|
|
|
do {
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
} while(!p.isQueueEmpty() && !p.isAllThreadsWaiting());
|
|
|
|
|
|
|
|
ASSERT_EQ(data.load(), 11);
|
|
|
|
}
|
2021-09-06 06:54:24 +00:00
|
|
|
|
|
|
|
TEST(ECThreadPool, Simple) {
|
2021-09-07 02:46:38 +00:00
|
|
|
ThreeThreadPool p;
|
2021-09-06 06:54:24 +00:00
|
|
|
std::atomic_int data;
|
|
|
|
data.store(0);
|
|
|
|
const auto fn = [](void *ud) {
|
|
|
|
auto *data = static_cast<std::atomic_int*>(ud);
|
|
|
|
data->fetch_add(1);
|
|
|
|
};
|
|
|
|
|
|
|
|
p.queueFn(fn, &data);
|
|
|
|
|
|
|
|
p.wakeThreads();
|
|
|
|
|
2021-09-06 11:57:13 +00:00
|
|
|
do {
|
2021-09-06 06:54:24 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
2021-09-07 02:29:06 +00:00
|
|
|
} while(!p.isQueueEmpty() && !p.isAllThreadsWaiting());
|
2021-09-06 06:54:24 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(data.load(), 1);
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < 10; ++i) {
|
|
|
|
p.queueFn(fn, &data);
|
|
|
|
}
|
|
|
|
p.wakeThreads();
|
|
|
|
|
2021-09-06 11:57:13 +00:00
|
|
|
do {
|
2021-09-06 06:54:24 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
2021-09-07 02:29:06 +00:00
|
|
|
} while(!p.isQueueEmpty() && !p.isAllThreadsWaiting());
|
2021-09-06 06:54:24 +00:00
|
|
|
|
|
|
|
ASSERT_EQ(data.load(), 11);
|
|
|
|
}
|
2021-09-07 11:21:20 +00:00
|
|
|
|
|
|
|
TEST(ECThreadPool, QueryCount) {
|
|
|
|
{
|
|
|
|
OneThreadPool oneP;
|
|
|
|
ASSERT_EQ(1, oneP.getThreadCount());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ThreeThreadPool threeP;
|
|
|
|
ASSERT_EQ(3, threeP.getThreadCount());
|
|
|
|
}
|
|
|
|
}
|