Files
Library/tests/standalone/Retry/Retry.cpp
T
faumarayandClaude Sonnet 4.6 58ff7b38a0 Add 20+ new modules with tests: Pipeline, Matrix, Tokenizer, ScopeGuard, Bitset, Optional, Counted, Curry, TypeMap, Bench, DisjointSet, Histogram, StopWatch, and more
Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now
an alias for Edit<string> so count_op/apply work on line diffs; Memoized
uses std::apply instead of type-based get to handle duplicate arg types.

874/877 tests pass (3 are platform-gated plugin tests).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 23:49:09 +03:00

153 lines
5.2 KiB
C++

#include <Void/Retry/Retry.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <expected>
#include <string>
using namespace Void::Retry;
// ── retry ─────────────────────────────────────────────────────────────────────
Void::Testing::Test retry_succeeds_on_first_attempt()
{
const Policy<NoDelay> p{.max_attempts = 3};
int calls = 0;
const auto r = retry(p, [&]() -> std::expected<int, std::string> {
++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<NoDelay> p{.max_attempts = 3};
int calls = 0;
const auto r = retry(p, [&]() -> std::expected<int, std::string> {
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<NoDelay> p{.max_attempts = 4};
int calls = 0;
const auto r = retry(p, [&]() -> std::expected<int, std::string> {
++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<NoDelay> p{.max_attempts = 3};
int n = 0;
const auto r = retry(p, [&]() -> std::expected<int, std::string> {
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<NoDelay> p{.max_attempts = 2};
const auto v = retry_or_throw(p, []() -> std::expected<int, std::string> {
return 99;
});
Void::Testing::expect_eq(v, 99);
}
Void::Testing::Test retry_or_throw_throws_on_failure()
{
const Policy<NoDelay> p{.max_attempts = 2};
bool threw = false;
try {
retry_or_throw(p, []() -> std::expected<int, std::string> {
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<NoDelay> 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<NoDelay> 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});
}