#include #include #include #include #include #include #include #include #include #include #include #include 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(offset)}; } void advance_clock(ManualClock& clock, std::chrono::nanoseconds delta) { Void::Testing::require(clock.advance(delta).has_value()); } std::optional code_of(const Void::Result::Result& res) { if (res.has_value()) { return std::nullopt; } return static_cast(res.error().code().value); } Void::Coro::Task schedule_into(Executor* exec, OperationOptions options, const Void::Time::MonotonicClock* clock, std::optional>* out) { *out = co_await schedule_on(*exec, std::move(options), *clock); } Void::Coro::Task schedule_host_into(Executor* exec, OperationOptions options, std::optional>* out) { *out = co_await schedule_on(*exec, std::move(options)); } Void::Coro::Task increment_task(int* counter) { ++(*counter); co_return; } struct SleepProbe { int resumes{0}; std::optional> result{}; }; Void::Coro::Task await_sleep(Void::Coro::Task> sleep, SleepProbe* probe) { auto res = co_await sleep; probe->result = std::move(res); ++probe->resumes; } void start(Void::Coro::Task& 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::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> 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> 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> 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> 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> 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> 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()); }