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>
164 lines
3.7 KiB
C++
164 lines
3.7 KiB
C++
#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;
|
|
|
|
namespace
|
|
{
|
|
// A linear workflow: Start -> Work (loops 3x) -> success(count).
|
|
class Counter : public Void::Fsm::Machine<Counter, int>
|
|
{
|
|
public:
|
|
|
|
int count = 0;
|
|
std::vector<std::string>* log = nullptr;
|
|
|
|
struct Start : State {
|
|
Task<Step> run() override
|
|
{
|
|
if (self().log) {
|
|
self().log->push_back("start");
|
|
}
|
|
co_return next<Work>();
|
|
}
|
|
};
|
|
|
|
struct Work : State {
|
|
Task<Step> run() override
|
|
{
|
|
auto& m = self();
|
|
++m.count;
|
|
if (m.log) {
|
|
m.log->push_back("work" + std::to_string(m.count));
|
|
}
|
|
if (m.count >= 3) {
|
|
co_return succeed(m.count);
|
|
}
|
|
co_return next<Work>();
|
|
}
|
|
};
|
|
|
|
using States = Void::Fsm::States<Start, Work>;
|
|
|
|
Counter()
|
|
: Void::Fsm::Machine<Counter, int>(
|
|
MachineOptions{.own_worker = false})
|
|
{
|
|
}
|
|
};
|
|
|
|
class Failing : public Void::Fsm::Machine<Failing, int>
|
|
{
|
|
public:
|
|
|
|
struct Boom : State {
|
|
Task<Step> run() override { co_return fail("kaboom"); }
|
|
};
|
|
|
|
using States = Void::Fsm::States<Boom>;
|
|
|
|
Failing()
|
|
: Void::Fsm::Machine<Failing, int>(
|
|
MachineOptions{.own_worker = false})
|
|
{
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test fsm_runs_to_success_with_result()
|
|
{
|
|
std::vector<std::string> log;
|
|
Counter fsm;
|
|
fsm.log = &log;
|
|
const auto outcome = fsm.run_blocking();
|
|
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 3);
|
|
Void::Testing::expect(fsm.status() == Void::Fsm::Status::Succeeded);
|
|
|
|
const std::vector<std::string> expected{"start", "work1", "work2", "work3"};
|
|
Void::Testing::expect(log == expected);
|
|
}
|
|
|
|
Void::Testing::Test fsm_failure_outcome()
|
|
{
|
|
Failing fsm;
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(!outcome.has_value());
|
|
Void::Testing::expect_eq(outcome.error().message, std::string{"kaboom"});
|
|
Void::Testing::expect(fsm.status() == Void::Fsm::Status::Failed);
|
|
}
|
|
|
|
Void::Testing::Test fsm_pump_drives_to_completion()
|
|
{
|
|
Counter fsm;
|
|
fsm.start();
|
|
Void::Testing::expect(!fsm.finished());
|
|
fsm.pump();
|
|
Void::Testing::expect(fsm.finished());
|
|
Void::Testing::expect_eq(*fsm.wait(), 3);
|
|
}
|
|
|
|
Void::Testing::Test fsm_void_result_succeeds()
|
|
{
|
|
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<Trivial, void>(
|
|
MachineOptions{.own_worker = false})
|
|
{
|
|
}
|
|
} fsm;
|
|
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
}
|
|
|
|
Void::Testing::Test fsm_dynamic_state_registration()
|
|
{
|
|
// 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); }
|
|
};
|
|
struct Normal : State {
|
|
Task<Step> run() override { co_return succeed(1); }
|
|
};
|
|
struct Start : State {
|
|
Task<Step> run() override
|
|
{
|
|
if (self().has_state<Express>()) {
|
|
co_return next_dynamic<Express>();
|
|
}
|
|
co_return next_dynamic<Normal>();
|
|
}
|
|
};
|
|
explicit Flow(bool e)
|
|
: Void::Fsm::Machine<Flow, int>(
|
|
MachineOptions{.own_worker = false}),
|
|
express(e)
|
|
{
|
|
initial<Start>();
|
|
add<Normal>();
|
|
add_if<Express>(express);
|
|
}
|
|
};
|
|
|
|
Flow with{true};
|
|
Void::Testing::expect_eq(*with.run_blocking(), 99);
|
|
|
|
Flow without{false};
|
|
Void::Testing::expect_eq(*without.run_blocking(), 1);
|
|
}
|