141 lines
3.6 KiB
C++
141 lines
3.6 KiB
C++
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <concepts>
|
|
#include <condition_variable>
|
|
#include <cstddef>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
namespace Void::Testing
|
|
{
|
|
|
|
/// Small synchronization gate for deterministic completion-order tests.
|
|
///
|
|
/// Participants call `arrive()` after a computation returned and can then
|
|
/// block in `wait_release()`. The test thread can wait for a number of
|
|
/// arrivals (`wait_for`) and release blocked participants explicitly, on
|
|
/// failure, or after a timeout. All waits are bounded.
|
|
class CompletionGate
|
|
{
|
|
public:
|
|
|
|
using Clock = std::chrono::steady_clock;
|
|
|
|
CompletionGate() = default;
|
|
|
|
CompletionGate(const CompletionGate&) = delete;
|
|
CompletionGate& operator=(const CompletionGate&) = delete;
|
|
|
|
/// Record one completed invocation and wake `wait_for` waiters.
|
|
void arrive() noexcept
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
++arrivals_;
|
|
condition_.notify_all();
|
|
}
|
|
|
|
[[nodiscard]] std::size_t arrivals() const noexcept
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
return arrivals_;
|
|
}
|
|
|
|
/// Wait until at least `count` invocations arrived. Returns false on
|
|
/// timeout.
|
|
[[nodiscard]] bool wait_for(std::size_t count,
|
|
std::chrono::milliseconds timeout)
|
|
{
|
|
std::unique_lock lock{mutex_};
|
|
return condition_.wait_for(
|
|
lock, timeout, [&] { return arrivals_ >= count; });
|
|
}
|
|
|
|
/// Unblock every current and future `wait_release` caller.
|
|
void release() noexcept
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
released_ = true;
|
|
condition_.notify_all();
|
|
}
|
|
|
|
[[nodiscard]] bool released() const noexcept
|
|
{
|
|
std::lock_guard lock{mutex_};
|
|
return released_;
|
|
}
|
|
|
|
/// Block until `release()`; returns false on timeout so a failing test
|
|
/// can still finish.
|
|
[[nodiscard]] bool wait_release(std::chrono::milliseconds timeout)
|
|
{
|
|
std::unique_lock lock{mutex_};
|
|
return condition_.wait_for(lock, timeout,
|
|
[&] { return released_; });
|
|
}
|
|
|
|
private:
|
|
|
|
mutable std::mutex mutex_;
|
|
std::condition_variable condition_;
|
|
std::size_t arrivals_{0};
|
|
bool released_{false};
|
|
};
|
|
|
|
/// Callable adapter that runs an observer exactly once after a successful
|
|
/// invocation returns, and before the enclosing future is fulfilled.
|
|
///
|
|
/// The observer is *not* called when the callable throws; the exception is
|
|
/// forwarded unchanged. Void, value, reference and move-only results are
|
|
/// preserved. The observer itself must not throw; an exception from it
|
|
/// propagates instead of the result.
|
|
template <typename Fn, typename Observer> class ObservedCallable
|
|
{
|
|
public:
|
|
|
|
template <typename F, typename O>
|
|
ObservedCallable(F&& fn, O&& observer)
|
|
: fn_(std::forward<F>(fn))
|
|
, observer_(std::forward<O>(observer))
|
|
{
|
|
}
|
|
|
|
template <typename... Args>
|
|
requires std::invocable<Fn&, Args...>
|
|
decltype(auto) operator()(Args&&... args)
|
|
{
|
|
using Result = std::invoke_result_t<Fn&, Args...>;
|
|
if constexpr (std::is_void_v<Result>) {
|
|
std::invoke(fn_, std::forward<Args>(args)...);
|
|
std::invoke(observer_);
|
|
return;
|
|
} else if constexpr (std::is_reference_v<Result>) {
|
|
Result result =
|
|
std::invoke(fn_, std::forward<Args>(args)...);
|
|
std::invoke(observer_);
|
|
return std::forward<Result>(result);
|
|
} else {
|
|
Result result =
|
|
std::invoke(fn_, std::forward<Args>(args)...);
|
|
std::invoke(observer_);
|
|
return result;
|
|
}
|
|
}
|
|
|
|
private:
|
|
|
|
Fn fn_;
|
|
Observer observer_;
|
|
};
|
|
|
|
template <typename Fn, typename Observer>
|
|
[[nodiscard]] auto observe_completion(Fn&& fn, Observer&& observer)
|
|
{
|
|
return ObservedCallable<std::decay_t<Fn>, std::decay_t<Observer>>(
|
|
std::forward<Fn>(fn), std::forward<Observer>(observer));
|
|
}
|
|
|
|
} // namespace Void::Testing
|