- NEW std-only Functional/RetryState.hpp: RetrySpec/BackoffSpec validation, begin/next/resume/succeed decisions, saturating backoff, Full/Equal jitter, explicit no_deadline, random_device-seeded default engine, final stop decisions - Retry.hpp: relocate JitterMode, add SteadyNow/ThreadSleep/BlockingRetryOptions and retry_blocking with injected now/sleep; saturating wait computation - 1.x retry/retry_while/retry_or_throw unchanged (zero-attempt defect pinned) - Tests: RetryState (13), RetryBlocking (10) Review: W11-REVIEW-A1 READY (P3 fixes 1, 2, 5 applied by root) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1202 lines
39 KiB
C++
1202 lines
39 KiB
C++
#include <Void/Functional/RetryState.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cmath>
|
|
#include <concepts>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
#include <optional>
|
|
#include <random>
|
|
#include <set>
|
|
#include <source_location>
|
|
#include <string_view>
|
|
#include <type_traits>
|
|
#include <vector>
|
|
|
|
// Pure decision-engine tests: no clock is read and nothing sleeps. Every
|
|
// time value is passed in explicitly.
|
|
|
|
namespace
|
|
{
|
|
using namespace std::chrono_literals;
|
|
namespace R = Void::Retry;
|
|
using Void::Testing::expect;
|
|
using Void::Testing::expect_eq;
|
|
|
|
using R::SteadyTimePoint;
|
|
using Engine = std::mt19937_64;
|
|
using Loc = std::source_location;
|
|
|
|
constexpr SteadyTimePoint t0{std::chrono::seconds{1000}};
|
|
|
|
// A deterministic engine that cannot be seeded from one integer, so
|
|
// detail::seeded_engine<> has to fall back to default construction.
|
|
struct StepEngine {
|
|
using result_type = std::uint64_t;
|
|
StepEngine() = default;
|
|
static constexpr result_type min() noexcept { return 0; }
|
|
static constexpr result_type max() noexcept
|
|
{
|
|
return std::numeric_limits<result_type>::max();
|
|
}
|
|
result_type operator()() noexcept
|
|
{
|
|
state += 0x9E3779B97F4A7C15ULL;
|
|
return state;
|
|
}
|
|
result_type state{0};
|
|
};
|
|
// An engine seeded from a type narrower than random_device::result_type:
|
|
// seeded_engine<> must construct it with parentheses, because list
|
|
// initialisation would reject the narrowing conversion.
|
|
struct NarrowSeedEngine {
|
|
using result_type = std::uint64_t;
|
|
explicit NarrowSeedEngine(std::uint16_t seed) : state{seed} {}
|
|
static constexpr result_type min() noexcept { return 0; }
|
|
static constexpr result_type max() noexcept
|
|
{
|
|
return std::numeric_limits<result_type>::max();
|
|
}
|
|
result_type operator()() noexcept
|
|
{
|
|
state += 0x9E3779B97F4A7C15ULL;
|
|
return state;
|
|
}
|
|
result_type state;
|
|
};
|
|
static_assert(std::uniform_random_bit_generator<NarrowSeedEngine>);
|
|
static_assert(std::is_constructible_v<NarrowSeedEngine,
|
|
std::random_device::result_type>);
|
|
static_assert(std::uniform_random_bit_generator<StepEngine>);
|
|
static_assert(
|
|
!std::is_constructible_v<StepEngine, std::random_device::result_type>);
|
|
|
|
void expect_ns(std::chrono::nanoseconds actual,
|
|
std::chrono::nanoseconds expected, Loc loc = Loc::current())
|
|
{
|
|
expect_eq(actual.count(), expected.count(), loc);
|
|
}
|
|
|
|
void expect_tp(SteadyTimePoint actual, SteadyTimePoint expected,
|
|
Loc loc = Loc::current())
|
|
{
|
|
expect_eq(actual.time_since_epoch().count(),
|
|
expected.time_since_epoch().count(), loc);
|
|
}
|
|
|
|
void expect_attempt(const R::RetryDecision& d, std::uint32_t attempts,
|
|
Loc loc = Loc::current())
|
|
{
|
|
expect_eq(static_cast<int>(d.action),
|
|
static_cast<int>(R::RetryAction::attempt), loc);
|
|
expect_eq(static_cast<int>(d.reason),
|
|
static_cast<int>(R::RetryStopReason::none), loc);
|
|
expect_eq(d.attempts_started, attempts, loc);
|
|
}
|
|
|
|
void expect_wait(const R::RetryDecision& d, SteadyTimePoint at,
|
|
std::uint32_t attempts, Loc loc = Loc::current())
|
|
{
|
|
expect_eq(static_cast<int>(d.action),
|
|
static_cast<int>(R::RetryAction::wait_until), loc);
|
|
expect_eq(static_cast<int>(d.reason),
|
|
static_cast<int>(R::RetryStopReason::none), loc);
|
|
expect_tp(d.at, at, loc);
|
|
expect_eq(d.attempts_started, attempts, loc);
|
|
}
|
|
|
|
void expect_stop(const R::RetryDecision& d, R::RetryStopReason reason,
|
|
std::uint32_t attempts, Loc loc = Loc::current())
|
|
{
|
|
expect_eq(static_cast<int>(d.action),
|
|
static_cast<int>(R::RetryAction::stop), loc);
|
|
expect_eq(static_cast<int>(d.reason), static_cast<int>(reason), loc);
|
|
expect_eq(d.attempts_started, attempts, loc);
|
|
}
|
|
|
|
void expect_invalid(const R::RetrySpec& spec, R::RetryConfigError error,
|
|
Loc loc = Loc::current())
|
|
{
|
|
const auto v = R::validate(spec);
|
|
expect(!v.has_value(), loc);
|
|
if (!v.has_value()) {
|
|
expect_eq(static_cast<int>(v.error()), static_cast<int>(error),
|
|
loc);
|
|
}
|
|
const auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(!created.has_value(), loc);
|
|
if (!created.has_value()) {
|
|
expect_eq(static_cast<int>(created.error()),
|
|
static_cast<int>(error), loc);
|
|
}
|
|
}
|
|
|
|
void expect_valid(const R::RetrySpec& spec, Loc loc = Loc::current())
|
|
{
|
|
expect(R::validate(spec).has_value(), loc);
|
|
expect(R::RetryState<Engine>::create(spec, t0, Engine{42}).has_value(),
|
|
loc);
|
|
}
|
|
|
|
// Bulk checks are folded into one assertion each: the test runner keeps
|
|
// only the first 1000 assertions of a test (kMaxRetainedAssertionsPerTest)
|
|
// and decides pass/fail from those, so a failure past that point would be
|
|
// lost. Counting violations keeps every check visible.
|
|
void expect_all_within(const std::vector<std::chrono::nanoseconds>& values,
|
|
std::chrono::nanoseconds lo,
|
|
std::chrono::nanoseconds hi,
|
|
Loc loc = Loc::current())
|
|
{
|
|
std::size_t outside = 0;
|
|
for (const auto v : values) {
|
|
if (v < lo || v > hi) {
|
|
++outside;
|
|
}
|
|
}
|
|
expect_eq(outside, std::size_t{0}, loc);
|
|
}
|
|
|
|
// Drives begin / next / resume with every attempt failing retryably and
|
|
// the clock jumping straight to each wake-up time. Returns the wait
|
|
// lengths (`at - now`) in order; stops after `retries` waits or at the
|
|
// first non-wait decision.
|
|
template <class G>
|
|
[[nodiscard]] std::vector<std::chrono::nanoseconds>
|
|
collect_waits(R::RetryState<G>& state, SteadyTimePoint now,
|
|
std::size_t retries)
|
|
{
|
|
std::vector<std::chrono::nanoseconds> waits;
|
|
R::RetryDecision d = state.begin(now);
|
|
for (std::size_t i = 0;
|
|
i < retries && d.action == R::RetryAction::attempt; ++i) {
|
|
const R::RetryDecision n =
|
|
state.next(now, R::AttemptOutcome::retryable_failure);
|
|
if (n.action != R::RetryAction::wait_until) {
|
|
break;
|
|
}
|
|
waits.push_back(n.at - now);
|
|
now = n.at;
|
|
d = state.resume(now);
|
|
}
|
|
return waits;
|
|
}
|
|
|
|
[[nodiscard]] R::RetrySpec constant_delay_spec(
|
|
std::chrono::nanoseconds delay,
|
|
std::optional<R::JitterMode> jitter, std::uint32_t attempts = 1000)
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = attempts;
|
|
spec.budget = R::no_deadline;
|
|
spec.backoff.initial = delay;
|
|
spec.backoff.cap = delay;
|
|
spec.backoff.multiplier = 1.0;
|
|
spec.backoff.jitter = jitter;
|
|
return spec;
|
|
}
|
|
} // namespace
|
|
|
|
// ── validation ────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_state_rejects_invalid_specs()
|
|
{
|
|
using E = R::RetryConfigError;
|
|
|
|
// zero_attempts
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 0;
|
|
expect_invalid(spec, E::zero_attempts);
|
|
}
|
|
|
|
// negative_duration: initial, cap and budget are each checked
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.backoff.initial = -1ns;
|
|
expect_invalid(spec, E::negative_duration);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.backoff.cap = -1ns;
|
|
expect_invalid(spec, E::negative_duration);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.budget = -1ns;
|
|
expect_invalid(spec, E::negative_duration);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.budget = std::chrono::nanoseconds::min();
|
|
expect_invalid(spec, E::negative_duration);
|
|
}
|
|
|
|
// zero_budget: 0 is rejected rather than meaning "unlimited"
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.budget = 0ns;
|
|
expect_invalid(spec, E::zero_budget);
|
|
}
|
|
|
|
// nonfinite_multiplier
|
|
for (const double m : {std::numeric_limits<double>::quiet_NaN(),
|
|
std::numeric_limits<double>::infinity(),
|
|
-std::numeric_limits<double>::infinity()}) {
|
|
R::RetrySpec spec;
|
|
spec.backoff.multiplier = m;
|
|
expect_invalid(spec, E::nonfinite_multiplier);
|
|
}
|
|
|
|
// multiplier_below_one
|
|
for (const double m : {0.5, 0.0, -2.0, std::nextafter(1.0, 0.0)}) {
|
|
R::RetrySpec spec;
|
|
spec.backoff.multiplier = m;
|
|
expect_invalid(spec, E::multiplier_below_one);
|
|
}
|
|
|
|
// cap_below_initial
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.backoff.initial = 2s;
|
|
spec.backoff.cap = 1s;
|
|
expect_invalid(spec, E::cap_below_initial);
|
|
}
|
|
|
|
// Precedence: the first failing check in the documented order wins.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 0;
|
|
spec.budget = -1ns;
|
|
spec.backoff.multiplier = std::numeric_limits<double>::quiet_NaN();
|
|
expect_invalid(spec, E::zero_attempts);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.backoff.cap = -1ns; // also below initial
|
|
spec.backoff.multiplier = std::numeric_limits<double>::quiet_NaN();
|
|
expect_invalid(spec, E::negative_duration);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.budget = 0ns;
|
|
spec.backoff.multiplier = 0.5;
|
|
expect_invalid(spec, E::zero_budget);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.backoff.multiplier = std::numeric_limits<double>::quiet_NaN();
|
|
spec.backoff.initial = 2s;
|
|
spec.backoff.cap = 1s;
|
|
expect_invalid(spec, E::nonfinite_multiplier);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.backoff.multiplier = 0.5;
|
|
spec.backoff.initial = 2s;
|
|
spec.backoff.cap = 1s;
|
|
expect_invalid(spec, E::multiplier_below_one);
|
|
}
|
|
|
|
// Boundary values that are valid.
|
|
expect_valid(R::RetrySpec{});
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 1;
|
|
expect_valid(spec);
|
|
spec.max_attempts = std::numeric_limits<std::uint32_t>::max();
|
|
expect_valid(spec);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.backoff.multiplier = 1.0;
|
|
spec.backoff.cap = spec.backoff.initial;
|
|
expect_valid(spec);
|
|
}
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.budget = R::no_deadline;
|
|
expect_valid(spec);
|
|
spec.budget = 1ns;
|
|
expect_valid(spec);
|
|
}
|
|
{
|
|
R::RetrySpec spec; // zero delays are allowed
|
|
spec.backoff.initial = 0ns;
|
|
spec.backoff.cap = 0ns;
|
|
expect_valid(spec);
|
|
}
|
|
}
|
|
|
|
// ── exact attempt counts ──────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_state_exact_three_attempts_default()
|
|
{
|
|
const R::RetrySpec spec{}; // 3 attempts, 30s budget, 100ms x2 up to 30s
|
|
expect_eq(spec.max_attempts, std::uint32_t{3});
|
|
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_eq(state.attempts_started(), std::uint32_t{0});
|
|
|
|
std::uint32_t attempts = 0;
|
|
SteadyTimePoint now = t0;
|
|
|
|
R::RetryDecision d = state.begin(now);
|
|
expect_attempt(d, 1);
|
|
attempts += (d.action == R::RetryAction::attempt) ? 1U : 0U;
|
|
expect_eq(state.attempts_started(), std::uint32_t{1});
|
|
|
|
// retry index 0 -> 100ms
|
|
d = state.next(now, R::AttemptOutcome::retryable_failure);
|
|
expect_wait(d, now + 100ms, 1);
|
|
now = d.at;
|
|
d = state.resume(now);
|
|
expect_attempt(d, 2);
|
|
attempts += (d.action == R::RetryAction::attempt) ? 1U : 0U;
|
|
|
|
// retry index 1 -> 200ms
|
|
d = state.next(now, R::AttemptOutcome::retryable_failure);
|
|
expect_wait(d, now + 200ms, 2);
|
|
now = d.at;
|
|
d = state.resume(now);
|
|
expect_attempt(d, 3);
|
|
attempts += (d.action == R::RetryAction::attempt) ? 1U : 0U;
|
|
|
|
// the third failure leaves no attempt to retry
|
|
d = state.next(now, R::AttemptOutcome::retryable_failure);
|
|
expect_stop(d, R::RetryStopReason::attempts_exhausted, 3);
|
|
|
|
expect_eq(attempts, std::uint32_t{3});
|
|
expect_eq(state.attempts_started(), std::uint32_t{3});
|
|
expect_tp(now, t0 + 300ms);
|
|
|
|
// Asking again keeps answering "exhausted" and never starts an attempt.
|
|
d = state.next(now, R::AttemptOutcome::retryable_failure);
|
|
expect_stop(d, R::RetryStopReason::attempts_exhausted, 3);
|
|
expect_eq(state.attempts_started(), std::uint32_t{3});
|
|
}
|
|
|
|
Void::Testing::Test retry_state_attempt_counter_stays_bounded()
|
|
{
|
|
// A single allowed attempt: next() stops, and a misused resume() cannot
|
|
// push the counter past max_attempts.
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 1;
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::retryable_failure),
|
|
R::RetryStopReason::attempts_exhausted, 1);
|
|
expect_stop(state.resume(t0), R::RetryStopReason::attempts_exhausted, 1);
|
|
expect_eq(state.attempts_started(), std::uint32_t{1});
|
|
|
|
// UINT32_MAX attempts: counters and the delay index do not overflow.
|
|
R::RetrySpec big;
|
|
big.max_attempts = std::numeric_limits<std::uint32_t>::max();
|
|
big.budget = R::no_deadline;
|
|
auto big_created = R::RetryState<Engine>::create(big, t0, Engine{42});
|
|
expect(big_created.has_value());
|
|
auto& big_state = *big_created;
|
|
SteadyTimePoint now = t0;
|
|
expect_attempt(big_state.begin(now), 1);
|
|
for (std::uint32_t i = 1; i <= 40; ++i) {
|
|
const auto n =
|
|
big_state.next(now, R::AttemptOutcome::retryable_failure);
|
|
expect_eq(static_cast<int>(n.action),
|
|
static_cast<int>(R::RetryAction::wait_until));
|
|
expect_eq(n.attempts_started, i);
|
|
// no jitter: wait is the unjittered, saturated delay
|
|
expect_ns(n.at - now, R::backoff_delay(big.backoff, i - 1));
|
|
now = n.at;
|
|
expect_attempt(big_state.resume(now), i + 1);
|
|
}
|
|
expect_eq(big_state.attempts_started(), std::uint32_t{41});
|
|
}
|
|
|
|
// ── deadlines ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_state_deadline_at_admission_prevents_first_attempt()
|
|
{
|
|
const R::RetrySpec spec{}; // 30s budget
|
|
|
|
// Admission deadline equal to the start time: not even one attempt.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42}, t0);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect(state.deadline().has_value());
|
|
if (state.deadline().has_value()) {
|
|
expect_tp(*state.deadline(), t0);
|
|
}
|
|
const auto d = state.begin(t0);
|
|
expect_stop(d, R::RetryStopReason::deadline_exceeded, 0);
|
|
expect_eq(state.attempts_started(), std::uint32_t{0});
|
|
}
|
|
|
|
// Admission deadline already in the past.
|
|
{
|
|
auto created =
|
|
R::RetryState<Engine>::create(spec, t0, Engine{42}, t0 - 1s);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect(state.deadline().has_value());
|
|
if (state.deadline().has_value()) {
|
|
expect_tp(*state.deadline(), t0 - 1s);
|
|
}
|
|
expect_stop(state.begin(t0), R::RetryStopReason::deadline_exceeded, 0);
|
|
expect_eq(state.attempts_started(), std::uint32_t{0});
|
|
}
|
|
|
|
// now == deadline is exceeded; one nanosecond earlier is still allowed.
|
|
{
|
|
auto created =
|
|
R::RetryState<Engine>::create(spec, t0, Engine{42}, t0 + 1ms);
|
|
expect(created.has_value());
|
|
expect_attempt(created->begin(t0 + 1ms - 1ns), 1);
|
|
}
|
|
{
|
|
auto created =
|
|
R::RetryState<Engine>::create(spec, t0, Engine{42}, t0 + 1ms);
|
|
expect(created.has_value());
|
|
expect_stop(created->begin(t0 + 1ms),
|
|
R::RetryStopReason::deadline_exceeded, 0);
|
|
}
|
|
|
|
// The effective deadline is the earlier of start + budget and admission.
|
|
{
|
|
auto later = R::RetryState<Engine>::create(spec, t0, Engine{42}, t0 + 1h);
|
|
expect(later.has_value());
|
|
expect(later->deadline().has_value());
|
|
if (later->deadline().has_value()) {
|
|
expect_tp(*later->deadline(), t0 + 30s);
|
|
}
|
|
auto none = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(none.has_value());
|
|
expect(none->deadline().has_value());
|
|
if (none->deadline().has_value()) {
|
|
expect_tp(*none->deadline(), t0 + 30s);
|
|
}
|
|
}
|
|
|
|
// start + budget saturates instead of overflowing.
|
|
{
|
|
R::RetrySpec huge;
|
|
huge.budget = std::chrono::nanoseconds::max();
|
|
auto created = R::RetryState<Engine>::create(huge, t0 + 1h, Engine{42});
|
|
expect(created.has_value());
|
|
expect(created->deadline().has_value());
|
|
if (created->deadline().has_value()) {
|
|
expect_tp(*created->deadline(), SteadyTimePoint::max());
|
|
}
|
|
}
|
|
|
|
// The budget deadline itself also rejects a late begin().
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
expect_stop(created->begin(t0 + 30s),
|
|
R::RetryStopReason::deadline_exceeded, 0);
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test retry_state_backoff_overshooting_deadline_stops()
|
|
{
|
|
R::RetrySpec spec; // 100ms, 200ms, 400ms, ...
|
|
spec.max_attempts = 10;
|
|
|
|
auto run = [&](std::chrono::nanoseconds budget) {
|
|
spec.budget = budget;
|
|
return R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
};
|
|
|
|
// Budget 250ms: the 200ms wait after attempt 2 would end at 300ms.
|
|
{
|
|
auto created = run(250ms);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
auto d = state.next(t0, R::AttemptOutcome::retryable_failure);
|
|
expect_wait(d, t0 + 100ms, 1);
|
|
expect_attempt(state.resume(t0 + 100ms), 2);
|
|
d = state.next(t0 + 100ms, R::AttemptOutcome::retryable_failure);
|
|
expect_stop(d, R::RetryStopReason::deadline_exceeded, 2);
|
|
}
|
|
|
|
// Budget 300ms: the wake-up lands exactly on the deadline, which counts
|
|
// as exceeded.
|
|
{
|
|
auto created = run(300ms);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_wait(state.next(t0, R::AttemptOutcome::retryable_failure),
|
|
t0 + 100ms, 1);
|
|
expect_attempt(state.resume(t0 + 100ms), 2);
|
|
expect_stop(state.next(t0 + 100ms,
|
|
R::AttemptOutcome::retryable_failure),
|
|
R::RetryStopReason::deadline_exceeded, 2);
|
|
}
|
|
|
|
// Budget 300ms + 1ns: the same wake-up is now one nanosecond early.
|
|
{
|
|
auto created = run(300ms + 1ns);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_wait(state.next(t0, R::AttemptOutcome::retryable_failure),
|
|
t0 + 100ms, 1);
|
|
expect_attempt(state.resume(t0 + 100ms), 2);
|
|
expect_wait(state.next(t0 + 100ms,
|
|
R::AttemptOutcome::retryable_failure),
|
|
t0 + 300ms, 2);
|
|
}
|
|
|
|
// A long attempt that already consumed the budget: the next wait cannot
|
|
// fit, even though attempts remain.
|
|
{
|
|
auto created = run(1s);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0 + 2s, R::AttemptOutcome::retryable_failure),
|
|
R::RetryStopReason::deadline_exceeded, 1);
|
|
}
|
|
|
|
// resume() re-checks the deadline at the time the wait actually ended.
|
|
{
|
|
auto created = run(1s);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_wait(state.next(t0, R::AttemptOutcome::retryable_failure),
|
|
t0 + 100ms, 1);
|
|
expect_stop(state.resume(t0 + 1s),
|
|
R::RetryStopReason::deadline_exceeded, 1);
|
|
expect_eq(state.attempts_started(), std::uint32_t{1});
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test retry_state_no_deadline_explicit_marker()
|
|
{
|
|
expect(R::no_deadline == R::NoDeadline{});
|
|
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 5;
|
|
spec.budget = R::no_deadline;
|
|
expect_valid(spec);
|
|
|
|
// With no admission deadline there is no deadline at all, however far in
|
|
// the future the clock is.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect(!state.deadline().has_value());
|
|
|
|
SteadyTimePoint now = SteadyTimePoint::max() - 1h;
|
|
expect_attempt(state.begin(now), 1);
|
|
const std::chrono::nanoseconds expected[] = {100ms, 200ms, 400ms,
|
|
800ms};
|
|
std::uint32_t attempts = 1;
|
|
for (const auto delay : expected) {
|
|
const auto d =
|
|
state.next(now, R::AttemptOutcome::retryable_failure);
|
|
expect_wait(d, now + delay, attempts);
|
|
now = d.at;
|
|
expect_attempt(state.resume(now), ++attempts);
|
|
}
|
|
expect_eq(attempts, std::uint32_t{5});
|
|
expect_stop(state.next(now, R::AttemptOutcome::retryable_failure),
|
|
R::RetryStopReason::attempts_exhausted, 5);
|
|
}
|
|
|
|
// An admission deadline still applies on its own.
|
|
{
|
|
auto created =
|
|
R::RetryState<Engine>::create(spec, t0, Engine{42}, t0 + 150ms);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect(state.deadline().has_value());
|
|
if (state.deadline().has_value()) {
|
|
expect_tp(*state.deadline(), t0 + 150ms);
|
|
}
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_wait(state.next(t0, R::AttemptOutcome::retryable_failure),
|
|
t0 + 100ms, 1);
|
|
expect_attempt(state.resume(t0 + 100ms), 2);
|
|
expect_stop(state.next(t0 + 100ms,
|
|
R::AttemptOutcome::retryable_failure),
|
|
R::RetryStopReason::deadline_exceeded, 2);
|
|
}
|
|
}
|
|
|
|
// ── backoff saturation ────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_state_backoff_saturates_without_overflow()
|
|
{
|
|
const R::BackoffSpec def{}; // 100ms, cap 30s, x2
|
|
expect_ns(R::backoff_delay(def, 0), 100ms);
|
|
expect_ns(R::backoff_delay(def, 1), 200ms);
|
|
expect_ns(R::backoff_delay(def, 2), 400ms);
|
|
expect_ns(R::backoff_delay(def, 8), 25'600ms);
|
|
expect_ns(R::backoff_delay(def, 9), 30s); // 51.2s clamped
|
|
expect_ns(R::backoff_delay(def, 63), 30s);
|
|
expect_ns(R::backoff_delay(def, 1'000'000), 30s);
|
|
expect_ns(R::backoff_delay(def, std::numeric_limits<std::uint32_t>::max()),
|
|
30s);
|
|
|
|
constexpr auto ns_max = std::chrono::nanoseconds::max();
|
|
|
|
// Absurd multiplier: the product leaves double range and still saturates.
|
|
{
|
|
R::BackoffSpec b;
|
|
b.multiplier = 1e300;
|
|
expect_ns(R::backoff_delay(b, 0), 100ms);
|
|
expect_ns(R::backoff_delay(b, 1), 30s);
|
|
expect_ns(R::backoff_delay(b, 1000), 30s);
|
|
expect_ns(R::backoff_delay(
|
|
b, std::numeric_limits<std::uint32_t>::max()),
|
|
30s);
|
|
}
|
|
|
|
// initial and cap near the top of the integer range.
|
|
{
|
|
R::BackoffSpec b;
|
|
b.initial = ns_max / 2;
|
|
b.cap = ns_max;
|
|
b.multiplier = 1e300;
|
|
expect_ns(R::backoff_delay(b, 0), ns_max / 2);
|
|
expect_ns(R::backoff_delay(b, 1), ns_max);
|
|
expect_ns(R::backoff_delay(b, 2), ns_max);
|
|
}
|
|
{
|
|
// Constant backoff is exact even where double cannot represent it.
|
|
R::BackoffSpec b;
|
|
b.initial = ns_max - 1ns;
|
|
b.cap = ns_max;
|
|
b.multiplier = 1.0;
|
|
expect_ns(R::backoff_delay(b, 0), ns_max - 1ns);
|
|
expect_ns(R::backoff_delay(b, 77), ns_max - 1ns);
|
|
}
|
|
{
|
|
// A product just below the cap converts exactly; the next one clamps.
|
|
R::BackoffSpec b;
|
|
b.initial = std::chrono::nanoseconds{1'000'000'000'000'000'000};
|
|
b.cap = ns_max;
|
|
b.multiplier = 2.0;
|
|
expect_ns(R::backoff_delay(b, 3),
|
|
std::chrono::nanoseconds{8'000'000'000'000'000'000});
|
|
expect_ns(R::backoff_delay(b, 4), ns_max);
|
|
}
|
|
|
|
// backoff_delay() is total even for specs that validate() rejects: it
|
|
// never overflows and stays within [0, cap].
|
|
{
|
|
R::BackoffSpec b;
|
|
b.multiplier = std::numeric_limits<double>::quiet_NaN();
|
|
const auto nan_delay = R::backoff_delay(b, 3);
|
|
expect(nan_delay >= 0ns);
|
|
expect(nan_delay <= b.cap);
|
|
b.multiplier = std::numeric_limits<double>::infinity();
|
|
expect_ns(R::backoff_delay(b, 1), b.cap);
|
|
b.multiplier = -1e300;
|
|
const auto negative = R::backoff_delay(b, 1);
|
|
expect(negative >= 0ns);
|
|
expect(negative <= b.cap);
|
|
b.multiplier = 0.5;
|
|
const auto shrinking = R::backoff_delay(b, 4);
|
|
expect(shrinking >= 0ns);
|
|
expect(shrinking <= b.cap);
|
|
R::BackoffSpec zero;
|
|
zero.initial = 0ns;
|
|
zero.multiplier = std::numeric_limits<double>::infinity();
|
|
expect_ns(R::backoff_delay(zero, 5), 0ns);
|
|
R::BackoffSpec negative_initial;
|
|
negative_initial.initial = -5ns;
|
|
expect_ns(R::backoff_delay(negative_initial, 2), 0ns);
|
|
}
|
|
|
|
// Through the state machine: a saturated delay added to the clock
|
|
// saturates too, and an unlimited budget still allows the wait.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 5;
|
|
spec.budget = R::no_deadline;
|
|
spec.backoff.initial = ns_max / 2;
|
|
spec.backoff.cap = ns_max;
|
|
spec.backoff.multiplier = 1e300;
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
const auto first =
|
|
state.next(t0, R::AttemptOutcome::retryable_failure);
|
|
expect_wait(first, t0 + ns_max / 2, 1);
|
|
expect_attempt(state.resume(first.at), 2);
|
|
const auto second =
|
|
state.next(first.at, R::AttemptOutcome::retryable_failure);
|
|
expect_wait(second, SteadyTimePoint::max(), 2);
|
|
}
|
|
|
|
// ... and a finite budget turns the same overshoot into a clean stop.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 5;
|
|
spec.budget = 1h;
|
|
spec.backoff.initial = ns_max / 2;
|
|
spec.backoff.cap = ns_max;
|
|
spec.backoff.multiplier = 1e300;
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::retryable_failure),
|
|
R::RetryStopReason::deadline_exceeded, 1);
|
|
}
|
|
}
|
|
|
|
// ── jitter ────────────────────────────────────────────────────────────────────
|
|
|
|
// clang-format off
|
|
Void::Testing::Test retry_state_full_equal_jitter_bounds_and_seed_reproducibility()
|
|
// clang-format on
|
|
{
|
|
constexpr auto delay = 100ms;
|
|
|
|
for (const R::JitterMode mode :
|
|
{R::JitterMode::Full, R::JitterMode::Equal}) {
|
|
const auto spec = constant_delay_spec(delay, mode);
|
|
const auto lo = (mode == R::JitterMode::Equal) ? delay / 2 : 0ms;
|
|
|
|
auto a = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
auto b = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
auto c = R::RetryState<Engine>::create(spec, t0, Engine{43});
|
|
expect(a.has_value() && b.has_value() && c.has_value());
|
|
if (!(a.has_value() && b.has_value() && c.has_value())) {
|
|
return;
|
|
}
|
|
const auto wa = collect_waits(*a, t0, 500);
|
|
const auto wb = collect_waits(*b, t0, 500);
|
|
const auto wc = collect_waits(*c, t0, 500);
|
|
expect_eq(wa.size(), std::size_t{500});
|
|
|
|
expect_all_within(wa, lo, delay);
|
|
// Same seed: same schedule. Different seed: a different schedule.
|
|
expect(wa == wb);
|
|
expect(wa != wc);
|
|
// The delay really is randomised.
|
|
const std::set<std::int64_t> distinct = [&] {
|
|
std::set<std::int64_t> s;
|
|
for (const auto w : wa) {
|
|
s.insert(w.count());
|
|
}
|
|
return s;
|
|
}();
|
|
expect(distinct.size() > 1);
|
|
}
|
|
|
|
// Tiny delays pin the exact formula: Full is uniform in [0, d], Equal is
|
|
// d/2 + uniform in [0, d - d/2]. Every value in range shows up and none
|
|
// outside it does.
|
|
struct Case {
|
|
std::chrono::nanoseconds d;
|
|
R::JitterMode mode;
|
|
std::set<std::int64_t> expected;
|
|
};
|
|
const Case cases[] = {
|
|
{3ns, R::JitterMode::Full, {0, 1, 2, 3}},
|
|
{3ns, R::JitterMode::Equal, {1, 2, 3}},
|
|
{2ns, R::JitterMode::Equal, {1, 2}},
|
|
{1ns, R::JitterMode::Equal, {0, 1}},
|
|
{1ns, R::JitterMode::Full, {0, 1}},
|
|
};
|
|
for (const auto& c : cases) {
|
|
const auto spec = constant_delay_spec(c.d, c.mode, 5000);
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
if (!created.has_value()) {
|
|
return;
|
|
}
|
|
std::set<std::int64_t> seen;
|
|
for (const auto w : collect_waits(*created, t0, 2000)) {
|
|
seen.insert(w.count());
|
|
}
|
|
expect(seen == c.expected);
|
|
}
|
|
|
|
// Growing backoff: each wait is bounded by that retry's own delay.
|
|
for (const R::JitterMode mode :
|
|
{R::JitterMode::Full, R::JitterMode::Equal}) {
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 40;
|
|
spec.budget = R::no_deadline;
|
|
spec.backoff.jitter = mode;
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
if (!created.has_value()) {
|
|
return;
|
|
}
|
|
const auto waits = collect_waits(*created, t0, 39);
|
|
expect_eq(waits.size(), std::size_t{39});
|
|
std::size_t outside = 0;
|
|
for (std::size_t k = 0; k < waits.size(); ++k) {
|
|
const auto d =
|
|
R::backoff_delay(spec.backoff, static_cast<std::uint32_t>(k));
|
|
const auto lo = (mode == R::JitterMode::Equal) ? d / 2 : 0ns;
|
|
if (waits[k] < lo || waits[k] > d) {
|
|
++outside;
|
|
}
|
|
}
|
|
expect_eq(outside, std::size_t{0});
|
|
}
|
|
|
|
// A zero delay has nothing to randomise.
|
|
for (const R::JitterMode mode :
|
|
{R::JitterMode::Full, R::JitterMode::Equal}) {
|
|
const auto spec = constant_delay_spec(0ns, mode, 50);
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
if (!created.has_value()) {
|
|
return;
|
|
}
|
|
const auto waits = collect_waits(*created, t0, 20);
|
|
expect_eq(waits.size(), std::size_t{20});
|
|
expect_all_within(waits, 0ns, 0ns);
|
|
}
|
|
|
|
// The largest possible delay does not overflow the integer draw.
|
|
{
|
|
const auto spec = constant_delay_spec(
|
|
std::chrono::nanoseconds::max(), R::JitterMode::Equal, 10);
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
if (!created.has_value()) {
|
|
return;
|
|
}
|
|
expect_attempt(created->begin(t0), 1);
|
|
const auto d =
|
|
created->next(t0, R::AttemptOutcome::retryable_failure);
|
|
expect_eq(static_cast<int>(d.action),
|
|
static_cast<int>(R::RetryAction::wait_until));
|
|
expect(d.at >= R::saturating_add(
|
|
t0, std::chrono::nanoseconds::max() / 2));
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test retry_state_default_engine_jitter_in_bounds()
|
|
{
|
|
constexpr auto delay = 100ms;
|
|
for (const R::JitterMode mode :
|
|
{R::JitterMode::Full, R::JitterMode::Equal}) {
|
|
const auto spec = constant_delay_spec(delay, mode);
|
|
const auto lo = (mode == R::JitterMode::Equal) ? delay / 2 : 0ms;
|
|
|
|
// No engine argument: the default seeds std::mt19937_64 from
|
|
// std::random_device. Only bounds are checked, never values.
|
|
auto created = R::RetryState<>::create(spec, t0);
|
|
expect(created.has_value());
|
|
if (!created.has_value()) {
|
|
return;
|
|
}
|
|
const auto waits = collect_waits(*created, t0, 300);
|
|
expect_eq(waits.size(), std::size_t{300});
|
|
expect_all_within(waits, lo, delay);
|
|
}
|
|
|
|
// The factory itself yields a usable engine, and falls back to default
|
|
// construction for engines that cannot be seeded from one integer.
|
|
{
|
|
auto engine = R::detail::seeded_engine<Engine>();
|
|
(void)engine();
|
|
}
|
|
{
|
|
auto fallback = R::detail::seeded_engine<StepEngine>();
|
|
StepEngine reference{};
|
|
for (int i = 0; i < 3; ++i) {
|
|
expect(fallback() == reference());
|
|
}
|
|
}
|
|
{
|
|
// Narrow seed type: compiles because the seed is passed with
|
|
// parentheses; the resulting engine drives a state normally.
|
|
auto narrow = R::detail::seeded_engine<NarrowSeedEngine>();
|
|
(void)narrow();
|
|
const auto spec = constant_delay_spec(delay, R::JitterMode::Equal);
|
|
auto created = R::RetryState<NarrowSeedEngine>::create(spec, t0);
|
|
expect(created.has_value());
|
|
if (!created.has_value()) {
|
|
return;
|
|
}
|
|
const auto waits = collect_waits(*created, t0, 100);
|
|
expect_eq(waits.size(), std::size_t{100});
|
|
expect_all_within(waits, delay / 2, delay);
|
|
}
|
|
{
|
|
const auto spec = constant_delay_spec(delay, R::JitterMode::Full);
|
|
auto created = R::RetryState<StepEngine>::create(spec, t0);
|
|
expect(created.has_value());
|
|
if (!created.has_value()) {
|
|
return;
|
|
}
|
|
const auto waits = collect_waits(*created, t0, 100);
|
|
expect_eq(waits.size(), std::size_t{100});
|
|
expect_all_within(waits, 0ns, delay);
|
|
}
|
|
}
|
|
|
|
// ── stop requests and classification ──────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_state_stop_prevents_next_attempt()
|
|
{
|
|
const R::RetrySpec spec{};
|
|
|
|
// A stop request before the first attempt prevents it and wins over an
|
|
// expired deadline.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42}, t0);
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_stop(state.begin(t0, true), R::RetryStopReason::stop_requested,
|
|
0);
|
|
expect_eq(state.attempts_started(), std::uint32_t{0});
|
|
}
|
|
|
|
// A stop request after a failed attempt ends the sequence without a wait.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::retryable_failure, true),
|
|
R::RetryStopReason::stop_requested, 1);
|
|
expect_eq(state.attempts_started(), std::uint32_t{1});
|
|
}
|
|
|
|
// A stop request that arrives while waiting is honoured by resume() and
|
|
// the attempt counter does not move.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
const auto d = state.next(t0, R::AttemptOutcome::retryable_failure);
|
|
expect_wait(d, t0 + 100ms, 1);
|
|
expect_stop(state.resume(d.at, true),
|
|
R::RetryStopReason::stop_requested, 1);
|
|
expect_eq(state.attempts_started(), std::uint32_t{1});
|
|
}
|
|
|
|
// stop wins over an expired deadline in resume() too.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.resume(t0 + 1h, true),
|
|
R::RetryStopReason::stop_requested, 1);
|
|
}
|
|
|
|
// Precedence inside next(): permanent failure and exhaustion are
|
|
// reported ahead of a stop request.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::permanent_failure, true),
|
|
R::RetryStopReason::not_retryable, 1);
|
|
}
|
|
{
|
|
R::RetrySpec one;
|
|
one.max_attempts = 1;
|
|
auto created = R::RetryState<Engine>::create(one, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::retryable_failure, true),
|
|
R::RetryStopReason::attempts_exhausted, 1);
|
|
}
|
|
|
|
// Without a stop request the same sequence goes on.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0, false), 1);
|
|
expect_wait(state.next(t0, R::AttemptOutcome::retryable_failure, false),
|
|
t0 + 100ms, 1);
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test retry_state_permanent_failure_stops()
|
|
{
|
|
const R::RetrySpec spec{};
|
|
|
|
// A permanent failure stops immediately even with attempts and budget
|
|
// left, and never produces a wait.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::permanent_failure),
|
|
R::RetryStopReason::not_retryable, 1);
|
|
}
|
|
|
|
// Permanent after a retry: attempts_started reflects both attempts.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
const auto d = state.next(t0, R::AttemptOutcome::retryable_failure);
|
|
expect_wait(d, t0 + 100ms, 1);
|
|
expect_attempt(state.resume(d.at), 2);
|
|
expect_stop(state.next(d.at, R::AttemptOutcome::permanent_failure),
|
|
R::RetryStopReason::not_retryable, 2);
|
|
}
|
|
|
|
// On the last attempt, not_retryable wins over attempts_exhausted.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
SteadyTimePoint now = t0;
|
|
expect_attempt(state.begin(now), 1);
|
|
for (std::uint32_t i = 2; i <= 3; ++i) {
|
|
const auto d =
|
|
state.next(now, R::AttemptOutcome::retryable_failure);
|
|
expect_eq(static_cast<int>(d.action),
|
|
static_cast<int>(R::RetryAction::wait_until));
|
|
now = d.at;
|
|
expect_attempt(state.resume(now), i);
|
|
}
|
|
expect_stop(state.next(now, R::AttemptOutcome::permanent_failure),
|
|
R::RetryStopReason::not_retryable, 3);
|
|
}
|
|
|
|
// Permanent also wins over an exceeded deadline.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0 + 1h, R::AttemptOutcome::permanent_failure),
|
|
R::RetryStopReason::not_retryable, 1);
|
|
}
|
|
|
|
// succeed() ends the sequence with `succeeded` and the attempt count.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.succeed(), R::RetryStopReason::succeeded, 1);
|
|
}
|
|
|
|
// The default classifier retries everything and ignores the error value.
|
|
expect(R::RetryEveryError{}(42) == R::AttemptOutcome::retryable_failure);
|
|
expect(R::RetryEveryError{}(std::string_view{"x"}) ==
|
|
R::AttemptOutcome::retryable_failure);
|
|
static_assert(R::RetryEveryError{}(0) ==
|
|
R::AttemptOutcome::retryable_failure);
|
|
}
|
|
|
|
// ── saturating time arithmetic ────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_state_saturating_add_near_max()
|
|
{
|
|
constexpr auto tp_max = SteadyTimePoint::max();
|
|
constexpr auto tp_min = SteadyTimePoint::min();
|
|
constexpr auto ns_max = std::chrono::nanoseconds::max();
|
|
constexpr auto ns_min = std::chrono::nanoseconds::min();
|
|
|
|
// Compile-time evaluation is part of the contract.
|
|
static_assert(R::saturating_add(tp_max, 1ns) == tp_max);
|
|
static_assert(R::saturating_add(tp_min, -1ns) == tp_min);
|
|
static_assert(R::saturating_add(t0, 1s) == t0 + 1s);
|
|
|
|
// Upward saturation.
|
|
expect_tp(R::saturating_add(tp_max, 1ns), tp_max);
|
|
expect_tp(R::saturating_add(tp_max, ns_max), tp_max);
|
|
expect_tp(R::saturating_add(tp_max - 1ns, 1ns), tp_max);
|
|
expect_tp(R::saturating_add(tp_max - 1ns, 2ns), tp_max);
|
|
expect_tp(R::saturating_add(tp_max - 10s, 10s), tp_max);
|
|
expect_tp(R::saturating_add(tp_max - 10s, 10s + 1ns), tp_max);
|
|
expect_tp(R::saturating_add(t0, ns_max), tp_max);
|
|
expect_tp(R::saturating_add(t0 + 1h, ns_max), tp_max);
|
|
|
|
// Exact results right up to the boundary.
|
|
expect_tp(R::saturating_add(tp_max - 10s, 10s - 1ns), tp_max - 1ns);
|
|
expect_tp(R::saturating_add(tp_max - 10s, 5s), tp_max - 5s);
|
|
|
|
// Downward saturation.
|
|
expect_tp(R::saturating_add(tp_min, -1ns), tp_min);
|
|
expect_tp(R::saturating_add(tp_min, ns_min), tp_min);
|
|
expect_tp(R::saturating_add(tp_min + 1ns, -1ns), tp_min);
|
|
expect_tp(R::saturating_add(tp_min + 1ns, -2ns), tp_min);
|
|
expect_tp(R::saturating_add(SteadyTimePoint{}, ns_min), tp_min);
|
|
expect_tp(R::saturating_add(tp_min + 5s, -5s), tp_min);
|
|
expect_tp(R::saturating_add(tp_min + 5s, -5s - 1ns), tp_min);
|
|
|
|
// Ordinary arithmetic is unchanged.
|
|
expect_tp(R::saturating_add(t0, 0ns), t0);
|
|
expect_tp(R::saturating_add(t0, 1s), t0 + 1s);
|
|
expect_tp(R::saturating_add(t0, -1s), t0 - 1s);
|
|
expect_tp(R::saturating_add(t0, ns_min), t0 + ns_min);
|
|
expect_tp(R::saturating_add(tp_max, -1ns), tp_max - 1ns);
|
|
expect_tp(R::saturating_add(tp_min, 1ns), tp_min + 1ns);
|
|
expect_tp(R::saturating_add(tp_max, ns_min), tp_max + ns_min);
|
|
}
|
|
|
|
// ── final stop decisions ─────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_state_stop_decisions_are_final()
|
|
{
|
|
const R::RetrySpec spec{};
|
|
|
|
// After a stop request, a later resume() without the stop flag must not
|
|
// start another attempt: the stop decision is repeated.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::retryable_failure, true),
|
|
R::RetryStopReason::stop_requested, 1);
|
|
expect_stop(state.resume(t0), R::RetryStopReason::stop_requested, 1);
|
|
expect_stop(state.begin(t0), R::RetryStopReason::stop_requested, 1);
|
|
expect_eq(state.attempts_started(), std::uint32_t{1});
|
|
}
|
|
|
|
// After succeed(), begin()/resume()/next() keep reporting success.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.succeed(), R::RetryStopReason::succeeded, 1);
|
|
expect_stop(state.resume(t0), R::RetryStopReason::succeeded, 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::retryable_failure),
|
|
R::RetryStopReason::succeeded, 1);
|
|
expect_eq(state.attempts_started(), std::uint32_t{1});
|
|
}
|
|
|
|
// A permanent failure stays final even if the caller ignores it.
|
|
{
|
|
auto created = R::RetryState<Engine>::create(spec, t0, Engine{42});
|
|
expect(created.has_value());
|
|
auto& state = *created;
|
|
expect_attempt(state.begin(t0), 1);
|
|
expect_stop(state.next(t0, R::AttemptOutcome::permanent_failure),
|
|
R::RetryStopReason::not_retryable, 1);
|
|
expect_stop(state.resume(t0), R::RetryStopReason::not_retryable, 1);
|
|
expect_eq(state.attempts_started(), std::uint32_t{1});
|
|
}
|
|
}
|