#include #include #include #include #include namespace { using Void::Testing::Test; using namespace std::chrono_literals; namespace R = Void::Retry; struct RetryJitter : Void::Testing::Suite { Test full_jitter_stays_within_bounds() { R::Jittered j{R::ConstantDelay{100ms}, R::JitterMode::Full}; for (int i = 0; i < 100; ++i) { const auto d = j.delay(0); Void::Testing::expect(d >= 0ms); Void::Testing::expect(d <= 100ms); } } Test equal_jitter_keeps_floor() { R::Jittered j{R::ConstantDelay{100ms}, R::JitterMode::Equal}; for (int i = 0; i < 100; ++i) { const auto d = j.delay(0); Void::Testing::expect(d >= 50ms); Void::Testing::expect(d <= 100ms); } } Test budget_stops_retries_early() { R::Policy policy; policy.max_attempts = 5; policy.backoff = R::ConstantDelay{0ms}; policy.max_elapsed = 5ms; int calls = 0; auto result = R::retry(policy, [&]() -> std::expected { ++calls; std::this_thread::sleep_for( 10ms); // overruns the budget return std::unexpected{-1}; }); Void::Testing::expect(!result.has_value()); // Budget (5ms) is blown after the first 10ms attempt, so we // stop well short of max_attempts. Void::Testing::expect(calls < 5); } Test jittered_retry_still_succeeds() { R::Policy> policy; policy.max_attempts = 3; policy.backoff = {R::ConstantDelay{1ms}, R::JitterMode::Full}; int calls = 0; auto result = R::retry(policy, [&]() -> std::expected { if (++calls < 2) { return std::unexpected{-1}; } return 42; }); Void::Testing::expect(result.has_value()); Void::Testing::expect_eq(result.value(), 42); Void::Testing::expect_eq(calls, 2); } }; } // namespace