Files
Library/tests/standalone/Resilience/ResourceMonitor.cpp
T
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

52 lines
1.6 KiB
C++

#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