From 3953891511d127e0181c525d5214f0fda59c3cad Mon Sep 17 00:00:00 2001 From: Aleksey Selihov <1+faumaray@noreply@void.com> Date: Thu, 3 Dec 2026 07:02:24 +0000 Subject: [PATCH] feat(Testing): deterministic completion-order helpers (#15) --- include/Void/Testing/CompletionObserver.hpp | 140 ++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 include/Void/Testing/CompletionObserver.hpp diff --git a/include/Void/Testing/CompletionObserver.hpp b/include/Void/Testing/CompletionObserver.hpp new file mode 100644 index 0000000..6f0d7fa --- /dev/null +++ b/include/Void/Testing/CompletionObserver.hpp @@ -0,0 +1,140 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +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 class ObservedCallable +{ + public: + + template + ObservedCallable(F&& fn, O&& observer) + : fn_(std::forward(fn)) + , observer_(std::forward(observer)) + { + } + + template + requires std::invocable + decltype(auto) operator()(Args&&... args) + { + using Result = std::invoke_result_t; + if constexpr (std::is_void_v) { + std::invoke(fn_, std::forward(args)...); + std::invoke(observer_); + return; + } else if constexpr (std::is_reference_v) { + Result result = + std::invoke(fn_, std::forward(args)...); + std::invoke(observer_); + return std::forward(result); + } else { + Result result = + std::invoke(fn_, std::forward(args)...); + std::invoke(observer_); + return result; + } + } + + private: + + Fn fn_; + Observer observer_; +}; + +template +[[nodiscard]] auto observe_completion(Fn&& fn, Observer&& observer) +{ + return ObservedCallable, std::decay_t>( + std::forward(fn), std::forward(observer)); +} + +} // namespace Void::Testing