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>
133 lines
3.4 KiB
C++
133 lines
3.4 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::MachineOptions;
|
|
using Void::Fsm::State;
|
|
using Void::Fsm::StateKey;
|
|
|
|
// Composite sub-states: a parent state runs a child region (run_region) that
|
|
// shares the parent's executor, event mailbox and services. The child's
|
|
// Outcome drives the parent's next transition; a parent-level await_any lets
|
|
// the region react to several event types (cooperative cancel).
|
|
|
|
namespace
|
|
{
|
|
struct Tick {
|
|
};
|
|
struct Cancel {
|
|
};
|
|
|
|
// Child region: waits for either Tick (succeed) or Cancel (fail),
|
|
// handling both at one level via await_any.
|
|
class Work : public Void::Fsm::Machine<Work>
|
|
{
|
|
public:
|
|
|
|
using Success = std::string;
|
|
using Failure = Void::Fsm::FsmError;
|
|
|
|
struct States {
|
|
struct Waiting : State {
|
|
Task<StateKey> run()
|
|
{
|
|
auto ev = co_await await_any<Tick, Cancel>();
|
|
if (std::holds_alternative<Cancel>(ev)) {
|
|
co_return fail("cancelled");
|
|
}
|
|
co_return succeed(std::string{"ticked"});
|
|
}
|
|
};
|
|
};
|
|
|
|
explicit Work(MachineOptions opts = {})
|
|
: Void::Fsm::Machine<Work>(opts)
|
|
{
|
|
}
|
|
};
|
|
|
|
// Parent: a composite Running state enters the Work region and maps its
|
|
// result — success passes the value through, failure routes to a sibling.
|
|
class Job : public Void::Fsm::Machine<Job>
|
|
{
|
|
public:
|
|
|
|
using Success = std::string;
|
|
|
|
struct States {
|
|
struct Running : State {
|
|
Task<StateKey> run()
|
|
{
|
|
auto r = co_await run_region<Work>();
|
|
if (r.has_value()) {
|
|
co_return succeed(*r);
|
|
}
|
|
co_return next<Aborted>();
|
|
}
|
|
};
|
|
struct Aborted : State {
|
|
Task<StateKey> run()
|
|
{
|
|
co_return succeed(std::string{"aborted"});
|
|
}
|
|
};
|
|
};
|
|
|
|
explicit Job(MachineOptions opts = {})
|
|
: Void::Fsm::Machine<Job>(opts)
|
|
{
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test composite_region_result_drives_parent()
|
|
{
|
|
Job fsm;
|
|
fsm.start();
|
|
fsm.post(Tick{}); // region succeeds -> parent passes "ticked" through
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, std::string{"ticked"});
|
|
}
|
|
|
|
// @tag integration
|
|
Void::Testing::Test parent_cancel_routes_to_sibling()
|
|
{
|
|
Job fsm;
|
|
fsm.start();
|
|
fsm.post(Cancel{}); // region fails -> parent transitions to Aborted
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, std::string{"aborted"});
|
|
}
|
|
|
|
// state_path() reports the parent→child chain while a region is active.
|
|
// Embedder mode keeps the single consumer on this thread, so the read is
|
|
// deterministic.
|
|
Void::Testing::Test state_path_reports_parent_child()
|
|
{
|
|
Job fsm{MachineOptions{.own_worker = false}};
|
|
fsm.start();
|
|
|
|
// Drive until the region parks on await_any: parent in Running, child in
|
|
// Waiting.
|
|
while (fsm.pump() > 0) {
|
|
}
|
|
const std::vector<std::string> expected{"Running", "Waiting"};
|
|
Void::Testing::expect(fsm.state_path() == expected);
|
|
|
|
// Release the region and run to completion.
|
|
fsm.post(Tick{});
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, std::string{"ticked"});
|
|
// Region gone → path collapses to the (final) parent state only.
|
|
Void::Testing::expect_eq(fsm.state_path().size(), std::size_t{1});
|
|
}
|