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>
111 lines
2.7 KiB
C++
111 lines
2.7 KiB
C++
#include <Void/Data/Reflect.hpp>
|
|
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::Step;
|
|
|
|
namespace
|
|
{
|
|
// A machine that parks at a named state and exposes its data for
|
|
// serialize/restore.
|
|
class Pipeline : public Void::Fsm::Machine<Pipeline, int>
|
|
{
|
|
public:
|
|
|
|
struct Data {
|
|
int processed = 0;
|
|
std::string stage = "init";
|
|
};
|
|
Data data;
|
|
|
|
struct Ingest : State {
|
|
Task<Step> run() override
|
|
{
|
|
co_await await_event<int>(); // park here
|
|
co_return next<Process>();
|
|
}
|
|
};
|
|
struct Process : State {
|
|
Task<Step> run() override
|
|
{
|
|
auto& m = self();
|
|
m.data.processed += 1;
|
|
co_return succeed(m.data.processed);
|
|
}
|
|
};
|
|
|
|
Pipeline()
|
|
: Void::Fsm::Machine<Pipeline, int>(
|
|
MachineOptions{.own_worker = false})
|
|
{
|
|
initial<Ingest>();
|
|
add<Process>();
|
|
}
|
|
|
|
protected:
|
|
|
|
void on_serialize(Void::Json::Value& out) const override
|
|
{
|
|
out = Void::Json::to_json(data);
|
|
}
|
|
void on_restore(const Void::Json::Value& in) override
|
|
{
|
|
if (auto r = Void::Json::from_json<Data>(in); r.has_value()) {
|
|
data = std::move(*r);
|
|
}
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test fsm_serialize_captures_state_and_data()
|
|
{
|
|
Pipeline fsm;
|
|
fsm.data.processed = 5;
|
|
fsm.data.stage = "running";
|
|
fsm.start();
|
|
fsm.pump(); // enters Ingest, parks on await_event<int>
|
|
|
|
const auto json = fsm.serialize();
|
|
Void::Testing::expect_eq(json["state"].as_string(), std::string{"Ingest"});
|
|
Void::Testing::expect_eq(json["data"]["processed"].as_int(),
|
|
std::int64_t{5});
|
|
Void::Testing::expect_eq(json["data"]["stage"].as_string(),
|
|
std::string{"running"});
|
|
fsm.stop();
|
|
}
|
|
|
|
Void::Testing::Test fsm_restore_resumes_at_named_state()
|
|
{
|
|
const auto saved = Void::Json::parse(
|
|
R"({"state":"Process","data":{"processed":9,"stage":"mid"}})");
|
|
Void::Testing::expect(saved.has_value());
|
|
|
|
Pipeline fsm;
|
|
const auto r = fsm.restore(*saved);
|
|
Void::Testing::expect(r.has_value());
|
|
|
|
// Starts directly at Process (skipping Ingest's event wait), with the
|
|
// restored data → succeeds with processed+1.
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 10);
|
|
}
|
|
|
|
Void::Testing::Test fsm_restore_rejects_unknown_state_and_running()
|
|
{
|
|
const auto bogus = Void::Json::parse(R"({"state":"NoSuch","data":{}})");
|
|
Void::Testing::expect(bogus.has_value());
|
|
|
|
Pipeline fsm;
|
|
Void::Testing::expect(!fsm.restore(*bogus).has_value());
|
|
|
|
fsm.start();
|
|
Void::Testing::expect(!fsm.restore(*bogus).has_value()); // running
|
|
fsm.stop();
|
|
}
|