- 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>
1045 lines
29 KiB
C++
1045 lines
29 KiB
C++
#include <Void/Functional/Retry.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <expected>
|
|
#include <limits>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <random>
|
|
#include <source_location>
|
|
#include <stop_token>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
// retry_blocking is exercised against a fake clock and a fake sleeper that
|
|
// both live in this file: no Time module and no real sleeping anywhere in
|
|
// this file. The last two tests cover the default (real) options only on
|
|
// paths that return immediately: an already-requested stop, a zero-length
|
|
// wait and a cross-thread stop request that is race-free whichever side
|
|
// wins. The timed ThreadSleep path is deliberately not exercised here.
|
|
|
|
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 Ns = std::chrono::nanoseconds;
|
|
|
|
constexpr SteadyTimePoint t0{std::chrono::seconds{1000}};
|
|
|
|
// Manually advanced clock plus a record of everything the adapter asked
|
|
// the sleeper to do.
|
|
struct FakeWorld {
|
|
SteadyTimePoint now{t0};
|
|
std::vector<Ns> waits;
|
|
bool sleeper_saw_stoppable_token{false};
|
|
};
|
|
|
|
struct FakeNow {
|
|
FakeWorld* world{nullptr};
|
|
[[nodiscard]] SteadyTimePoint operator()() const noexcept
|
|
{
|
|
return world->now;
|
|
}
|
|
};
|
|
|
|
// Records the wait and moves the fake clock forward by exactly that much.
|
|
struct FakeSleep {
|
|
FakeWorld* world{nullptr};
|
|
bool operator()(Ns wait, std::stop_token stop) const
|
|
{
|
|
world->waits.push_back(wait);
|
|
world->sleeper_saw_stoppable_token = stop.stop_possible();
|
|
world->now += wait;
|
|
return true;
|
|
}
|
|
};
|
|
|
|
// Models a sleeper that is cut short by a stop request: it asks the stop
|
|
// source to stop and, when `interrupted`, reports the wait as cut short
|
|
// (what ThreadSleep does).
|
|
struct StoppingSleep {
|
|
FakeWorld* world{nullptr};
|
|
std::stop_source* source{nullptr};
|
|
bool interrupted{true};
|
|
bool operator()(Ns wait, std::stop_token) const
|
|
{
|
|
world->waits.push_back(wait);
|
|
if (source != nullptr) {
|
|
source->request_stop();
|
|
}
|
|
if (interrupted) {
|
|
return false;
|
|
}
|
|
world->now += wait;
|
|
return true;
|
|
}
|
|
};
|
|
|
|
struct FatalClassifier {
|
|
[[nodiscard]] R::AttemptOutcome
|
|
operator()(const std::string& e) const noexcept
|
|
{
|
|
return e == "fatal" ? R::AttemptOutcome::permanent_failure
|
|
: R::AttemptOutcome::retryable_failure;
|
|
}
|
|
};
|
|
|
|
template <class Sleep, class Classify = R::RetryEveryError>
|
|
[[nodiscard]] auto make_options(FakeWorld& world, Sleep sleep,
|
|
Classify classify = {})
|
|
{
|
|
return R::BlockingRetryOptions<FakeNow, Sleep, Classify, Engine>{
|
|
.now = FakeNow{&world},
|
|
.sleep = std::move(sleep),
|
|
.classify = std::move(classify),
|
|
.urbg = Engine{42},
|
|
.stop = {},
|
|
.deadline = std::nullopt};
|
|
}
|
|
|
|
[[nodiscard]] auto make_options(FakeWorld& world)
|
|
{
|
|
return make_options(world, FakeSleep{&world});
|
|
}
|
|
|
|
void expect_waits(const std::vector<Ns>& actual,
|
|
const std::vector<Ns>& expected,
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
expect_eq(actual.size(), expected.size(), loc);
|
|
if (actual.size() != expected.size()) {
|
|
return;
|
|
}
|
|
for (std::size_t i = 0; i < actual.size(); ++i) {
|
|
expect_eq(actual[i].count(), expected[i].count(), loc);
|
|
}
|
|
}
|
|
|
|
// Bulk checks are folded into one assertion: 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.
|
|
void expect_all_within(const std::vector<Ns>& values, Ns lo, Ns hi,
|
|
std::source_location loc = std::source_location::current())
|
|
{
|
|
std::size_t outside = 0;
|
|
for (const Ns v : values) {
|
|
if (v < lo || v > hi) {
|
|
++outside;
|
|
}
|
|
}
|
|
expect_eq(outside, std::size_t{0}, loc);
|
|
}
|
|
|
|
[[nodiscard]] R::RetrySpec constant_delay_spec(
|
|
Ns delay, std::optional<R::JitterMode> jitter, std::uint32_t attempts)
|
|
{
|
|
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
|
|
|
|
// ── schedule, attempt counts and deadlines on a fake clock ────────────────────
|
|
|
|
Void::Testing::Test retry_blocking_fake_clock_exact_waits_and_attempts()
|
|
{
|
|
// Succeeds on the fourth attempt: three waits of 100ms, 200ms, 400ms.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 4;
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
if (++calls < 4) {
|
|
return std::unexpected{std::string{"e"}};
|
|
}
|
|
return 7;
|
|
},
|
|
make_options(world));
|
|
expect(r.has_value());
|
|
if (r.has_value()) {
|
|
expect_eq(*r, 7);
|
|
}
|
|
expect_eq(calls, 4);
|
|
expect_waits(world.waits, {100ms, 200ms, 400ms});
|
|
expect(world.now == t0 + 700ms);
|
|
expect(world.sleeper_saw_stoppable_token == false);
|
|
}
|
|
|
|
// Default spec (3 attempts), every attempt fails: two waits, then
|
|
// "attempts exhausted" with the third error kept.
|
|
{
|
|
const R::RetrySpec spec{};
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, int> {
|
|
return std::unexpected{++calls};
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::attempts_exhausted));
|
|
expect_eq(r.error().attempts, std::uint32_t{3});
|
|
expect(r.error().last_error.has_value());
|
|
expect(!r.error().config.has_value());
|
|
if (r.error().last_error.has_value()) {
|
|
expect_eq(*r.error().last_error, 3);
|
|
}
|
|
}
|
|
expect_eq(calls, 3);
|
|
expect_waits(world.waits, {100ms, 200ms});
|
|
expect(world.now == t0 + 300ms);
|
|
}
|
|
|
|
// A budget of 250ms: attempt 2 starts at 100ms, and the 200ms wait that
|
|
// would follow ends at 300ms, past the deadline.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 5;
|
|
spec.budget = 250ms;
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, int> {
|
|
return std::unexpected{++calls};
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::deadline_exceeded));
|
|
expect_eq(r.error().attempts, std::uint32_t{2});
|
|
}
|
|
expect_eq(calls, 2);
|
|
expect_waits(world.waits, {100ms});
|
|
expect(world.now == t0 + 100ms);
|
|
}
|
|
|
|
// A wake-up landing exactly on the deadline is too late.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 5;
|
|
spec.budget = 300ms;
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, int> {
|
|
return std::unexpected{++calls};
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::deadline_exceeded));
|
|
}
|
|
expect_eq(calls, 2);
|
|
}
|
|
|
|
// Attempts that take time count against the budget (1s): attempt 1 ends
|
|
// at 600ms, the 100ms wait fits, attempt 2 ends at 1300ms and nothing
|
|
// more fits.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 5;
|
|
spec.budget = 1s;
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, int> {
|
|
world.now += 600ms;
|
|
return std::unexpected{++calls};
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::deadline_exceeded));
|
|
expect_eq(r.error().attempts, std::uint32_t{2});
|
|
}
|
|
expect_eq(calls, 2);
|
|
expect_waits(world.waits, {100ms});
|
|
}
|
|
|
|
// Jitter on a fake clock: each of the 49 waits lies in [0, 100ms], and a
|
|
// second run with the same seed produces the same schedule.
|
|
{
|
|
const auto spec =
|
|
constant_delay_spec(100ms, R::JitterMode::Full, 50);
|
|
std::vector<Ns> first;
|
|
for (int run = 0; run < 2; ++run) {
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, int> {
|
|
return std::unexpected{++calls};
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
expect_eq(calls, 50);
|
|
expect_eq(world.waits.size(), std::size_t{49});
|
|
expect_all_within(world.waits, 0ns, 100ms);
|
|
if (run == 0) {
|
|
first = world.waits;
|
|
} else {
|
|
expect(first == world.waits);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── validation and admission ──────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_blocking_zero_attempts_config_error_no_call()
|
|
{
|
|
// The 1.x retry() treats zero attempts as a default-constructed success;
|
|
// retry_blocking reports a configuration error and never calls `fn`.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 0;
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
return 1;
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect(r.error().config.has_value());
|
|
if (r.error().config.has_value()) {
|
|
expect_eq(static_cast<int>(*r.error().config),
|
|
static_cast<int>(R::RetryConfigError::zero_attempts));
|
|
}
|
|
expect_eq(r.error().attempts, std::uint32_t{0});
|
|
expect(!r.error().last_error.has_value());
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::none));
|
|
}
|
|
expect_eq(calls, 0);
|
|
expect(world.waits.empty());
|
|
}
|
|
|
|
// Every other configuration error is reported the same way.
|
|
{
|
|
struct Bad {
|
|
R::RetrySpec spec;
|
|
R::RetryConfigError error;
|
|
};
|
|
std::vector<Bad> bad;
|
|
{
|
|
R::RetrySpec s;
|
|
s.backoff.initial = -1ns;
|
|
bad.push_back({s, R::RetryConfigError::negative_duration});
|
|
}
|
|
{
|
|
R::RetrySpec s;
|
|
s.budget = 0ns;
|
|
bad.push_back({s, R::RetryConfigError::zero_budget});
|
|
}
|
|
{
|
|
R::RetrySpec s;
|
|
s.backoff.multiplier = std::numeric_limits<double>::quiet_NaN();
|
|
bad.push_back({s, R::RetryConfigError::nonfinite_multiplier});
|
|
}
|
|
{
|
|
R::RetrySpec s;
|
|
s.backoff.multiplier = 0.5;
|
|
bad.push_back({s, R::RetryConfigError::multiplier_below_one});
|
|
}
|
|
{
|
|
R::RetrySpec s;
|
|
s.backoff.initial = 2s;
|
|
s.backoff.cap = 1s;
|
|
bad.push_back({s, R::RetryConfigError::cap_below_initial});
|
|
}
|
|
for (const Bad& b : bad) {
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
b.spec,
|
|
[&]() -> std::expected<int, int> {
|
|
++calls;
|
|
return 1;
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect(r.error().config.has_value());
|
|
if (r.error().config.has_value()) {
|
|
expect_eq(static_cast<int>(*r.error().config),
|
|
static_cast<int>(b.error));
|
|
}
|
|
expect_eq(r.error().attempts, std::uint32_t{0});
|
|
}
|
|
expect_eq(calls, 0);
|
|
expect(world.waits.empty());
|
|
}
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test retry_blocking_expired_deadline_no_call_no_success()
|
|
{
|
|
const R::RetrySpec spec{};
|
|
|
|
// An admission deadline that is already due: `fn` would succeed, but it
|
|
// is never called and no success is returned.
|
|
for (const auto deadline : {t0, t0 - 1h, t0 - 1ns}) {
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
auto options = make_options(world);
|
|
options.deadline = deadline;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
return 1;
|
|
},
|
|
std::move(options));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::deadline_exceeded));
|
|
expect_eq(r.error().attempts, std::uint32_t{0});
|
|
expect(!r.error().last_error.has_value());
|
|
expect(!r.error().config.has_value());
|
|
}
|
|
expect_eq(calls, 0);
|
|
expect(world.waits.empty());
|
|
}
|
|
|
|
// One nanosecond of room is enough to run the attempt.
|
|
{
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
auto options = make_options(world);
|
|
options.deadline = t0 + 1ns;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
return 1;
|
|
},
|
|
std::move(options));
|
|
expect(r.has_value());
|
|
expect_eq(calls, 1);
|
|
}
|
|
|
|
// The admission deadline tightens the spec's budget (30s default).
|
|
{
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
auto options = make_options(world);
|
|
options.deadline = t0 + 250ms;
|
|
R::RetrySpec many;
|
|
many.max_attempts = 10;
|
|
const auto r = R::retry_blocking(
|
|
many,
|
|
[&]() -> std::expected<int, int> {
|
|
return std::unexpected{++calls};
|
|
},
|
|
std::move(options));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::deadline_exceeded));
|
|
expect_eq(r.error().attempts, std::uint32_t{2});
|
|
}
|
|
expect_waits(world.waits, {100ms});
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test retry_blocking_last_error_and_attempt_count()
|
|
{
|
|
// The most recent error is kept, not the first.
|
|
{
|
|
const R::RetrySpec spec{};
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
return std::unexpected{"e" + std::to_string(++calls)};
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(r.error().attempts, std::uint32_t{3});
|
|
expect(r.error().last_error.has_value());
|
|
if (r.error().last_error.has_value()) {
|
|
expect_eq(*r.error().last_error, std::string{"e3"});
|
|
}
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::attempts_exhausted));
|
|
}
|
|
expect_eq(calls, 3);
|
|
}
|
|
|
|
// A single allowed attempt: one call, one error, no waits.
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 1;
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
return std::unexpected{std::string{"only"}};
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(r.error().attempts, std::uint32_t{1});
|
|
expect(r.error().last_error.has_value());
|
|
if (r.error().last_error.has_value()) {
|
|
expect_eq(*r.error().last_error, std::string{"only"});
|
|
}
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::attempts_exhausted));
|
|
}
|
|
expect_eq(calls, 1);
|
|
expect(world.waits.empty());
|
|
}
|
|
|
|
// First attempt succeeds: the value comes back and nothing else happens.
|
|
{
|
|
const R::RetrySpec spec{};
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<std::string, std::string> {
|
|
++calls;
|
|
return std::string{"ok"};
|
|
},
|
|
make_options(world));
|
|
expect(r.has_value());
|
|
if (r.has_value()) {
|
|
expect_eq(*r, std::string{"ok"});
|
|
}
|
|
expect_eq(calls, 1);
|
|
expect(world.waits.empty());
|
|
}
|
|
|
|
// No attempt ran: last_error is empty (stop requested before the start).
|
|
{
|
|
const R::RetrySpec spec{};
|
|
FakeWorld world;
|
|
std::stop_source source;
|
|
source.request_stop();
|
|
auto options = make_options(world);
|
|
options.stop = source.get_token();
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
return 1;
|
|
},
|
|
std::move(options));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::stop_requested));
|
|
expect_eq(r.error().attempts, std::uint32_t{0});
|
|
expect(!r.error().last_error.has_value());
|
|
expect(!r.error().config.has_value());
|
|
}
|
|
expect_eq(calls, 0);
|
|
}
|
|
|
|
// expected<void, E> is supported.
|
|
{
|
|
const R::RetrySpec spec{};
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<void, int> {
|
|
if (++calls < 2) {
|
|
return std::unexpected{calls};
|
|
}
|
|
return {};
|
|
},
|
|
make_options(world));
|
|
expect(r.has_value());
|
|
expect_eq(calls, 2);
|
|
expect_waits(world.waits, {100ms});
|
|
}
|
|
}
|
|
|
|
// ── stop requests ─────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_blocking_stop_during_sleep_no_extra_attempt()
|
|
{
|
|
const R::RetrySpec spec{};
|
|
|
|
// The sleeper is interrupted (returns false): the call ends with
|
|
// stop_requested and the second attempt never starts.
|
|
{
|
|
FakeWorld world;
|
|
std::stop_source source;
|
|
auto options =
|
|
make_options(world, StoppingSleep{&world, &source, true});
|
|
options.stop = source.get_token();
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
return std::unexpected{"e" + std::to_string(++calls)};
|
|
},
|
|
std::move(options));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::stop_requested));
|
|
expect_eq(r.error().attempts, std::uint32_t{1});
|
|
expect(r.error().last_error.has_value());
|
|
if (r.error().last_error.has_value()) {
|
|
expect_eq(*r.error().last_error, std::string{"e1"});
|
|
}
|
|
}
|
|
expect_eq(calls, 1);
|
|
expect_waits(world.waits, {100ms});
|
|
}
|
|
|
|
// The sleeper reports a normal wake-up but a stop was requested meanwhile:
|
|
// resume() sees it and no further attempt starts.
|
|
{
|
|
FakeWorld world;
|
|
std::stop_source source;
|
|
auto options =
|
|
make_options(world, StoppingSleep{&world, &source, false});
|
|
options.stop = source.get_token();
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
return std::unexpected{std::string{"e"}};
|
|
},
|
|
std::move(options));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::stop_requested));
|
|
expect_eq(r.error().attempts, std::uint32_t{1});
|
|
}
|
|
expect_eq(calls, 1);
|
|
expect_waits(world.waits, {100ms});
|
|
}
|
|
|
|
// A stop requested from inside the attempt ends the sequence without
|
|
// any wait.
|
|
{
|
|
FakeWorld world;
|
|
std::stop_source source;
|
|
auto options = make_options(world);
|
|
options.stop = source.get_token();
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
source.request_stop();
|
|
return std::unexpected{std::string{"e"}};
|
|
},
|
|
std::move(options));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::stop_requested));
|
|
expect_eq(r.error().attempts, std::uint32_t{1});
|
|
expect(r.error().last_error.has_value());
|
|
}
|
|
expect_eq(calls, 1);
|
|
expect(world.waits.empty());
|
|
}
|
|
|
|
// A success is a success even if a stop was requested during the attempt.
|
|
{
|
|
FakeWorld world;
|
|
std::stop_source source;
|
|
auto options = make_options(world);
|
|
options.stop = source.get_token();
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
source.request_stop();
|
|
return 5;
|
|
},
|
|
std::move(options));
|
|
expect(r.has_value());
|
|
if (r.has_value()) {
|
|
expect_eq(*r, 5);
|
|
}
|
|
}
|
|
|
|
// The sleeper receives the caller's stop token.
|
|
{
|
|
FakeWorld world;
|
|
std::stop_source source;
|
|
auto options = make_options(world);
|
|
options.stop = source.get_token();
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, int> {
|
|
if (++calls < 2) {
|
|
return std::unexpected{calls};
|
|
}
|
|
return 1;
|
|
},
|
|
std::move(options));
|
|
expect(r.has_value());
|
|
expect(world.sleeper_saw_stoppable_token);
|
|
}
|
|
}
|
|
|
|
// ── classification ────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_blocking_permanent_failure_single_attempt()
|
|
{
|
|
R::RetrySpec spec;
|
|
spec.max_attempts = 5;
|
|
|
|
// A permanent error on the first attempt: one call, no wait.
|
|
{
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
return std::unexpected{std::string{"fatal"}};
|
|
},
|
|
make_options(world, FakeSleep{&world}, FatalClassifier{}));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::not_retryable));
|
|
expect_eq(r.error().attempts, std::uint32_t{1});
|
|
expect(r.error().last_error.has_value());
|
|
if (r.error().last_error.has_value()) {
|
|
expect_eq(*r.error().last_error, std::string{"fatal"});
|
|
}
|
|
}
|
|
expect_eq(calls, 1);
|
|
expect(world.waits.empty());
|
|
}
|
|
|
|
// Transient errors are retried until a permanent one shows up.
|
|
{
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::string> {
|
|
return std::unexpected{++calls < 3 ? std::string{"transient"}
|
|
: std::string{"fatal"}};
|
|
},
|
|
make_options(world, FakeSleep{&world}, FatalClassifier{}));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::not_retryable));
|
|
expect_eq(r.error().attempts, std::uint32_t{3});
|
|
}
|
|
expect_eq(calls, 3);
|
|
expect_waits(world.waits, {100ms, 200ms});
|
|
}
|
|
|
|
// On the last allowed attempt, a permanent error is "not retryable", the
|
|
// same error under the default classifier is "attempts exhausted".
|
|
{
|
|
R::RetrySpec two;
|
|
two.max_attempts = 2;
|
|
auto fn = [](int& calls) {
|
|
return [&calls]() -> std::expected<int, std::string> {
|
|
return std::unexpected{++calls < 2 ? std::string{"transient"}
|
|
: std::string{"fatal"}};
|
|
};
|
|
};
|
|
{
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
two, fn(calls),
|
|
make_options(world, FakeSleep{&world}, FatalClassifier{}));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(
|
|
static_cast<int>(r.error().reason),
|
|
static_cast<int>(R::RetryStopReason::not_retryable));
|
|
}
|
|
}
|
|
{
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(two, fn(calls),
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect_eq(static_cast<int>(r.error().reason),
|
|
static_cast<int>(
|
|
R::RetryStopReason::attempts_exhausted));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── ownership ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test retry_blocking_move_only_value()
|
|
{
|
|
const R::RetrySpec spec{};
|
|
|
|
// Move-only success value.
|
|
{
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<std::unique_ptr<int>, int> {
|
|
if (++calls < 2) {
|
|
return std::unexpected{calls};
|
|
}
|
|
return std::make_unique<int>(42);
|
|
},
|
|
make_options(world));
|
|
expect(r.has_value());
|
|
if (r.has_value()) {
|
|
expect(*r != nullptr);
|
|
if (*r != nullptr) {
|
|
expect_eq(**r, 42);
|
|
}
|
|
}
|
|
expect_eq(calls, 2);
|
|
expect_waits(world.waits, {100ms});
|
|
}
|
|
|
|
// The value can be moved out of the result.
|
|
{
|
|
FakeWorld world;
|
|
auto r = R::retry_blocking(
|
|
spec,
|
|
[]() -> std::expected<std::unique_ptr<std::string>, int> {
|
|
return std::make_unique<std::string>("owned");
|
|
},
|
|
make_options(world));
|
|
expect(r.has_value());
|
|
if (r.has_value()) {
|
|
std::unique_ptr<std::string> taken = std::move(*r);
|
|
expect(taken != nullptr);
|
|
if (taken != nullptr) {
|
|
expect_eq(*taken, std::string{"owned"});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Move-only error type: kept as last_error without copying.
|
|
{
|
|
FakeWorld world;
|
|
int calls = 0;
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, std::unique_ptr<std::string>> {
|
|
return std::unexpected{
|
|
std::make_unique<std::string>("e" +
|
|
std::to_string(++calls))};
|
|
},
|
|
make_options(world));
|
|
expect(!r.has_value());
|
|
if (!r.has_value()) {
|
|
expect(r.error().last_error.has_value());
|
|
if (r.error().last_error.has_value() &&
|
|
*r.error().last_error != nullptr) {
|
|
expect_eq(**r.error().last_error, std::string{"e3"});
|
|
}
|
|
expect_eq(r.error().attempts, std::uint32_t{3});
|
|
}
|
|
}
|
|
|
|
// The callable itself may be move-only and is invoked as an lvalue.
|
|
{
|
|
FakeWorld world;
|
|
auto counter = std::make_unique<int>(0);
|
|
const auto r = R::retry_blocking(
|
|
spec,
|
|
[counter = std::move(counter)]() mutable
|
|
-> std::expected<int, int> {
|
|
if (++*counter < 3) {
|
|
return std::unexpected{*counter};
|
|
}
|
|
return *counter;
|
|
},
|
|
make_options(world));
|
|
expect(r.has_value());
|
|
if (r.has_value()) {
|
|
expect_eq(*r, 3);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ── legacy behaviour, recorded for later work ─────────────────────────────────
|
|
|
|
Void::Testing::Test legacy_retry_zero_attempts_behavior_pinned()
|
|
{
|
|
// 1.x defect, pinned on purpose and recorded for Wave C: with
|
|
// max_attempts == 0 the legacy retry(), retry_while() and
|
|
// retry_or_throw() never call the callable and return a
|
|
// default-constructed result, which for retry() is a *successful*
|
|
// std::expected. Do not "fix" this here; retry_blocking() is the API that
|
|
// rejects zero attempts (see retry_blocking_zero_attempts_*).
|
|
const R::Policy<R::NoDelay> policy{.max_attempts = 0};
|
|
|
|
int calls = 0;
|
|
const auto r = R::retry(policy, [&]() -> std::expected<int, std::string> {
|
|
++calls;
|
|
return std::unexpected{std::string{"never called"}};
|
|
});
|
|
expect_eq(calls, 0);
|
|
expect(r.has_value());
|
|
if (r.has_value()) {
|
|
expect_eq(*r, 0);
|
|
}
|
|
|
|
int while_calls = 0;
|
|
int predicate_calls = 0;
|
|
const int w = R::retry_while(
|
|
policy,
|
|
[&]() -> int {
|
|
++while_calls;
|
|
return 503;
|
|
},
|
|
[&](int) {
|
|
++predicate_calls;
|
|
return true;
|
|
});
|
|
expect_eq(while_calls, 0);
|
|
expect_eq(predicate_calls, 0);
|
|
expect_eq(w, 0);
|
|
|
|
int throw_calls = 0;
|
|
const int v =
|
|
R::retry_or_throw(policy, [&]() -> std::expected<int, std::string> {
|
|
++throw_calls;
|
|
return std::unexpected{std::string{"never called"}};
|
|
});
|
|
expect_eq(throw_calls, 0);
|
|
expect_eq(v, 0);
|
|
}
|
|
|
|
// ── default (real) options, immediate paths only ─────────────────────────────
|
|
|
|
Void::Testing::Test retry_blocking_thread_sleep_honors_stop_and_zero_wait()
|
|
{
|
|
R::ThreadSleep thread_sleep;
|
|
|
|
// A stop that is already requested wins without blocking, however long
|
|
// the wait.
|
|
{
|
|
std::stop_source source;
|
|
source.request_stop();
|
|
expect(!thread_sleep(1h, source.get_token()));
|
|
expect(!thread_sleep(Ns::max(), source.get_token()));
|
|
}
|
|
|
|
// A zero or negative wait returns at once, stoppable token or not.
|
|
{
|
|
std::stop_source source;
|
|
expect(thread_sleep(0ns, source.get_token()));
|
|
expect(thread_sleep(-5ns, source.get_token()));
|
|
expect(thread_sleep(0ns, std::stop_token{}));
|
|
expect(thread_sleep(Ns::min(), std::stop_token{}));
|
|
}
|
|
|
|
// A stop request from another thread ends an hour-long wait. The request
|
|
// may land before or after the wait begins; either way the call returns
|
|
// false promptly, so the outcome does not depend on timing.
|
|
{
|
|
std::stop_source source;
|
|
std::jthread requester{[&source] { source.request_stop(); }};
|
|
expect(!thread_sleep(1h, source.get_token()));
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test retry_blocking_defaults_compile_and_run()
|
|
{
|
|
// No options argument: steady clock, thread sleep, retry-everything
|
|
// classifier and a random_device-seeded engine are all defaults. The
|
|
// delay is zero, so ThreadSleep returns immediately and nothing blocks.
|
|
const auto spec = constant_delay_spec(0ns, std::nullopt, 3);
|
|
|
|
int calls = 0;
|
|
const auto failed = R::retry_blocking(
|
|
spec, [&]() -> std::expected<int, int> {
|
|
return std::unexpected{++calls};
|
|
});
|
|
expect(!failed.has_value());
|
|
if (!failed.has_value()) {
|
|
expect_eq(static_cast<int>(failed.error().reason),
|
|
static_cast<int>(R::RetryStopReason::attempts_exhausted));
|
|
expect_eq(failed.error().attempts, std::uint32_t{3});
|
|
}
|
|
expect_eq(calls, 3);
|
|
|
|
int second_calls = 0;
|
|
const auto ok = R::retry_blocking(
|
|
spec, [&]() -> std::expected<int, int> {
|
|
if (++second_calls < 2) {
|
|
return std::unexpected{second_calls};
|
|
}
|
|
return 9;
|
|
});
|
|
expect(ok.has_value());
|
|
if (ok.has_value()) {
|
|
expect_eq(*ok, 9);
|
|
}
|
|
expect_eq(second_calls, 2);
|
|
|
|
// Options with only the stop token set (all other members defaulted)
|
|
// work through the fully-spelled type as well.
|
|
std::stop_source source;
|
|
source.request_stop();
|
|
R::BlockingRetryOptions<> options;
|
|
options.stop = source.get_token();
|
|
int stopped_calls = 0;
|
|
const auto stopped = R::retry_blocking(
|
|
spec,
|
|
[&]() -> std::expected<int, int> {
|
|
++stopped_calls;
|
|
return 1;
|
|
},
|
|
std::move(options));
|
|
expect(!stopped.has_value());
|
|
expect_eq(stopped_calls, 0);
|
|
}
|