Files
Library/tests/standalone/Resilience/CircuitBreaker.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

90 lines
2.4 KiB
C++

#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