Files
Library/tests/standalone/Fsm/SubMachine.cpp
T
faumarayandClaude Fable 5 b34226369a FSM (2): declarative sub-machine continuations (startMachine + Continue<R>)
A state can now launch a full sub-FSM and have the engine resume into a
named state that handles the result, instead of awaiting+branching by hand:

    co_return startMachine<Login, AfterLogin>(user, tries);

    struct AfterLogin : Continue<bool> {        // Login = Machine<Login,bool>
      Task<Step> on_success(bool ok) override {
        co_return ok ? next<Home>() : next<Locked>(); }
      Task<Step> on_failure(FsmError e) override { co_return fail(e.message); }
    };

- Sub runs on the parent's shared executor/event-mailbox/services (reuses
  run_child); seed data is forwarded to Sub's constructor.
- The engine dispatches on its own: on entry with a pending result it calls
  the state's resume(), which Continue<R> overrides to pick
  on_success(R)/on_failure(FsmError) by the outcome. Continue<void> takes a
  nullary on_success(). A Continue handler may itself return another
  startMachine (chaining) or any Step.
- Type-safe: startMachine<Sub, Next> static_asserts Next derives
  Continue<Sub::result_type> and is in States<>.

Mechanics: Step gains Kind::Launch carrying the target + a type-erased
launcher (a coroutine that runs the sub and maps its Outcome to a
LaunchResult); drive() awaits it, stashes the result, and the entered
state's resume() consumes it. StateBase::resume defaults to run().

5 new tests (SubMachine): success/failure dispatch, void-result nullary
on_success, chained startMachine through Continue states, and that the sub
shares the parent's event mailbox. 987 green, ASan+UBSan clean, stable
across 10 runs, -Werror clean.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-14 15:37:40 +03:00

216 lines
4.8 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
using Void::Coro::Task;
using Void::Fsm::FsmError;
using Void::Fsm::Step;
namespace Fsm = Void::Fsm;
// startMachine<Sub, Next>(args...) launches a full sub-FSM on the parent's
// shared executor/events/services, then enters Next — a Continue<Sub::result>
// state — and the engine dispatches to on_success / on_failure by the outcome.
namespace
{
// Sub-machine seeded by constructor arg: doubles `seed`, fails if negative.
class Doubler : public Fsm::Machine<Doubler, int>
{
public:
int seed;
struct Run : State {
Task<Step> run() override
{
if (self().seed < 0) {
co_return fail("negative");
}
co_return succeed(self().seed * 2);
}
};
using States = Fsm::States<Run>;
explicit Doubler(int s = 0) : seed(s) {}
};
class Calc : public Fsm::Machine<Calc, std::string>
{
public:
int input = 0;
struct Start : State {
Task<Step> run() override
{
co_return startMachine<Doubler, AfterDouble>(
self().input);
}
};
struct AfterDouble : Continue<int> {
Task<Step> on_success(int v) override
{
co_return succeed("doubled:" + std::to_string(v));
}
Task<Step> on_failure(FsmError e) override
{
co_return succeed("failed:" + e.message);
}
};
using States = Fsm::States<Start, AfterDouble>;
explicit Calc(int in) : input(in) {}
};
} // namespace
// @tag integration
Void::Testing::Test submachine_success_dispatches_on_success()
{
Calc fsm{5};
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"doubled:10"});
}
// @tag integration
Void::Testing::Test submachine_failure_dispatches_on_failure()
{
Calc fsm{-1};
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"failed:negative"});
}
// ── void-result sub-machine ─────────────────────────────────────────────────
namespace
{
class Pinger : public Fsm::Machine<Pinger, void>
{
public:
struct Ping : State {
Task<Step> run() override { co_return succeed(); }
};
using States = Fsm::States<Ping>;
};
class Host : public Fsm::Machine<Host, int>
{
public:
struct Go : State {
Task<Step> run() override
{
co_return startMachine<Pinger, Done>();
}
};
struct Done : Continue<void> {
Task<Step> on_success() override { co_return succeed(42); }
};
using States = Fsm::States<Go, Done>;
};
} // namespace
// @tag integration
Void::Testing::Test submachine_void_result_calls_nullary_on_success()
{
Host fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 42);
}
// ── chained startMachine (a Continue state launches another) ─────────────────
namespace
{
class Chain : public Fsm::Machine<Chain, int>
{
public:
struct A : State {
Task<Step> run() override
{
co_return startMachine<Doubler, B>(3); // 3 -> 6
}
};
struct B : Continue<int> {
Task<Step> on_success(int v) override
{
co_return startMachine<Doubler, C>(v); // 6 -> 12
}
};
struct C : Continue<int> {
Task<Step> on_success(int v) override
{
co_return succeed(v);
}
};
using States = Fsm::States<A, B, C>;
};
} // namespace
// @tag integration
Void::Testing::Test submachine_chains_through_continue_states()
{
Chain fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 12);
}
// ── the sub-machine shares the parent's event mailbox ────────────────────────
namespace
{
struct Tick {
};
class WaitSub : public Fsm::Machine<WaitSub, int>
{
public:
struct W : State {
Task<Step> run() override
{
co_await await_event<Tick>();
co_return succeed(7);
}
};
using States = Fsm::States<W>;
};
class Outer : public Fsm::Machine<Outer, int>
{
public:
struct Begin : State {
Task<Step> run() override
{
co_return startMachine<WaitSub, Fin>();
}
};
struct Fin : Continue<int> {
Task<Step> on_success(int v) override
{
co_return succeed(v);
}
};
using States = Fsm::States<Begin, Fin>;
};
} // namespace
// @tag integration
Void::Testing::Test submachine_shares_parent_event_mailbox()
{
Outer fsm;
fsm.start();
fsm.post(Tick{}); // delivered to the sub-machine awaiting on the shared ctx
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 7);
}