feat(async): add retry_async over TimerQueue and deadline-domain conformance (M-TIME-03 B1b)
- NEW Async/Retry.hpp: retry_async drives RetryState with the TimerQueue's injected clock, awaits backoff via sleep_until, resumes only after a clean sleep (any sleep error ends the retry), preserves success after stop, supports Task-returning and move-only callables - Tests: AsyncRetry (15), DeadlineDomains blocking-retry manual-clock case Review: W11-REVIEW-B1b READY (deviation accepted; doc fix applied by root) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3ce21be53a
commit
5c24f8e84f
5 files changed
+1555
-3
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -2,15 +2,21 @@
|
||||
#include <Void/Async/Operation.hpp>
|
||||
#include <Void/Async/TaskScope.hpp>
|
||||
#include <Void/Async/Timer.hpp>
|
||||
#include <Void/Functional/Retry.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
#include <Void/Time/Clock.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <stop_token>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -77,6 +83,51 @@ void start(Void::Coro::Task<void>& task)
|
||||
task.handle().resume();
|
||||
}
|
||||
|
||||
// retry_blocking's only notion of time: `now` reads the ManualClock and the
|
||||
// sleeper advances it by the requested wait (plus an optional overshoot).
|
||||
struct ManualNow {
|
||||
const ManualClock* clock{nullptr};
|
||||
|
||||
[[nodiscard]] Void::Retry::SteadyTimePoint operator()() const noexcept { return clock->now(); }
|
||||
};
|
||||
|
||||
struct ManualAdvanceSleep {
|
||||
ManualClock* clock{nullptr};
|
||||
std::vector<std::chrono::nanoseconds>* waits{nullptr};
|
||||
std::chrono::nanoseconds overshoot{0};
|
||||
|
||||
bool operator()(std::chrono::nanoseconds wait, std::stop_token) const
|
||||
{
|
||||
waits->push_back(wait);
|
||||
return clock->advance(wait + overshoot).has_value();
|
||||
}
|
||||
};
|
||||
|
||||
using ManualBlockingOptions = Void::Retry::
|
||||
BlockingRetryOptions<ManualNow, ManualAdvanceSleep, Void::Retry::RetryEveryError, std::mt19937_64>;
|
||||
|
||||
// Constant backoff of `delay`, no jitter, no time budget.
|
||||
Void::Retry::RetrySpec constant_retry_spec(std::chrono::nanoseconds delay, std::uint32_t attempts)
|
||||
{
|
||||
Void::Retry::RetrySpec spec;
|
||||
spec.max_attempts = attempts;
|
||||
spec.budget = Void::Retry::no_deadline;
|
||||
spec.backoff.initial = delay;
|
||||
spec.backoff.cap = delay;
|
||||
spec.backoff.multiplier = 1.0;
|
||||
spec.backoff.jitter = std::nullopt;
|
||||
return spec;
|
||||
}
|
||||
|
||||
// Fails every attempt with its 1-based index and records when it ran.
|
||||
auto failing_attempt(const ManualClock* clock, std::vector<TimePoint>* times)
|
||||
{
|
||||
return [clock, times]() -> std::expected<int, int> {
|
||||
times->push_back(clock->now());
|
||||
return std::unexpected{static_cast<int>(times->size())};
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test deadline_domain_schedule_on_and_scope_admission_injected_clock()
|
||||
@@ -242,3 +293,98 @@ Void::Testing::Test deadline_domain_timer_and_operation_deadline_same_manual_clo
|
||||
Void::Testing::require(later.result.has_value());
|
||||
Void::Testing::expect(later.result->has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test deadline_domain_blocking_retry_manual_clock()
|
||||
{
|
||||
namespace R = Void::Retry;
|
||||
using Ns = std::chrono::nanoseconds;
|
||||
// The host steady clock is far past the manual-domain deadline below, so
|
||||
// any host-clock comparison would stop the retry before its first attempt.
|
||||
Void::Testing::require(std::chrono::steady_clock::now().time_since_epoch() > 100ms);
|
||||
|
||||
// Admission deadline in the ManualClock domain (epoch 0): attempts at 0,
|
||||
// 10 and 20ms; the wake-up at 30ms would overshoot 25ms, so it stops.
|
||||
{
|
||||
ManualClock clock;
|
||||
const OperationOptions opts{.deadline = at(25ms)};
|
||||
Void::Testing::expect(is_deadline_expired(opts));
|
||||
Void::Testing::expect(!is_deadline_expired(opts, clock));
|
||||
|
||||
std::vector<TimePoint> attempts;
|
||||
std::vector<Ns> waits;
|
||||
const auto r = R::retry_blocking(constant_retry_spec(10ms, 10),
|
||||
failing_attempt(&clock, &attempts),
|
||||
ManualBlockingOptions{.now = ManualNow{&clock},
|
||||
.sleep = ManualAdvanceSleep{&clock, &waits},
|
||||
.classify = {},
|
||||
.urbg = std::mt19937_64{1},
|
||||
.stop = {},
|
||||
.deadline = opts.deadline});
|
||||
Void::Testing::require(!r.has_value());
|
||||
Void::Testing::expect_eq(static_cast<int>(r.error().reason),
|
||||
static_cast<int>(R::RetryStopReason::deadline_exceeded));
|
||||
Void::Testing::expect_eq(r.error().attempts, std::uint32_t{3});
|
||||
Void::Testing::require(r.error().last_error.has_value());
|
||||
Void::Testing::expect_eq(*r.error().last_error, 3);
|
||||
Void::Testing::require(attempts.size() == 3);
|
||||
Void::Testing::expect(attempts[0] == at(0ns));
|
||||
Void::Testing::expect(attempts[1] == at(10ms));
|
||||
Void::Testing::expect(attempts[2] == at(20ms));
|
||||
Void::Testing::require(waits.size() == 2);
|
||||
Void::Testing::expect_eq(waits[0], Ns{10ms});
|
||||
Void::Testing::expect_eq(waits[1], Ns{10ms});
|
||||
// Only the injected sleeper moved time.
|
||||
Void::Testing::expect(clock.now() == at(20ms));
|
||||
}
|
||||
|
||||
// The budget is measured from the start the manual clock reports, here
|
||||
// a century past epoch, far from any host uptime.
|
||||
{
|
||||
const TimePoint start = at(std::chrono::hours{24 * 365 * 100});
|
||||
ManualClock clock{start};
|
||||
auto spec = constant_retry_spec(10ms, 10);
|
||||
spec.budget = Ns{25ms};
|
||||
std::vector<TimePoint> attempts;
|
||||
std::vector<Ns> waits;
|
||||
const auto r = R::retry_blocking(spec,
|
||||
failing_attempt(&clock, &attempts),
|
||||
ManualBlockingOptions{.now = ManualNow{&clock},
|
||||
.sleep = ManualAdvanceSleep{&clock, &waits},
|
||||
.classify = {},
|
||||
.urbg = std::mt19937_64{1},
|
||||
.stop = {},
|
||||
.deadline = std::nullopt});
|
||||
Void::Testing::require(!r.has_value());
|
||||
Void::Testing::expect_eq(static_cast<int>(r.error().reason),
|
||||
static_cast<int>(R::RetryStopReason::deadline_exceeded));
|
||||
Void::Testing::expect_eq(r.error().attempts, std::uint32_t{3});
|
||||
Void::Testing::require(attempts.size() == 3);
|
||||
Void::Testing::expect(attempts[0] == start);
|
||||
Void::Testing::expect(attempts[2] == start + std::chrono::duration_cast<TimePoint::duration>(20ms));
|
||||
Void::Testing::expect_eq(waits.size(), std::size_t{2});
|
||||
}
|
||||
|
||||
// Advancing manual time past the deadline during one sleep expires the
|
||||
// operation exactly once: resume() observes it and no attempt follows.
|
||||
{
|
||||
ManualClock clock;
|
||||
std::vector<TimePoint> attempts;
|
||||
std::vector<Ns> waits;
|
||||
const auto r = R::retry_blocking(constant_retry_spec(10ms, 10),
|
||||
failing_attempt(&clock, &attempts),
|
||||
ManualBlockingOptions{.now = ManualNow{&clock},
|
||||
.sleep = ManualAdvanceSleep{&clock, &waits, 30ms},
|
||||
.classify = {},
|
||||
.urbg = std::mt19937_64{1},
|
||||
.stop = {},
|
||||
.deadline = at(25ms)});
|
||||
Void::Testing::require(!r.has_value());
|
||||
Void::Testing::expect_eq(static_cast<int>(r.error().reason),
|
||||
static_cast<int>(R::RetryStopReason::deadline_exceeded));
|
||||
Void::Testing::expect_eq(r.error().attempts, std::uint32_t{1});
|
||||
Void::Testing::expect_eq(attempts.size(), std::size_t{1});
|
||||
Void::Testing::require(waits.size() == 1);
|
||||
Void::Testing::expect_eq(waits[0], Ns{10ms});
|
||||
Void::Testing::expect(clock.now() == at(40ms));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user