Tests: Add task scheduler tests

This commit is contained in:
SirLynix 2024-02-02 16:20:51 +01:00
parent fa73e463a6
commit adc6a5c0a5
1 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,48 @@
#include <Nazara/Core/TaskScheduler.hpp>
#include <catch2/catch_test_macros.hpp>
#include <atomic>
SCENARIO("TaskScheduler", "[CORE][TaskScheduler]")
{
for (std::size_t workerCount : { 0, 1, 2, 4 })
{
GIVEN("A task scheduler with " << workerCount << " workers")
{
Nz::TaskScheduler scheduler(4);
WHEN("We add a single task and wait for it")
{
bool executed = false;
scheduler.AddTask([&] { executed = true; });
scheduler.WaitForTasks();
CHECK(executed);
}
WHEN("We add a lot of tasks and wait for all of them")
{
constexpr std::size_t taskCount = 512;
std::vector<Nz::UInt8> completionBuffer(taskCount, 0);
std::atomic_uint count = 0;
for (std::size_t i = 0; i < taskCount; ++i)
{
scheduler.AddTask([&, i]
{
completionBuffer[i]++;
count++;
});
}
scheduler.WaitForTasks();
CHECK(count == taskCount);
for (std::size_t i = 0; i < taskCount; ++i)
{
INFO("checking that task " << i << " was executed once");
CHECK(completionBuffer[i] == 1);
}
}
}
}
}