Add async/resilience/reflection primitives + FSM v2 rewrite
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>
This commit is contained in:
1 parent
43b5a81504
commit
80293ee56f
93 files changed
+8500
-3312
No files matched your search
@@ -0,0 +1,89 @@
|
||||
#include <Void/Resilience/CircuitBreaker.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <expected>
|
||||
#include <memory>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Resilience::CircuitBreaker;
|
||||
using Void::Testing::Test;
|
||||
using State = CircuitBreaker::State;
|
||||
using namespace std::chrono_literals;
|
||||
using TP = std::chrono::steady_clock::time_point;
|
||||
|
||||
struct ManualClock {
|
||||
std::shared_ptr<TP> now =
|
||||
std::make_shared<TP>(std::chrono::steady_clock::now());
|
||||
auto fn() const
|
||||
{
|
||||
auto p = now;
|
||||
return [p] { return *p; };
|
||||
}
|
||||
void advance(std::chrono::nanoseconds d) const { *now += d; }
|
||||
};
|
||||
|
||||
struct CircuitBreakerTests : Void::Testing::Suite<CircuitBreakerTests> {
|
||||
Test trips_open_after_threshold_failures()
|
||||
{
|
||||
ManualClock clk;
|
||||
CircuitBreaker cb{3, 1s, 1, clk.fn()};
|
||||
cb.record_failure();
|
||||
cb.record_failure();
|
||||
Void::Testing::expect(cb.state() == State::Closed);
|
||||
cb.record_failure(); // third -> open
|
||||
Void::Testing::expect(cb.state() == State::Open);
|
||||
Void::Testing::expect(!cb.allow());
|
||||
}
|
||||
|
||||
Test half_opens_after_cooldown_then_closes()
|
||||
{
|
||||
ManualClock clk;
|
||||
CircuitBreaker cb{2, 1s, 1, clk.fn()};
|
||||
cb.record_failure();
|
||||
cb.record_failure(); // open
|
||||
Void::Testing::expect(!cb.allow());
|
||||
clk.advance(1s);
|
||||
Void::Testing::expect(cb.allow()); // probe admitted (half-open)
|
||||
Void::Testing::expect(cb.state() == State::HalfOpen);
|
||||
cb.record_success(); // closes
|
||||
Void::Testing::expect(cb.state() == State::Closed);
|
||||
}
|
||||
|
||||
Test half_open_failure_reopens()
|
||||
{
|
||||
ManualClock clk;
|
||||
CircuitBreaker cb{2, 1s, 1, clk.fn()};
|
||||
cb.record_failure();
|
||||
cb.record_failure();
|
||||
clk.advance(1s);
|
||||
(void)cb.allow(); // -> half-open
|
||||
cb.record_failure();
|
||||
Void::Testing::expect(cb.state() == State::Open);
|
||||
}
|
||||
|
||||
Test execute_gates_and_records()
|
||||
{
|
||||
ManualClock clk;
|
||||
CircuitBreaker cb{2, 1s, 1, clk.fn()};
|
||||
const auto ok = [] { return std::expected<int, int>{42}; };
|
||||
const auto bad = [] {
|
||||
return std::expected<int, int>{std::unexpect, -1};
|
||||
};
|
||||
|
||||
auto r = cb.execute(ok);
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect(r->has_value());
|
||||
Void::Testing::expect_eq(r->value(), 42);
|
||||
|
||||
(void)cb.execute(bad);
|
||||
(void)cb.execute(bad); // open now
|
||||
Void::Testing::expect(cb.state() == State::Open);
|
||||
|
||||
auto rejected = cb.execute(ok);
|
||||
Void::Testing::expect(!rejected.has_value()); // circuit refused
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -0,0 +1,75 @@
|
||||
#include <Void/Resilience/KeyedRateLimiter.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Resilience::KeyedRateLimiter;
|
||||
using Void::Testing::Test;
|
||||
using namespace std::chrono_literals;
|
||||
using TP = std::chrono::steady_clock::time_point;
|
||||
|
||||
struct ManualClock {
|
||||
std::shared_ptr<TP> now =
|
||||
std::make_shared<TP>(std::chrono::steady_clock::now());
|
||||
auto fn() const
|
||||
{
|
||||
auto p = now;
|
||||
return [p] { return *p; };
|
||||
}
|
||||
void advance(std::chrono::nanoseconds d) const { *now += d; }
|
||||
};
|
||||
|
||||
struct KeyedRateLimiterTests : Void::Testing::Suite<KeyedRateLimiterTests> {
|
||||
// Each key drains its own bucket independently.
|
||||
Test keys_are_independent()
|
||||
{
|
||||
ManualClock clk;
|
||||
KeyedRateLimiter<std::uint64_t> rl{3.0, 1.0, clk.fn()};
|
||||
Void::Testing::expect(rl.allow(1));
|
||||
Void::Testing::expect(rl.allow(1));
|
||||
Void::Testing::expect(rl.allow(1));
|
||||
Void::Testing::expect(!rl.allow(1)); // key 1 exhausted
|
||||
Void::Testing::expect(rl.allow(2)); // key 2 still full
|
||||
}
|
||||
|
||||
Test refills_per_key_over_time()
|
||||
{
|
||||
ManualClock clk;
|
||||
KeyedRateLimiter<std::string> rl{2.0, 2.0, clk.fn()};
|
||||
Void::Testing::expect(rl.allow("a", 2.0));
|
||||
Void::Testing::expect(!rl.allow("a"));
|
||||
clk.advance(1s); // +2 tokens
|
||||
Void::Testing::expect(rl.allow("a"));
|
||||
}
|
||||
|
||||
Test reset_forgets_a_key()
|
||||
{
|
||||
ManualClock clk;
|
||||
KeyedRateLimiter<int> rl{1.0, 0.0, clk.fn()};
|
||||
Void::Testing::expect(rl.allow(7));
|
||||
Void::Testing::expect(!rl.allow(7));
|
||||
rl.reset(7); // next use starts full again
|
||||
Void::Testing::expect(rl.allow(7));
|
||||
}
|
||||
|
||||
Test cleanup_drops_stale_keys()
|
||||
{
|
||||
KeyedRateLimiter<int> rl{1.0, 1.0};
|
||||
(void)rl.allow(1);
|
||||
(void)rl.allow(2);
|
||||
Void::Testing::expect_eq(rl.tracked(), std::size_t{2});
|
||||
// Nothing is older than an hour yet.
|
||||
Void::Testing::expect_eq(rl.cleanup(1h), std::size_t{0});
|
||||
Void::Testing::expect_eq(rl.tracked(), std::size_t{2});
|
||||
// Everything is older than 0ns.
|
||||
Void::Testing::expect_eq(rl.cleanup(0ns), std::size_t{2});
|
||||
Void::Testing::expect_eq(rl.tracked(), std::size_t{0});
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <Void/Resilience/RateLimiter.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Resilience::TokenBucket;
|
||||
using Void::Testing::Test;
|
||||
using namespace std::chrono_literals;
|
||||
using TP = std::chrono::steady_clock::time_point;
|
||||
|
||||
struct ManualClock {
|
||||
std::shared_ptr<TP> now =
|
||||
std::make_shared<TP>(std::chrono::steady_clock::now());
|
||||
auto fn() const
|
||||
{
|
||||
auto p = now;
|
||||
return [p] { return *p; };
|
||||
}
|
||||
void advance(std::chrono::nanoseconds d) const { *now += d; }
|
||||
};
|
||||
|
||||
struct RateLimiterTests : Void::Testing::Suite<RateLimiterTests> {
|
||||
Test starts_full_and_drains()
|
||||
{
|
||||
ManualClock clk;
|
||||
TokenBucket b{10.0, 5.0, clk.fn()};
|
||||
Void::Testing::expect(b.try_acquire(10.0)); // drains the bucket
|
||||
Void::Testing::expect(!b.try_acquire(1.0)); // now empty
|
||||
}
|
||||
|
||||
Test refills_over_time()
|
||||
{
|
||||
ManualClock clk;
|
||||
TokenBucket b{10.0, 5.0, clk.fn()};
|
||||
Void::Testing::expect(b.try_acquire(10.0));
|
||||
clk.advance(1s); // +5 tokens
|
||||
Void::Testing::expect(b.try_acquire(5.0));
|
||||
Void::Testing::expect(!b.try_acquire(0.5));
|
||||
}
|
||||
|
||||
Test refill_is_clamped_to_capacity()
|
||||
{
|
||||
ManualClock clk;
|
||||
TokenBucket b{10.0, 5.0, clk.fn()};
|
||||
Void::Testing::expect(b.try_acquire(10.0));
|
||||
clk.advance(100s); // would be 500 tokens, clamps to 10
|
||||
Void::Testing::expect(b.available() <= 10.0);
|
||||
Void::Testing::expect(b.available() >= 9.99);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -0,0 +1,51 @@
|
||||
#include <Void/Resilience/ResourceMonitor.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Resilience::ResourceMonitor;
|
||||
using Void::Testing::Test;
|
||||
|
||||
struct ResourceMonitorTests : Void::Testing::Suite<ResourceMonitorTests> {
|
||||
Test acquire_refuses_to_exceed_limit()
|
||||
{
|
||||
ResourceMonitor m{100};
|
||||
Void::Testing::expect(m.try_acquire(80));
|
||||
Void::Testing::expect(!m.try_acquire(30)); // would be 110 > 100
|
||||
Void::Testing::expect_eq(m.usage(), std::uint64_t{80});
|
||||
}
|
||||
|
||||
Test high_watermark_fires_once_on_crossing()
|
||||
{
|
||||
ResourceMonitor m{100, 0.9, 0.7};
|
||||
int highs = 0;
|
||||
m.on_high_watermark([&](std::uint64_t) { ++highs; });
|
||||
Void::Testing::expect(m.try_acquire(80)); // 80 < 90, no fire
|
||||
Void::Testing::expect_eq(highs, 0);
|
||||
Void::Testing::expect(m.try_acquire(15)); // 95 >= 90, fire
|
||||
Void::Testing::expect_eq(highs, 1);
|
||||
Void::Testing::expect(m.try_acquire(3)); // 98, still alarmed
|
||||
Void::Testing::expect_eq(highs, 1); // no re-fire
|
||||
}
|
||||
|
||||
Test low_watermark_fires_on_drop()
|
||||
{
|
||||
ResourceMonitor m{100, 0.9, 0.7};
|
||||
int lows = 0;
|
||||
m.on_low_watermark([&](std::uint64_t) { ++lows; });
|
||||
Void::Testing::expect(m.try_acquire(95)); // alarmed
|
||||
m.release(30); // 65 <= 70 -> fire
|
||||
Void::Testing::expect_eq(lows, 1);
|
||||
Void::Testing::expect_eq(m.usage(), std::uint64_t{65});
|
||||
}
|
||||
|
||||
Test release_clamps_at_zero()
|
||||
{
|
||||
ResourceMonitor m{100};
|
||||
Void::Testing::expect(m.try_acquire(10));
|
||||
m.release(50); // more than held
|
||||
Void::Testing::expect_eq(m.usage(), std::uint64_t{0});
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
Reference in new issue
Block a user