Files
Library/tests/standalone/Resilience/RateLimiter.cpp
faumarayandClaude Opus 4.8 80293ee56f 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>
2026-06-22 15:45:16 +03:00

56 lines
1.5 KiB
C++

#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