A machine is now Machine<Derived> (dropping the ResultT template param) and
declares its result types as nested types reflected *by name* — Success and
Failure (a struct or a using-alias), each defaulting to void:
class Login : public Machine<Login> {
using Success = Token; // or a nested struct; default void
struct Failure { int code; };
};
- Outcome = std::expected<Success, Failure> (void failure -> std::monostate
error arm). succeed(Success{...}) / fail(Failure{...}) carry the typed
values (nullary when void). Resolved by plain dependent-name lookup
(Detail/Result.hpp), no P2996.
- Engine/system faults (no initial state, unregistered target, no entry)
travel a SEPARATE FsmError channel: status()==Faulted + system_error(),
never the typed Failure. New Step::Kind::Fault carries it.
- CRTP wrinkle: Derived is incomplete at base instantiation, so the engine
stores the result type-erased (std::any) and rebuilds the typed Outcome in
wait()/run_blocking()/take_outcome() — function templates defaulted on
Derived. run_child/run_region/start_machine reconstruct OutcomeOf<Sub> at
the boundary (Sub is complete).
- Step carries Success/Failure payloads as std::any; the reactive DSL
(finish/fail) and Continue<Sub> are reparameterised off the nested types.
Interim: Continue<Sub> keeps the named on_success/on_failure form (Phase 2
replaces it with signature-inferred ChildSuccess/ChildFailure). All FSM
tests migrated to the nested-type form. 995 tests (992 green, 3 skipped),
ASan clean, -Werror clean; the lone intermittent failure is the
pre-existing racy plugin hot-reload-under-load test (flaky on HEAD too).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
126 lines
2.8 KiB
C++
126 lines
2.8 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;
|
|
using Void::Fsm::Transition;
|
|
|
|
namespace
|
|
{
|
|
// Records enter/run/exit for each state into a shared log.
|
|
class Lifecycle : public Void::Fsm::Machine<Lifecycle>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
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>(
|
|
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> {
|
|
struct Stuck : State {
|
|
Task<void> on_enter() override { co_return; }
|
|
// no run() override
|
|
};
|
|
using States = Void::Fsm::States<Stuck>;
|
|
NoRun()
|
|
: Void::Fsm::Machine<NoRun>(MachineOptions{.own_worker = false})
|
|
{
|
|
}
|
|
} fsm;
|
|
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(!outcome.has_value());
|
|
}
|