#include #include #include #include #include using namespace Void::Retry; // ── retry ───────────────────────────────────────────────────────────────────── Void::Testing::Test retry_succeeds_on_first_attempt() { const Policy p{.max_attempts = 3}; int calls = 0; const auto r = retry(p, [&]() -> std::expected { ++calls; return 42; }); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, 42); Void::Testing::expect_eq(calls, 1); } Void::Testing::Test retry_succeeds_on_second_attempt() { const Policy p{.max_attempts = 3}; int calls = 0; const auto r = retry(p, [&]() -> std::expected { if (++calls < 2) return std::unexpected{"not yet"}; return 7; }); Void::Testing::expect(r.has_value()); Void::Testing::expect_eq(*r, 7); Void::Testing::expect_eq(calls, 2); } Void::Testing::Test retry_exhausts_all_attempts() { const Policy p{.max_attempts = 4}; int calls = 0; const auto r = retry(p, [&]() -> std::expected { ++calls; return std::unexpected{"always fails"}; }); Void::Testing::expect(!r.has_value()); Void::Testing::expect_eq(calls, 4); } Void::Testing::Test retry_returns_last_error() { const Policy p{.max_attempts = 3}; int n = 0; const auto r = retry(p, [&]() -> std::expected { return std::unexpected{std::to_string(++n)}; }); Void::Testing::expect(!r.has_value()); Void::Testing::expect_eq(r.error(), std::string{"3"}); } // ── retry_or_throw ──────────────────────────────────────────────────────────── Void::Testing::Test retry_or_throw_returns_value() { const Policy p{.max_attempts = 2}; const auto v = retry_or_throw(p, []() -> std::expected { return 99; }); Void::Testing::expect_eq(v, 99); } Void::Testing::Test retry_or_throw_throws_on_failure() { const Policy p{.max_attempts = 2}; bool threw = false; try { retry_or_throw(p, []() -> std::expected { return std::unexpected{"err"}; }); } catch (const std::runtime_error&) { threw = true; } Void::Testing::expect(threw); } // ── retry_while ─────────────────────────────────────────────────────────────── Void::Testing::Test retry_while_stops_on_success_code() { const Policy p{.max_attempts = 5}; int calls = 0; const auto r = retry_while( p, [&]() -> int { return ++calls < 3 ? 503 : 200; }, [](int code) { return code != 200; }); Void::Testing::expect_eq(r, 200); Void::Testing::expect_eq(calls, 3); } Void::Testing::Test retry_while_exhausted_returns_last() { const Policy p{.max_attempts = 3}; const auto r = retry_while( p, []() -> int { return 503; }, [](int code) { return code != 200; }); Void::Testing::expect_eq(r, 503); } // ── backoff policies (logic, no actual sleep) ───────────────────────────────── Void::Testing::Test nodelay_always_zero() { const NoDelay b; Void::Testing::expect_eq(b.delay(0).count(), std::int64_t{0}); Void::Testing::expect_eq(b.delay(10).count(), std::int64_t{0}); } Void::Testing::Test constant_delay_fixed() { const ConstantDelay b{std::chrono::milliseconds{250}}; Void::Testing::expect_eq(b.delay(0).count(), std::int64_t{250}); Void::Testing::expect_eq(b.delay(5).count(), std::int64_t{250}); } Void::Testing::Test linear_backoff_grows() { const LinearBackoff b{std::chrono::milliseconds{100}, std::chrono::milliseconds{10'000}}; Void::Testing::expect_eq(b.delay(0).count(), std::int64_t{100}); Void::Testing::expect_eq(b.delay(1).count(), std::int64_t{200}); Void::Testing::expect_eq(b.delay(2).count(), std::int64_t{300}); } Void::Testing::Test linear_backoff_clamped() { const LinearBackoff b{std::chrono::milliseconds{4000}, std::chrono::milliseconds{5000}}; Void::Testing::expect_eq(b.delay(2).count(), std::int64_t{5000}); } Void::Testing::Test exponential_backoff_grows() { const ExponentialBackoff b{std::chrono::milliseconds{100}, std::chrono::milliseconds{30'000}, 2.0}; Void::Testing::expect_eq(b.delay(0).count(), std::int64_t{100}); Void::Testing::expect_eq(b.delay(1).count(), std::int64_t{200}); Void::Testing::expect_eq(b.delay(2).count(), std::int64_t{400}); } Void::Testing::Test exponential_backoff_clamped() { const ExponentialBackoff b{std::chrono::milliseconds{10'000}, std::chrono::milliseconds{15'000}, 2.0}; Void::Testing::expect_eq(b.delay(2).count(), std::int64_t{15000}); }