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>
134 lines
3.5 KiB
C++
134 lines
3.5 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::State;
|
|
using Void::Fsm::StateKey;
|
|
namespace Fsm = Void::Fsm;
|
|
|
|
// 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
|
|
{
|
|
struct Click {
|
|
};
|
|
struct Done {
|
|
};
|
|
|
|
// 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:
|
|
|
|
using Success = int;
|
|
std::vector<std::string> log;
|
|
|
|
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); }
|
|
};
|
|
};
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test deferred_event_replays_after_transition()
|
|
{
|
|
Flow fsm;
|
|
fsm.start();
|
|
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 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);
|
|
}
|
|
|
|
// ── internal transition ─────────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
struct Tick {
|
|
};
|
|
struct Stop {
|
|
};
|
|
|
|
// Counter handles Tick internally (loop, no re-entry); Stop leaves with the
|
|
// accumulated count.
|
|
class Counter : public Fsm::Machine<Counter>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
int ticks = 0;
|
|
int enters = 0;
|
|
|
|
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);
|
|
}
|
|
};
|
|
};
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test internal_transition_stays_in_state()
|
|
{
|
|
Counter fsm;
|
|
fsm.start();
|
|
fsm.send(Tick{}, Tick{}, Tick{}); // handled internally, no re-entry
|
|
fsm.send(Stop{});
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 3); // three internal ticks
|
|
Void::Testing::expect_eq(fsm.enters, 1); // entered Run exactly once
|
|
}
|