- run_region<Sub>() — run_child plus introspection: shares the parent's executor/event mailbox/services, the calling state's on_enter/on_exit wrap it, and the parent's state_path() reports the parent->child chain while it runs. The caller maps the child's Outcome to a sibling transition. - await_any<Es...>() — typed multi-event await yielding std::variant<Es...>, so one state handles several event types at a single level (parent-level watching alongside a child region's own events). - state_path() now appends an active region child's path (consumer- consistent; deterministic in pump() mode). current_state() stays the lock-free cross-thread read. Coroutines aren't preemptible mid-flight, so composite cancellation is cooperative: the region awaits a shared Cancel event (events route to the deepest awaiter) and returns a failing Outcome the parent maps — no frame is abandoned. Documented in CLAUDE.md. 3 new tests (Hierarchy): region result drives the parent transition, parent cancel routes to a sibling, state_path reports parent->child. 982 green, ASan+UBSan clean, stable across 10 runs, -Werror clean. Completes the FSM v2 plan (A-D). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
125 lines
3.4 KiB
C++
125 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::Step;
|
|
|
|
// 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, std::string>
|
|
{
|
|
public:
|
|
|
|
struct Waiting : State {
|
|
Task<Step> run() override
|
|
{
|
|
auto ev = co_await await_any<Tick, Cancel>();
|
|
if (std::holds_alternative<Cancel>(ev)) {
|
|
co_return fail("cancelled");
|
|
}
|
|
co_return succeed(std::string{"ticked"});
|
|
}
|
|
};
|
|
using States = Void::Fsm::States<Waiting>;
|
|
|
|
explicit Work(MachineOptions opts = {})
|
|
: Void::Fsm::Machine<Work, std::string>(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, std::string>
|
|
{
|
|
public:
|
|
|
|
struct Running : State {
|
|
Task<Step> run() override
|
|
{
|
|
auto r = co_await run_region<Work>();
|
|
if (r.has_value()) {
|
|
co_return succeed(*r);
|
|
}
|
|
co_return next<Aborted>();
|
|
}
|
|
};
|
|
struct Aborted : State {
|
|
Task<Step> run() override
|
|
{
|
|
co_return succeed(std::string{"aborted"});
|
|
}
|
|
};
|
|
using States = Void::Fsm::States<Running, Aborted>;
|
|
|
|
explicit Job(MachineOptions opts = {})
|
|
: Void::Fsm::Machine<Job, std::string>(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});
|
|
}
|