Files
daggy/libdaggy/tests/unit_threadpool.cpp
2022-01-28 10:23:21 -04:00

49 lines
1.1 KiB
C++

#include <catch2/catch.hpp>
#include <future>
#include <iostream>
#include "daggy/ThreadPool.hpp"
using namespace std::chrono_literals;
using namespace daggy;
TEST_CASE("threadpool", "[threadpool]")
{
std::atomic<uint32_t> cnt(0);
ThreadPool tp(10);
std::vector<std::shared_ptr<Future<uint32_t>>> rets;
SECTION("Adding large tasks queues with return values")
{
std::vector<std::shared_ptr<Future<uint32_t>>> res;
for (size_t i = 0; i < 100; ++i)
res.emplace_back(tp.addTask([&cnt]() {
cnt++;
return cnt.load();
}));
for (auto &r : res)
r->get();
REQUIRE(cnt == 100);
}
SECTION("Slow runs")
{
std::vector<std::shared_ptr<Future<void>>> res;
using namespace std::chrono_literals;
for (size_t i = 0; i < 100; ++i)
res.push_back(tp.addTask([&cnt]() {
std::this_thread::sleep_for(20ms);
cnt++;
return;
}));
for (auto &r : res) {
while (! r->ready()) {
std::this_thread::sleep_for(150ms);
}
r->get();
}
REQUIRE(cnt == 100);
}
}