Files
Library/tests/standalone/Async/DeadlineDomains.cpp
T
faumarayandClaude Opus 5.5 99a1100bb1 feat(async,time): add injected-clock TimerQueue and clock saturation helpers (M-TIME-03 B1)
- NEW Async/Timer.hpp: TimerQueue (stable (deadline, seq) min-heap, bounded
  tombstones, explicit poll_expired, callbacks outside the lock, close/destroy
  deliver TimerEvent::closed), shared-state TimerHandle safe after queue
  destruction, sleep_until/sleep_for settling exactly once and resuming via
  post_completion; documented sleep-task lifetime and cancel-under-lock rules
- Time/Clock.hpp: saturating_add, deadline_after
- Tests: TimerQueue (15), DeadlineDomains (2), ClockSaturation (1); race loops
  count failures to stay under the runner's 1,000-assertion retention (X-QA-02)

Review: W11-REVIEW-B1 (CHANGES_REQUIRED F1-F3 fixed by root: test counters,
header lifetime/cancel docs)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 23:36:37 +03:00

245 lines
9.2 KiB
C++

#include <Void/Async/Executor.hpp>
#include <Void/Async/Operation.hpp>
#include <Void/Async/TaskScope.hpp>
#include <Void/Async/Timer.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <Void/Time/Clock.hpp>
#include <chrono>
#include <cstddef>
#include <memory>
#include <optional>
#include <utility>
namespace
{
using namespace Void::Async;
using namespace std::chrono_literals;
using TimePoint = Void::Time::MonotonicClock::time_point;
using Void::Time::ManualClock;
TimePoint at(std::chrono::nanoseconds offset)
{
return TimePoint{std::chrono::duration_cast<TimePoint::duration>(offset)};
}
void advance_clock(ManualClock& clock, std::chrono::nanoseconds delta)
{
Void::Testing::require(clock.advance(delta).has_value());
}
std::optional<AsyncErrorCode> code_of(const Void::Result::Result<void>& res)
{
if (res.has_value()) {
return std::nullopt;
}
return static_cast<AsyncErrorCode>(res.error().code().value);
}
Void::Coro::Task<void> schedule_into(Executor* exec,
OperationOptions options,
const Void::Time::MonotonicClock* clock,
std::optional<Void::Result::Result<void>>* out)
{
*out = co_await schedule_on(*exec, std::move(options), *clock);
}
Void::Coro::Task<void> schedule_host_into(Executor* exec,
OperationOptions options,
std::optional<Void::Result::Result<void>>* out)
{
*out = co_await schedule_on(*exec, std::move(options));
}
Void::Coro::Task<void> increment_task(int* counter)
{
++(*counter);
co_return;
}
struct SleepProbe {
int resumes{0};
std::optional<Void::Result::Result<void>> result{};
};
Void::Coro::Task<void> await_sleep(Void::Coro::Task<Void::Result::Result<void>> sleep,
SleepProbe* probe)
{
auto res = co_await sleep;
probe->result = std::move(res);
++probe->resumes;
}
void start(Void::Coro::Task<void>& task)
{
task.handle().resume();
}
} // namespace
Void::Testing::Test deadline_domain_schedule_on_and_scope_admission_injected_clock()
{
// Precondition: the host steady clock is far past the manual-domain
// deadline used below, so host comparisons treat it as expired.
Void::Testing::require(std::chrono::steady_clock::now().time_since_epoch() > 100ms);
ManualExecutor exec;
ManualClock clock; // epoch 0
const OperationOptions opts{.deadline = at(5ms)};
// One deadline value, two domains.
Void::Testing::expect(is_deadline_expired(opts));
Void::Testing::expect(!is_deadline_expired(opts, clock));
auto op = OperationState<void>::create(opts);
Void::Testing::expect(op->is_deadline_expired());
Void::Testing::expect(!op->is_deadline_expired(clock));
// schedule_on in the manual domain is admitted and runs.
std::optional<Void::Result::Result<void>> admitted;
auto t1 = schedule_into(&exec, opts, &clock, &admitted);
start(t1);
Void::Testing::expect(!admitted.has_value());
Void::Testing::expect_eq(exec.drain(), std::size_t{1});
Void::Testing::require(admitted.has_value());
Void::Testing::expect(admitted->has_value());
// The default (host) clock rejects the same manual-domain deadline.
std::optional<Void::Result::Result<void>> host_rejected;
auto t2 = schedule_host_into(&exec, opts, &host_rejected);
start(t2);
Void::Testing::require(host_rejected.has_value());
Void::Testing::expect(code_of(*host_rejected) == AsyncErrorCode::timeout);
Void::Testing::expect(exec.empty());
// Expiry in the manual domain happens only when the manual clock advances.
advance_clock(clock, 4ms);
std::optional<Void::Result::Result<void>> still_ok;
auto t3 = schedule_into(&exec, opts, &clock, &still_ok);
start(t3);
Void::Testing::expect_eq(exec.drain(), std::size_t{1});
Void::Testing::require(still_ok.has_value());
Void::Testing::expect(still_ok->has_value());
advance_clock(clock, 1ms);
std::optional<Void::Result::Result<void>> expired;
auto t4 = schedule_into(&exec, opts, &clock, &expired);
start(t4);
Void::Testing::require(expired.has_value());
Void::Testing::expect(code_of(*expired) == AsyncErrorCode::timeout);
Void::Testing::expect(exec.empty());
// Expiry observed between admission and execution is reported on resume.
ManualClock hop_clock;
std::optional<Void::Result::Result<void>> hop;
auto t5 = schedule_into(&exec, opts, &hop_clock, &hop);
start(t5);
Void::Testing::expect(!hop.has_value());
advance_clock(hop_clock, 5ms);
Void::Testing::expect_eq(exec.drain(), std::size_t{1});
Void::Testing::require(hop.has_value());
Void::Testing::expect(code_of(*hop) == AsyncErrorCode::timeout);
// TaskScope admission uses the scope's injected clock.
ManualClock scope_clock;
int ran = 0;
{
TaskScope scope{exec, TaskScopeOptions{}, scope_clock};
auto first = scope.spawn(increment_task(&ran), opts);
Void::Testing::expect(first.has_value());
advance_clock(scope_clock, 5ms);
auto late = scope.spawn(increment_task(&ran), opts);
Void::Testing::require(!late.has_value());
Void::Testing::expect(late.error() == AdmissionError::deadline_expired);
Void::Testing::expect(scope.join_pumping(exec).has_value());
}
{
TaskScope host_scope{exec};
auto host_late = host_scope.spawn(increment_task(&ran), opts);
Void::Testing::require(!host_late.has_value());
Void::Testing::expect(host_late.error() == AdmissionError::deadline_expired);
Void::Testing::expect(host_scope.join_pumping(exec).has_value());
}
Void::Testing::expect_eq(ran, 1);
}
Void::Testing::Test deadline_domain_timer_and_operation_deadline_same_manual_clock()
{
Void::Testing::require(std::chrono::steady_clock::now().time_since_epoch() > 100ms);
ManualExecutor exec;
ManualClock clock; // epoch 0
auto created = TimerQueue::create(exec, clock);
Void::Testing::require(created.has_value());
auto q = std::move(*created);
Void::Testing::expect(&q->clock() == &clock);
Void::Testing::expect(&q->executor() == &exec);
// An OperationOptions deadline in the ManualClock domain bounds the sleep.
const OperationOptions opts{.deadline = at(3ms)};
SleepProbe probe;
auto task = await_sleep(q->sleep_until(at(8ms), opts), &probe);
start(task);
Void::Testing::expect(!task.done()); // the host clock would call it expired
Void::Testing::require(q->next_deadline().has_value());
Void::Testing::expect(*q->next_deadline() == at(3ms));
Void::Testing::expect_eq(q->wait_duration(1s), std::chrono::nanoseconds{3ms});
Void::Testing::expect_eq(q->poll_expired(), std::size_t{0});
Void::Testing::expect(exec.empty());
advance_clock(clock, 2ms);
Void::Testing::expect_eq(q->poll_expired(), std::size_t{0});
Void::Testing::expect(exec.empty());
Void::Testing::expect_eq(probe.resumes, 0);
// Advance + explicit poll expires it once; the resume goes through the
// completion executor.
advance_clock(clock, 1ms);
Void::Testing::expect_eq(q->poll_expired(), std::size_t{1});
Void::Testing::expect_eq(probe.resumes, 0);
Void::Testing::expect_eq(exec.drain(), std::size_t{1});
Void::Testing::expect_eq(probe.resumes, 1);
Void::Testing::require(probe.result.has_value());
Void::Testing::expect(code_of(*probe.result) == AsyncErrorCode::timeout);
Void::Testing::expect_eq(q->active_timers(), std::size_t{0});
// Passing `when` later fires nothing and never produces a second result.
advance_clock(clock, 10ms);
Void::Testing::expect_eq(q->poll_expired(), std::size_t{0});
Void::Testing::expect_eq(exec.drain(), std::size_t{0});
Void::Testing::expect_eq(probe.resumes, 1);
// The same deadline is expired for schedule_on in the same manual domain.
std::optional<Void::Result::Result<void>> scheduled;
auto sched = schedule_into(&exec, opts, &q->clock(), &scheduled);
start(sched);
Void::Testing::require(scheduled.has_value());
Void::Testing::expect(code_of(*scheduled) == AsyncErrorCode::timeout);
// A deadline equal to `when` is not earlier: the sleep completes normally.
SleepProbe equal;
const auto target = Void::Time::deadline_after(clock, 5ms);
auto equal_task =
await_sleep(q->sleep_until(target, OperationOptions{.deadline = target}), &equal);
start(equal_task);
advance_clock(clock, 5ms);
Void::Testing::expect_eq(q->poll_expired(), std::size_t{1});
Void::Testing::expect_eq(exec.drain(), std::size_t{1});
Void::Testing::require(equal.result.has_value());
Void::Testing::expect(equal.result->has_value());
// A deadline later than `when` does not interfere either.
SleepProbe later;
auto later_task = await_sleep(
q->sleep_for(1ms, OperationOptions{.deadline = Void::Time::deadline_after(clock, 2ms)}),
&later);
start(later_task);
advance_clock(clock, 2ms);
Void::Testing::expect_eq(q->poll_expired(), std::size_t{1});
Void::Testing::expect_eq(exec.drain(), std::size_t{1});
Void::Testing::require(later.result.has_value());
Void::Testing::expect(later.result->has_value());
}