Files
Library/tests/standalone/Async/FuturePool.cpp

246 lines
7.5 KiB
C++

#include <Void/Async/FuturePool.hpp>
#include <Void/Async/ThreadExecutor.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <future>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <utility>
// All assertions run on the test thread; callables executed by workers only
// touch atomics (the assertion context is thread_local).
namespace
{
using namespace Void::Async;
using namespace std::chrono_literals;
class Gate
{
public:
void open()
{
{
std::lock_guard lk{mu_};
open_ = true;
}
cv_.notify_all();
}
void wait()
{
std::unique_lock lk{mu_};
cv_.wait(lk, [this] { return open_; });
}
private:
std::mutex mu_{};
std::condition_variable cv_{};
bool open_{false};
};
template <class Pred>
bool wait_until(Pred pred, std::chrono::milliseconds timeout = 5000ms)
{
const auto end = std::chrono::steady_clock::now() + timeout;
while (!pred()) {
if (std::chrono::steady_clock::now() >= end) {
return false;
}
std::this_thread::sleep_for(1ms);
}
return true;
}
// Opens the gate before the executor is destroyed, so a failed assertion can
// never leave a blocked callable hanging ~ThreadExecutor.
struct Fixture {
Gate gate{};
std::unique_ptr<ThreadExecutor> exec{};
Fixture(std::size_t workers, std::size_t max_admitted)
{
auto created = ThreadExecutor::create(
ThreadExecutorOptions{.workers = workers, .max_admitted = max_admitted});
Void::Testing::require(created.has_value());
exec = std::move(*created);
Void::Testing::require(exec->start().has_value());
}
~Fixture() { gate.open(); }
Fixture(const Fixture&) = delete;
Fixture& operator=(const Fixture&) = delete;
};
// Move-only callable that records every move: proves a rejected submission
// never consumed it.
struct TrackedCallable {
std::shared_ptr<std::atomic<int>> moves{std::make_shared<std::atomic<int>>(0)};
std::unique_ptr<int> payload{std::make_unique<int>(41)};
std::atomic<bool>* ran{nullptr};
TrackedCallable() = default;
explicit TrackedCallable(std::atomic<bool>* ran_flag) : ran(ran_flag) {}
TrackedCallable(TrackedCallable&& other) noexcept
: moves(other.moves)
, payload(std::move(other.payload))
, ran(other.ran)
{
moves->fetch_add(1);
}
TrackedCallable& operator=(TrackedCallable&&) = delete;
TrackedCallable(const TrackedCallable&) = delete;
TrackedCallable& operator=(const TrackedCallable&) = delete;
int operator()()
{
if (ran != nullptr) {
ran->store(true);
}
return payload ? *payload + 1 : -1;
}
};
} // namespace
Void::Testing::Test future_pool_overloaded_does_not_consume_callable()
{
std::atomic<bool> blocker_running{false};
std::atomic<bool> ran{false};
Fixture fx{1, 1};
FuturePool pool{*fx.exec};
auto blocker = pool.try_submit([&] {
blocker_running.store(true);
fx.gate.wait();
return 7;
});
Void::Testing::require(blocker.has_value());
Void::Testing::require(wait_until([&] { return blocker_running.load(); }));
TrackedCallable callable{&ran};
const auto rejected = pool.try_submit(std::move(callable));
Void::Testing::require(!rejected.has_value());
Void::Testing::expect(rejected.error() == AdmissionError::overloaded);
// Reserve-then-transfer: the callable was not even moved from.
Void::Testing::expect_eq(callable.moves->load(), 0);
Void::Testing::require(callable.payload != nullptr);
Void::Testing::expect_eq(*callable.payload, 41);
Void::Testing::expect(!ran.load());
fx.gate.open();
Void::Testing::expect_eq(blocker->get(), 7);
Void::Testing::require(wait_until([&] { return fx.exec->admitted() == 0; }));
// The very same callable is still usable.
auto accepted = pool.try_submit(std::move(callable));
Void::Testing::require(accepted.has_value());
Void::Testing::expect_eq(accepted->get(), 42);
Void::Testing::expect(ran.load());
}
Void::Testing::Test future_pool_stopped_after_close()
{
Fixture fx{1, 4};
FuturePool pool{*fx.exec};
auto before = pool.try_submit([] { return 1; });
Void::Testing::require(before.has_value());
Void::Testing::expect_eq(before->get(), 1);
fx.exec->close();
std::atomic<bool> ran{false};
TrackedCallable callable{&ran};
const auto stopped = pool.try_submit(std::move(callable));
Void::Testing::require(!stopped.has_value());
Void::Testing::expect(stopped.error() == AdmissionError::stopped);
Void::Testing::expect_eq(callable.moves->load(), 0);
Void::Testing::expect(callable.payload != nullptr);
Void::Testing::expect_eq(fx.exec->admitted(), std::size_t{0});
Void::Testing::require(fx.exec->join().has_value());
Void::Testing::expect(!ran.load());
}
Void::Testing::Test future_pool_exception_in_future()
{
Fixture fx{2, 8};
FuturePool pool{*fx.exec};
auto failing = pool.try_submit([]() -> int { throw std::runtime_error{"boom"}; });
Void::Testing::require(failing.has_value());
bool caught = false;
try {
(void)failing->get();
} catch (const std::runtime_error& error) {
caught = std::string{error.what()} == "boom";
}
Void::Testing::expect(caught);
// void callables work, and a thrown exception does not poison the pool.
std::atomic<int> ran{0};
auto void_job = pool.try_submit([&ran] { ran.fetch_add(1); });
Void::Testing::require(void_job.has_value());
void_job->get();
Void::Testing::expect_eq(ran.load(), 1);
auto after = pool.try_submit([] { return 5; });
Void::Testing::require(after.has_value());
Void::Testing::expect_eq(after->get(), 5);
// Slots are released even when the callable threw.
Void::Testing::expect(wait_until([&] { return fx.exec->admitted() == 0; }));
}
Void::Testing::Test future_pool_bound_includes_running()
{
std::atomic<bool> first_running{false};
std::atomic<int> started{0};
Fixture fx{1, 2};
FuturePool pool{*fx.exec};
auto running = pool.try_submit([&] {
first_running.store(true);
started.fetch_add(1);
fx.gate.wait();
return 1;
});
Void::Testing::require(running.has_value());
Void::Testing::require(wait_until([&] { return first_running.load(); }));
// The running callable still counts against the bound.
Void::Testing::expect_eq(fx.exec->admitted(), std::size_t{1});
auto queued = pool.try_submit([&] {
started.fetch_add(1);
return 2;
});
Void::Testing::require(queued.has_value());
Void::Testing::expect_eq(fx.exec->admitted(), std::size_t{2});
const auto rejected = pool.try_submit([] { return 3; });
Void::Testing::require(!rejected.has_value());
Void::Testing::expect(rejected.error() == AdmissionError::overloaded);
Void::Testing::expect_eq(fx.exec->stats().rejected, std::uint64_t{1});
fx.gate.open();
Void::Testing::expect_eq(running->get(), 1);
Void::Testing::expect_eq(queued->get(), 2);
Void::Testing::expect_eq(started.load(), 2);
Void::Testing::require(wait_until([&] { return fx.exec->admitted() == 0; }));
auto again = pool.try_submit([] { return 4; });
Void::Testing::require(again.has_value());
Void::Testing::expect_eq(again->get(), 4);
}