FSM v2 (A): CRTP typed access, States<> auto-registration, lifecycle, observers
Maintainability core, breaking the state API (all FSM tests migrated):
- Machine<Derived, ResultT> (CRTP). State::self() returns Derived& — drops
the static_cast<Derived&>(machine()) boilerplate from every state.
- `using States = Fsm::States<...>` auto-registers every state in the base
ctor (first entry = initial unless `using Initial = S`); no more manual
add<S>()/initial<S>() for static machines. next<S>() static_asserts S is
in the list (unregistered targets are now a COMPILE error, not a runtime
fail); next_dynamic<S>() + add<S>()/add_if<S>() remain for dynamic
machines.
- Per-state lifecycle: StateBase gains virtual on_enter()/on_exit()
(Coro::Task<void>, default no-op) run around run(); the engine visit is
enter -> run -> exit, so entry/exit actions split out of run(). run()
now has a default (fails) so reactive states (Phase B) needn't define it.
- Observability: virtual on_transition(from,to) (default logs via Void::Log)
+ observe(fn) called on every transition (and the initial entry) with a
Transition{from,to,step}. state_path()/state_names() added.
Migrated Basic/Events/Nesting/Serialize/Concurrency + the Orchestrator and
service-hot-reload tests to CRTP + self() + States<>; new Lifecycle suite
(enter/run/exit ordering, observer edges, default-run failure). 968 green,
ASan+UBSan clean, -Werror clean.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7b6cd7341a
commit
e941a48007
11 files changed
+355
-117
No files matched your search
@@ -37,7 +37,7 @@ namespace
|
||||
// An FSM that drives the plugin-provided ICalc service: each Work step
|
||||
// accumulates and loops until a target total is reached. It resolves
|
||||
// the service every step, so it picks up a reloaded implementation.
|
||||
class Accumulate : public Void::Fsm::Machine<int>
|
||||
class Accumulate : public Void::Fsm::Machine<Accumulate, int>
|
||||
{
|
||||
public:
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ using Void::Fsm::Step;
|
||||
namespace
|
||||
{
|
||||
// A linear workflow: Start -> Work (loops 3x) -> success(count).
|
||||
class Counter : public Void::Fsm::Machine<int>
|
||||
class Counter : public Void::Fsm::Machine<Counter, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -22,9 +22,8 @@ namespace
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Counter&>(machine());
|
||||
if (m.log) {
|
||||
m.log->push_back("start");
|
||||
if (self().log) {
|
||||
self().log->push_back("start");
|
||||
}
|
||||
co_return next<Work>();
|
||||
}
|
||||
@@ -33,7 +32,7 @@ namespace
|
||||
struct Work : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Counter&>(machine());
|
||||
auto& m = self();
|
||||
++m.count;
|
||||
if (m.log) {
|
||||
m.log->push_back("work" + std::to_string(m.count));
|
||||
@@ -45,16 +44,16 @@ namespace
|
||||
}
|
||||
};
|
||||
|
||||
using States = Void::Fsm::States<Start, Work>;
|
||||
|
||||
Counter()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Counter, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>();
|
||||
add<Work>();
|
||||
}
|
||||
};
|
||||
|
||||
// A workflow that fails.
|
||||
class Failing : public Void::Fsm::Machine<int>
|
||||
class Failing : public Void::Fsm::Machine<Failing, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -62,10 +61,12 @@ namespace
|
||||
Task<Step> run() override { co_return fail("kaboom"); }
|
||||
};
|
||||
|
||||
using States = Void::Fsm::States<Boom>;
|
||||
|
||||
Failing()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Failing, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Boom>();
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -98,42 +99,23 @@ Void::Testing::Test fsm_pump_drives_to_completion()
|
||||
{
|
||||
Counter fsm;
|
||||
fsm.start();
|
||||
Void::Testing::expect(!fsm.finished()); // driver queued, not yet run
|
||||
fsm.pump(); // Start -> Work* -> success
|
||||
Void::Testing::expect(!fsm.finished());
|
||||
fsm.pump();
|
||||
Void::Testing::expect(fsm.finished());
|
||||
Void::Testing::expect_eq(*fsm.wait(), 3);
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_transition_to_unregistered_fails()
|
||||
{
|
||||
struct Lonely : Void::Fsm::Machine<void> {
|
||||
struct Other : State {
|
||||
Task<Step> run() override { co_return succeed(); }
|
||||
};
|
||||
struct Start : State {
|
||||
Task<Step> run() override { co_return next<Other>(); }
|
||||
};
|
||||
Lonely()
|
||||
: Void::Fsm::Machine<void>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>(); // Other intentionally not registered
|
||||
}
|
||||
} fsm;
|
||||
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(!outcome.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_void_result_succeeds()
|
||||
{
|
||||
struct Trivial : Void::Fsm::Machine<void> {
|
||||
struct Trivial : Void::Fsm::Machine<Trivial, void> {
|
||||
struct Done : State {
|
||||
Task<Step> run() override { co_return succeed(); }
|
||||
};
|
||||
using States = Void::Fsm::States<Done>;
|
||||
Trivial()
|
||||
: Void::Fsm::Machine<void>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Trivial, void>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Done>();
|
||||
}
|
||||
} fsm;
|
||||
|
||||
@@ -143,9 +125,9 @@ Void::Testing::Test fsm_void_result_succeeds()
|
||||
|
||||
Void::Testing::Test fsm_dynamic_state_registration()
|
||||
{
|
||||
// Two configurations of the same machine type: with and without the
|
||||
// optional Express state.
|
||||
struct Flow : Void::Fsm::Machine<int> {
|
||||
// Dynamic machine: states registered conditionally, addressed with
|
||||
// next_dynamic<S>() (skips the compile-time States<> check).
|
||||
struct Flow : Void::Fsm::Machine<Flow, int> {
|
||||
bool express;
|
||||
struct Express : State {
|
||||
Task<Step> run() override { co_return succeed(99); }
|
||||
@@ -156,15 +138,15 @@ Void::Testing::Test fsm_dynamic_state_registration()
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Flow&>(machine());
|
||||
if (m.has_state<Express>()) {
|
||||
co_return next<Express>();
|
||||
if (self().has_state<Express>()) {
|
||||
co_return next_dynamic<Express>();
|
||||
}
|
||||
co_return next<Normal>();
|
||||
co_return next_dynamic<Normal>();
|
||||
}
|
||||
};
|
||||
explicit Flow(bool e)
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false}),
|
||||
: Void::Fsm::Machine<Flow, int>(
|
||||
MachineOptions{.own_worker = false}),
|
||||
express(e)
|
||||
{
|
||||
initial<Start>();
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace
|
||||
// Sums many Tick events delivered from several threads, finishing when
|
||||
// the running total reaches a target. Single consumer (the worker) so
|
||||
// the total is race-free without the FSM holding any lock.
|
||||
class Summer : public Void::Fsm::Machine<long long>
|
||||
class Summer : public Void::Fsm::Machine<Summer, long long>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace
|
||||
struct Run : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Summer&>(machine());
|
||||
auto& m = self();
|
||||
while (m.total < m.target) {
|
||||
auto t = co_await await_event<Tick>();
|
||||
m.total += t.n;
|
||||
@@ -42,7 +42,7 @@ namespace
|
||||
};
|
||||
|
||||
explicit Summer(long long tgt)
|
||||
: Void::Fsm::Machine<long long>(), target(tgt)
|
||||
: Void::Fsm::Machine<Summer, long long>(), target(tgt)
|
||||
{
|
||||
initial<Run>();
|
||||
}
|
||||
@@ -132,7 +132,7 @@ Void::Testing::Test fsm_current_state_reads_never_tear_under_load()
|
||||
|
||||
Void::Testing::Test fsm_resolves_service_through_context()
|
||||
{
|
||||
struct Calc : Void::Fsm::Machine<int> {
|
||||
struct Calc : Void::Fsm::Machine<Calc, int> {
|
||||
struct Go : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
@@ -140,7 +140,8 @@ Void::Testing::Test fsm_resolves_service_through_context()
|
||||
}
|
||||
};
|
||||
Calc()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Calc, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Go>();
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ namespace
|
||||
};
|
||||
|
||||
// Waits for Input events, accumulating until a Stop event arrives.
|
||||
class Accumulator : public Void::Fsm::Machine<int>
|
||||
class Accumulator : public Void::Fsm::Machine<Accumulator, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace
|
||||
struct Waiting : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Accumulator&>(machine());
|
||||
auto& m = self();
|
||||
// Two typed mailboxes: pull whichever arrives.
|
||||
auto in = co_await await_event<Input>();
|
||||
m.total += in.value;
|
||||
@@ -45,7 +45,7 @@ namespace
|
||||
};
|
||||
|
||||
explicit Accumulator(bool own_worker)
|
||||
: Void::Fsm::Machine<int>(
|
||||
: Void::Fsm::Machine<Accumulator, int>(
|
||||
MachineOptions{.own_worker = own_worker})
|
||||
{
|
||||
initial<Waiting>();
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
#include <Void/Fsm/Fsm.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::MachineOptions;
|
||||
using Void::Fsm::Step;
|
||||
using Void::Fsm::Transition;
|
||||
|
||||
namespace
|
||||
{
|
||||
// Records enter/run/exit for each state into a shared log.
|
||||
class Lifecycle : public Void::Fsm::Machine<Lifecycle, int>
|
||||
{
|
||||
public:
|
||||
|
||||
std::vector<std::string> log;
|
||||
|
||||
struct A : State {
|
||||
Task<void> on_enter() override
|
||||
{
|
||||
self().log.push_back("enter A");
|
||||
co_return;
|
||||
}
|
||||
Task<Step> run() override
|
||||
{
|
||||
self().log.push_back("run A");
|
||||
co_return next<B>();
|
||||
}
|
||||
Task<void> on_exit() override
|
||||
{
|
||||
self().log.push_back("exit A");
|
||||
co_return;
|
||||
}
|
||||
};
|
||||
|
||||
struct B : State {
|
||||
Task<void> on_enter() override
|
||||
{
|
||||
self().log.push_back("enter B");
|
||||
co_return;
|
||||
}
|
||||
Task<Step> run() override
|
||||
{
|
||||
self().log.push_back("run B");
|
||||
co_return succeed(1);
|
||||
}
|
||||
Task<void> on_exit() override
|
||||
{
|
||||
self().log.push_back("exit B");
|
||||
co_return;
|
||||
}
|
||||
};
|
||||
|
||||
using States = Void::Fsm::States<A, B>;
|
||||
|
||||
Lifecycle()
|
||||
: Void::Fsm::Machine<Lifecycle, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test fsm_lifecycle_enter_run_exit_order()
|
||||
{
|
||||
Lifecycle fsm;
|
||||
(void)fsm.run_blocking();
|
||||
|
||||
const std::vector<std::string> expected{"enter A", "run A", "exit A",
|
||||
"enter B", "run B", "exit B"};
|
||||
Void::Testing::expect(fsm.log == expected);
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_observer_sees_every_transition()
|
||||
{
|
||||
Lifecycle fsm;
|
||||
std::vector<std::string> edges;
|
||||
fsm.observe([&](const Transition& t) {
|
||||
edges.push_back((t.from.empty() ? "*" : t.from) + "->" + t.to);
|
||||
});
|
||||
(void)fsm.run_blocking();
|
||||
|
||||
// Initial entry (*->A), then A->B.
|
||||
const std::vector<std::string> expected{"*->A", "A->B"};
|
||||
Void::Testing::expect(edges == expected);
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_state_names_and_path()
|
||||
{
|
||||
Lifecycle fsm;
|
||||
const auto& names = fsm.state_names();
|
||||
Void::Testing::expect_eq(names.size(), std::size_t{2});
|
||||
Void::Testing::expect_eq(names[0], std::string{"A"});
|
||||
Void::Testing::expect_eq(names[1], std::string{"B"});
|
||||
|
||||
fsm.start();
|
||||
fsm.pump();
|
||||
Void::Testing::expect(fsm.finished());
|
||||
}
|
||||
|
||||
// A state defining only on_enter (no run) falls through to the default
|
||||
// run() which fails — confirms the default body exists.
|
||||
Void::Testing::Test fsm_state_without_run_fails()
|
||||
{
|
||||
struct NoRun : Void::Fsm::Machine<NoRun, void> {
|
||||
struct Stuck : State {
|
||||
Task<void> on_enter() override { co_return; }
|
||||
// no run() override
|
||||
};
|
||||
using States = Void::Fsm::States<Stuck>;
|
||||
NoRun()
|
||||
: Void::Fsm::Machine<NoRun, void>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
}
|
||||
} fsm;
|
||||
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(!outcome.has_value());
|
||||
}
|
||||
@@ -11,7 +11,7 @@ using Void::Fsm::Step;
|
||||
namespace
|
||||
{
|
||||
// Child machine: computes a value and succeeds.
|
||||
class Subtotal : public Void::Fsm::Machine<int>
|
||||
class Subtotal : public Void::Fsm::Machine<Subtotal, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -20,13 +20,14 @@ namespace
|
||||
struct Compute : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Subtotal&>(machine());
|
||||
auto& m = self();
|
||||
co_return succeed(m.seed * 10);
|
||||
}
|
||||
};
|
||||
|
||||
explicit Subtotal(int s)
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false}),
|
||||
: Void::Fsm::Machine<Subtotal, int>(
|
||||
MachineOptions{.own_worker = false}),
|
||||
seed(s)
|
||||
{
|
||||
initial<Compute>();
|
||||
@@ -34,7 +35,7 @@ namespace
|
||||
};
|
||||
|
||||
// Child machine that fails.
|
||||
class FailingChild : public Void::Fsm::Machine<int>
|
||||
class FailingChild : public Void::Fsm::Machine<FailingChild, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -42,14 +43,15 @@ namespace
|
||||
Task<Step> run() override { co_return fail("child boom"); }
|
||||
};
|
||||
FailingChild()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<FailingChild, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Boom>();
|
||||
}
|
||||
};
|
||||
|
||||
// Parent: enters a child, uses its result to decide the outcome.
|
||||
class Parent : public Void::Fsm::Machine<int>
|
||||
class Parent : public Void::Fsm::Machine<Parent, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -66,13 +68,14 @@ namespace
|
||||
};
|
||||
|
||||
Parent()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Parent, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>();
|
||||
}
|
||||
};
|
||||
|
||||
class ParentOfFailure : public Void::Fsm::Machine<int>
|
||||
class ParentOfFailure : public Void::Fsm::Machine<ParentOfFailure, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -88,7 +91,8 @@ namespace
|
||||
};
|
||||
|
||||
ParentOfFailure()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<ParentOfFailure, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>();
|
||||
}
|
||||
@@ -117,7 +121,7 @@ Void::Testing::Test fsm_nesting_with_worker_thread()
|
||||
{
|
||||
Parent fsm{}; // embedder by default here
|
||||
// Re-run as worker-driven to exercise the shared executor + worker.
|
||||
struct WorkerParent : Void::Fsm::Machine<int> {
|
||||
struct WorkerParent : Void::Fsm::Machine<WorkerParent, int> {
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace
|
||||
{
|
||||
// A machine that parks at a named state and exposes its data for
|
||||
// serialize/restore.
|
||||
class Pipeline : public Void::Fsm::Machine<int>
|
||||
class Pipeline : public Void::Fsm::Machine<Pipeline, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -33,14 +33,15 @@ namespace
|
||||
struct Process : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Pipeline&>(machine());
|
||||
auto& m = self();
|
||||
m.data.processed += 1;
|
||||
co_return succeed(m.data.processed);
|
||||
}
|
||||
};
|
||||
|
||||
Pipeline()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
: Void::Fsm::Machine<Pipeline, int>(
|
||||
MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Ingest>();
|
||||
add<Process>();
|
||||
|
||||
@@ -66,7 +66,7 @@ namespace
|
||||
};
|
||||
|
||||
// An FSM that bumps the counter service N times then succeeds.
|
||||
class Worker : public Void::Fsm::Machine<int>
|
||||
class Worker : public Void::Fsm::Machine<Worker, int>
|
||||
{
|
||||
public:
|
||||
|
||||
@@ -75,7 +75,7 @@ namespace
|
||||
struct Run : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Worker&>(machine());
|
||||
auto& m = self();
|
||||
int last = 0;
|
||||
for (int i = 0; i < m.times; ++i) {
|
||||
last = service<ICounter>().inc();
|
||||
@@ -85,7 +85,8 @@ namespace
|
||||
};
|
||||
|
||||
explicit Worker(int t)
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false}),
|
||||
: Void::Fsm::Machine<Worker, int>(
|
||||
MachineOptions{.own_worker = false}),
|
||||
times(t)
|
||||
{
|
||||
initial<Run>();
|
||||
@@ -109,7 +110,7 @@ Void::Testing::Test orchestrator_launches_and_supervises()
|
||||
{
|
||||
// Worker-backed FSM so the orchestrator's start() spins a worker and
|
||||
// the FSM runs to completion on its own.
|
||||
struct AsyncWorker : Void::Fsm::Machine<int> {
|
||||
struct AsyncWorker : Void::Fsm::Machine<AsyncWorker, int> {
|
||||
struct Run : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user