A round of new universal capabilities (all generic — templated/reflection based, no app coupling), verified by the test suite (1103 passing): Data reflection: - Reflect.hpp now (de)serializes chrono::duration, chrono::time_point, std::variant (tagged), std::array, and non-string-keyed std::map; Meta.hpp gains is_time_point_v et al. Async: - EventBus gains Mode::Async (worker-thread dispatch + flush()). - ThreadPool gains a bounded queue, try_submit(), and rejection stats. New modules / containers: - Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker, ResourceMonitor. - Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V> (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats). - System: process/CPU/memory metrics (Metrics.hpp) and a streaming Process API. Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions, history states, deferred events), Configuration/Logging reflection helpers, and the accompanying test suites. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
#include <Void/Functional/Retry.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Suite.hpp>
|
|
|
|
#include <chrono>
|
|
#include <expected>
|
|
|
|
namespace
|
|
{
|
|
using Void::Testing::Test;
|
|
using namespace std::chrono_literals;
|
|
namespace R = Void::Retry;
|
|
|
|
struct RetryJitter : Void::Testing::Suite<RetryJitter> {
|
|
Test full_jitter_stays_within_bounds()
|
|
{
|
|
R::Jittered<R::ConstantDelay> 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<R::ConstantDelay> 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<R::ConstantDelay> policy;
|
|
policy.max_attempts = 5;
|
|
policy.backoff = R::ConstantDelay{0ms};
|
|
policy.max_elapsed = 5ms;
|
|
|
|
int calls = 0;
|
|
auto result =
|
|
R::retry(policy, [&]() -> std::expected<int, int> {
|
|
++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<R::Jittered<R::ConstantDelay>> policy;
|
|
policy.max_attempts = 3;
|
|
policy.backoff = {R::ConstantDelay{1ms}, R::JitterMode::Full};
|
|
|
|
int calls = 0;
|
|
auto result =
|
|
R::retry(policy, [&]() -> std::expected<int, int> {
|
|
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
|