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>
76 lines
2.2 KiB
C++
76 lines
2.2 KiB
C++
#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
|