Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
153 lines
5.2 KiB
C++
153 lines
5.2 KiB
C++
#include <Void/Functional/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});
|
|
}
|