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,86 @@
|
||||
#include <Void/Containers/BufferPool.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::BufferPool::BufferPool;
|
||||
using Void::Testing::Test;
|
||||
|
||||
struct BufferPoolTests : Void::Testing::Suite<BufferPoolTests> {
|
||||
// Logical size is the request; capacity rounds up to a power of
|
||||
// two.
|
||||
Test acquire_rounds_capacity_to_size_class()
|
||||
{
|
||||
BufferPool<std::int16_t> pool;
|
||||
auto buf = pool.acquire(500);
|
||||
Void::Testing::expect_eq(buf.size(), std::size_t{500});
|
||||
Void::Testing::expect_eq(buf.capacity(), std::size_t{512});
|
||||
}
|
||||
|
||||
// A fresh request allocates; a same-class request after drop
|
||||
// reuses.
|
||||
Test reuses_storage_of_same_size_class()
|
||||
{
|
||||
BufferPool<std::int16_t> pool;
|
||||
std::int16_t* first = nullptr;
|
||||
{
|
||||
auto buf = pool.acquire(500);
|
||||
first = buf.data();
|
||||
} // returned to the pool here
|
||||
Void::Testing::expect_eq(pool.stats().allocations,
|
||||
std::uint64_t{1});
|
||||
Void::Testing::expect_eq(pool.stats().pooled, std::size_t{1});
|
||||
|
||||
auto buf2 = pool.acquire(300); // also class 512
|
||||
Void::Testing::expect_eq(pool.stats().reuses, std::uint64_t{1});
|
||||
Void::Testing::expect_eq(pool.stats().allocations,
|
||||
std::uint64_t{1});
|
||||
// Same backing storage -> no reallocation.
|
||||
Void::Testing::expect(buf2.data() == first);
|
||||
Void::Testing::expect_eq(buf2.capacity(), std::size_t{512});
|
||||
}
|
||||
|
||||
Test distinct_size_classes_use_distinct_buckets()
|
||||
{
|
||||
BufferPool<float> pool;
|
||||
{
|
||||
auto a = pool.acquire(500); // class 512
|
||||
auto b = pool.acquire(1000); // class 1024
|
||||
Void::Testing::expect_eq(a.capacity(), std::size_t{512});
|
||||
Void::Testing::expect_eq(b.capacity(), std::size_t{1024});
|
||||
}
|
||||
Void::Testing::expect_eq(pool.stats().allocations,
|
||||
std::uint64_t{2});
|
||||
Void::Testing::expect_eq(pool.stats().pooled, std::size_t{2});
|
||||
}
|
||||
|
||||
Test buffer_is_writable_and_iterable()
|
||||
{
|
||||
BufferPool<int> pool;
|
||||
auto buf = pool.acquire(4);
|
||||
for (std::size_t i = 0; i < buf.size(); ++i) {
|
||||
buf[i] = static_cast<int>(i * i);
|
||||
}
|
||||
int sum = 0;
|
||||
for (int v : buf) {
|
||||
sum += v;
|
||||
}
|
||||
Void::Testing::expect_eq(sum, 0 + 1 + 4 + 9);
|
||||
Void::Testing::expect_eq(buf.span().size(), std::size_t{4});
|
||||
}
|
||||
|
||||
Test shrink_drops_pooled_storage()
|
||||
{
|
||||
BufferPool<int> pool;
|
||||
{
|
||||
auto a = pool.acquire(10);
|
||||
}
|
||||
Void::Testing::expect_eq(pool.stats().pooled, std::size_t{1});
|
||||
pool.shrink();
|
||||
Void::Testing::expect_eq(pool.stats().pooled, std::size_t{0});
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -0,0 +1,123 @@
|
||||
#include <Void/Containers/Cache.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Cache::Cache;
|
||||
using Void::Cache::Policy;
|
||||
using Void::Testing::Test;
|
||||
using namespace std::chrono_literals;
|
||||
using TP = std::chrono::steady_clock::time_point;
|
||||
|
||||
// A manually-advanced clock for deterministic TTL tests.
|
||||
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 CacheTests : Void::Testing::Suite<CacheTests> {
|
||||
// ── TTL ───────────────────────────────────────────────────
|
||||
Test ttl_expires_lazily()
|
||||
{
|
||||
ManualClock clk;
|
||||
Cache<int, std::string> c{4, Policy::LRU, 100ms, clk.fn()};
|
||||
c.put(1, "a");
|
||||
clk.advance(50ms);
|
||||
Void::Testing::expect(c.get(1).has_value());
|
||||
clk.advance(60ms); // now 110ms > 100ms TTL
|
||||
Void::Testing::expect(!c.get(1).has_value());
|
||||
Void::Testing::expect_eq(c.stats().expirations,
|
||||
std::uint64_t{1});
|
||||
}
|
||||
|
||||
Test per_entry_ttl_overrides_default()
|
||||
{
|
||||
ManualClock clk;
|
||||
Cache<int, int> c{4, Policy::LRU, 100ms, clk.fn()};
|
||||
c.put(1, 10, 10ms); // shorter than default
|
||||
clk.advance(20ms);
|
||||
Void::Testing::expect(!c.get(1).has_value());
|
||||
}
|
||||
|
||||
Test sweep_purges_expired()
|
||||
{
|
||||
ManualClock clk;
|
||||
Cache<int, int> c{8, Policy::LRU, 10ms, clk.fn()};
|
||||
c.put(1, 1);
|
||||
c.put(2, 2);
|
||||
clk.advance(20ms);
|
||||
Void::Testing::expect_eq(c.sweep(), std::size_t{2});
|
||||
Void::Testing::expect_eq(c.size(), std::size_t{0});
|
||||
}
|
||||
|
||||
// ── eviction policies ─────────────────────────────────────
|
||||
Test lru_evicts_least_recently_used()
|
||||
{
|
||||
Cache<int, int> c{2, Policy::LRU};
|
||||
c.put(1, 1);
|
||||
c.put(2, 2);
|
||||
(void)c.get(1); // promote 1
|
||||
c.put(3, 3); // evicts 2
|
||||
Void::Testing::expect(c.contains(1));
|
||||
Void::Testing::expect(!c.contains(2));
|
||||
Void::Testing::expect(c.contains(3));
|
||||
}
|
||||
|
||||
Test fifo_evicts_oldest_inserted()
|
||||
{
|
||||
Cache<int, int> c{2, Policy::FIFO};
|
||||
c.put(1, 1);
|
||||
c.put(2, 2);
|
||||
(void)c.get(1); // does NOT promote under FIFO
|
||||
c.put(3, 3); // evicts 1 (oldest inserted)
|
||||
Void::Testing::expect(!c.contains(1));
|
||||
Void::Testing::expect(c.contains(2));
|
||||
Void::Testing::expect(c.contains(3));
|
||||
}
|
||||
|
||||
Test lfu_evicts_least_frequently_used()
|
||||
{
|
||||
Cache<int, int> c{2, Policy::LFU};
|
||||
c.put(1, 1);
|
||||
c.put(2, 2);
|
||||
(void)c.get(1);
|
||||
(void)c.get(1); // 1 is hot
|
||||
c.put(3, 3); // evicts 2 (lower frequency)
|
||||
Void::Testing::expect(c.contains(1));
|
||||
Void::Testing::expect(!c.contains(2));
|
||||
Void::Testing::expect(c.contains(3));
|
||||
}
|
||||
|
||||
// ── stats ─────────────────────────────────────────────────
|
||||
Test stats_track_hits_and_misses()
|
||||
{
|
||||
Cache<int, int> c{4};
|
||||
c.put(1, 1);
|
||||
(void)c.get(1); // hit
|
||||
(void)c.get(2); // miss
|
||||
(void)c.get(1); // hit
|
||||
const auto s = c.stats();
|
||||
Void::Testing::expect_eq(s.hits, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(s.misses, std::uint64_t{1});
|
||||
}
|
||||
|
||||
Test stats_track_evictions()
|
||||
{
|
||||
Cache<int, int> c{1, Policy::LRU};
|
||||
c.put(1, 1);
|
||||
c.put(2, 2); // evicts 1
|
||||
Void::Testing::expect_eq(c.stats().evictions, std::uint64_t{1});
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <Void/Containers/Debug.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct P {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test debug_dump_reflects_struct_elements()
|
||||
{
|
||||
std::vector<P> v{{1, 2}, {3, 4}};
|
||||
Void::Testing::expect_eq(Void::Containers::debug_dump(v),
|
||||
std::string{"[P{x: 1, y: 2}, P{x: 3, y: 4}]"});
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test debug_dump_handles_scalars()
|
||||
{
|
||||
std::vector<int> n{1, 2, 3};
|
||||
Void::Testing::expect_eq(Void::Containers::debug_dump(n),
|
||||
std::string{"[1, 2, 3]"});
|
||||
std::vector<int> empty;
|
||||
Void::Testing::expect_eq(Void::Containers::debug_dump(empty),
|
||||
std::string{"[]"});
|
||||
}
|
||||
Reference in new issue
Block a user