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
@@ -3,15 +3,20 @@
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::Step;
|
||||
using Void::Fsm::State;
|
||||
using Void::Fsm::StateKey;
|
||||
namespace Fsm = Void::Fsm;
|
||||
|
||||
// Phase-6 reactive DSL: deferred events (held + replayed after a transition),
|
||||
// entry/exit actions (on.enter/on.exit), and internal transitions (handle an
|
||||
// event without leaving the state).
|
||||
// Imperative equivalents of the old reactive extras:
|
||||
// - deferred events : an event a state does not await stays buffered in the
|
||||
// typed mailbox and is consumed by a later state — defer
|
||||
// is automatic, no declaration needed.
|
||||
// - entry/exit actions: the first/last lines of run().
|
||||
// - internal transition: handle an event in a loop without leaving the state.
|
||||
|
||||
namespace
|
||||
{
|
||||
@@ -20,8 +25,8 @@ namespace
|
||||
struct Done {
|
||||
};
|
||||
|
||||
// Busy defers Click (held, not dropped) and runs enter/exit actions; on
|
||||
// Done it transitions to Idle, which then handles the replayed Click.
|
||||
// Busy awaits only Done; a Click posted meanwhile stays buffered (deferred)
|
||||
// and is handled by Idle. enter/exit are the first/last lines of run().
|
||||
class Flow : public Fsm::Machine<Flow>
|
||||
{
|
||||
public:
|
||||
@@ -29,29 +34,29 @@ namespace
|
||||
using Success = int;
|
||||
std::vector<std::string> log;
|
||||
|
||||
struct Busy : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
on.enter([](Flow& m) { m.log.push_back("enter"); });
|
||||
on.exit([](Flow& m) { m.log.push_back("exit"); });
|
||||
on.template defer<Click>();
|
||||
on.template event<Done>().template to<Idle>();
|
||||
}
|
||||
struct States {
|
||||
struct Busy : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
auto& m = self();
|
||||
m.log.push_back("enter");
|
||||
co_await await_event<Done>(); // Click buffered
|
||||
m.log.push_back("exit");
|
||||
co_return next<Idle>();
|
||||
}
|
||||
};
|
||||
struct Idle : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
co_await await_event<Click>(); // buffered Click
|
||||
self().log.push_back("click");
|
||||
co_return next<End>();
|
||||
}
|
||||
};
|
||||
struct End : State {
|
||||
Task<StateKey> run() { co_return succeed(1); }
|
||||
};
|
||||
};
|
||||
struct Idle : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
on.template event<Click>()
|
||||
.action([](Flow& m, const Click&) {
|
||||
m.log.push_back("click");
|
||||
})
|
||||
.template to<End>();
|
||||
}
|
||||
};
|
||||
struct End : State {
|
||||
Task<Step> run() { co_return succeed(1); }
|
||||
};
|
||||
using States = Fsm::States<Busy, Idle, End>;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
@@ -60,12 +65,12 @@ Void::Testing::Test deferred_event_replays_after_transition()
|
||||
{
|
||||
Flow fsm;
|
||||
fsm.start();
|
||||
fsm.send(Click{}); // deferred while in Busy (not dropped)
|
||||
fsm.send(Done{}); // Busy -> Idle; the held Click is replayed -> End
|
||||
fsm.send(Click{}); // not awaited in Busy → stays buffered (not dropped)
|
||||
fsm.send(Done{}); // Busy -> Idle; Idle consumes the buffered Click -> End
|
||||
const auto outcome = fsm.wait();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 1);
|
||||
// enter/exit actions fired around Busy; the deferred Click was handled.
|
||||
// enter/exit ran around Busy; the buffered Click was handled in Idle.
|
||||
const std::vector<std::string> expected{"enter", "exit", "click"};
|
||||
Void::Testing::expect(fsm.log == expected);
|
||||
}
|
||||
@@ -79,8 +84,8 @@ namespace
|
||||
struct Stop {
|
||||
};
|
||||
|
||||
// Counter handles Tick internally (no exit/enter, no transition); Stop
|
||||
// leaves with the accumulated count.
|
||||
// Counter handles Tick internally (loop, no re-entry); Stop leaves with the
|
||||
// accumulated count.
|
||||
class Counter : public Fsm::Machine<Counter>
|
||||
{
|
||||
public:
|
||||
@@ -89,19 +94,28 @@ namespace
|
||||
int ticks = 0;
|
||||
int enters = 0;
|
||||
|
||||
struct Run : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
on.enter([](Counter& m) { ++m.enters; });
|
||||
on.template event<Tick>().internal(
|
||||
[](Counter& m, const Tick&) { ++m.ticks; });
|
||||
on.template event<Stop>().template to<Done>();
|
||||
}
|
||||
struct States {
|
||||
struct Run : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
auto& m = self();
|
||||
++m.enters; // entered exactly once
|
||||
for (;;) {
|
||||
auto ev = co_await await_any<Tick, Stop>();
|
||||
if (std::holds_alternative<Stop>(ev)) {
|
||||
co_return next<Done>();
|
||||
}
|
||||
++m.ticks; // internal: handled, stay here
|
||||
}
|
||||
}
|
||||
};
|
||||
struct Done : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
co_return succeed(self().ticks);
|
||||
}
|
||||
};
|
||||
};
|
||||
struct Done : State {
|
||||
Task<Step> run() { co_return succeed(self().ticks); }
|
||||
};
|
||||
using States = Fsm::States<Run, Done>;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
|
||||
Reference in new issue
Block a user