Files
Library/tests/standalone/Fsm/Direct.cpp
T
faumarayandClaude Fable 5 ec9e466d7a FSM (4): one signature-inferred State (drops Reactive/Continue bases)
There is now a single state base, `State`. The engine infers each state's
role purely from member signatures via P2996 — no `override`, no
framework-mandated names:

  struct Work : State {                          // entry
    Coro::Task<Step> run() { ... }
    Coro::Task<Step> run(Cfg c) { ... }          // startup-entry (launch data)
  };
  struct Idle : State {                          // reactive
    void transitions(TransitionBuilder& on) { on.event<Go>().to<Run>(); }
  };
  struct After : State {                         // continuation
    Coro::Task<Step> ok(ChildSuccess auto s) { use s.value; }
    Coro::Task<Step> no(ChildFailure auto f) { use f.value; }
  };

- Detail/Roles.hpp: ChildSuccess/ChildFailure concepts over boxes, plus the
  reflection selectors (invoke_entry / invoke_startup / invoke_handler /
  dispatch_result / has_transitions / make_driver). At add<S>() a per-state
  driver thunk is stored (reactive table vs entry, by whether S has a
  TransitionBuilder& member); drive() calls it. startMachine<Sub, Next>(data)
  builds a Launch step carrying a launcher (injects data into Sub's initial
  startup-entry) + a dispatcher (dispatch_result<Next, Sub>) that builds the
  ChildSuccess/Failure box and calls Next's matching handler.
- Removed Reactive/Continue<R> bases, StateKind/kind(), StateBase::run/resume
  (only on_enter/on_exit remain virtual). Reactive states keep the same DSL.
- Toolchain note: template-for + member splicing fails inside a coroutine
  body in this GCC build, so the selectors are non-coroutines returning a
  Task<Step> (plain Step wrapped via ready_task). Discovery is
  invocability-based — no parameter-type reflection (P3096). Probe-verified.

All FSM tests migrated to the nameless style (SubMachine: Continue ->
ChildSuccess/Failure handlers + startup-entry seeding; reactive states drop
the Reactive base/override). 995 tests (992 green, 3 skipped), ASan clean,
-Werror clean; lone intermittent failure remains the pre-existing racy
plugin hot-reload-under-load test.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-15 01:20:56 +03:00

287 lines
6.6 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <thread>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::Step;
namespace Fsm = Void::Fsm;
using namespace std::chrono_literals;
// ── fluent events + free jumps ──────────────────────────────────────────────
namespace
{
struct Add {
int n;
};
// Sums two Add events, posted in one fluent send() call.
class Sum : public Fsm::Machine<Sum>
{
public:
using Success = int;
int total = 0;
struct Take : State {
Task<Step> run()
{
auto a = co_await await_event<Add>();
self().total += a.n;
auto b = co_await await_event<Add>();
self().total += b.n;
co_return succeed(self().total);
}
};
using States = Fsm::States<Take>;
};
} // namespace
// @tag integration
Void::Testing::Test send_posts_multiple_events()
{
Sum fsm;
fsm.start();
fsm.send(Add{3}, Add{4}); // one fluent call, two events
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 7);
}
namespace
{
// A and C are not connected by any declared edge; goto_state<C>() jumps
// from A to C freely.
class Hop : public Fsm::Machine<Hop>
{
public:
using Success = int;
struct A : State {
Task<Step> run() { co_return goto_state<C>(); }
};
struct B : State {
Task<Step> run() { co_return succeed(1); }
};
struct C : State {
Task<Step> run() { co_return succeed(3); }
};
using States = Fsm::States<A, B, C>;
};
} // namespace
// @tag integration
Void::Testing::Test goto_state_jumps_to_any_registered_state()
{
Hop fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 3); // went A -> C, skipping B
}
namespace
{
struct Tick {
};
// Loops in Loop on each Tick; an external jump<Done>() redirects at the
// next boundary.
class Jumper : public Fsm::Machine<Jumper>
{
public:
using Success = int;
struct Loop : State {
Task<Step> run()
{
co_await await_event<Tick>();
co_return next<Loop>();
}
};
struct Done : State {
Task<Step> run() { co_return succeed(99); }
};
using States = Fsm::States<Loop, Done>;
explicit Jumper(MachineOptions o = {}) : Fsm::Machine<Jumper>(o) {}
};
} // namespace
Void::Testing::Test jump_redirects_at_next_boundary()
{
Jumper fsm{MachineOptions{.own_worker = false}};
fsm.start();
while (fsm.pump() > 0) {
} // enter Loop, park on Tick
fsm.send(Tick{});
while (fsm.pump() > 0) {
} // Loop -> next<Loop> -> park again
fsm.jump<Jumper::Done>(); // request a jump
fsm.send(Tick{});
while (fsm.pump() > 0) {
} // Loop finishes -> boundary applies the jump -> Done
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 99);
}
// ── lifecycle control ───────────────────────────────────────────────────────
namespace
{
// Increments a machine-owned counter each run (data survives reset()).
class Counter : public Fsm::Machine<Counter>
{
public:
using Success = int;
int runs = 0;
struct Once : State {
Task<Step> run() { co_return succeed(++self().runs); }
};
using States = Fsm::States<Once>;
};
} // namespace
// @tag integration
Void::Testing::Test restart_reruns_the_machine()
{
Counter fsm;
const auto first = fsm.run_blocking();
Void::Testing::expect_eq(*first, 1);
fsm.restart();
const auto second = fsm.wait();
Void::Testing::expect_eq(*second, 2);
}
namespace
{
class Pick : public Fsm::Machine<Pick>
{
public:
using Success = int;
struct A : State {
Task<Step> run() { co_return succeed(1); }
};
struct B : State {
Task<Step> run() { co_return succeed(2); }
};
using States = Fsm::States<A, B>; // initial = A
};
} // namespace
// @tag integration
Void::Testing::Test restart_at_changes_initial_state()
{
Pick fsm;
const auto first = fsm.run_blocking();
Void::Testing::expect_eq(*first, 1); // A
fsm.restart_at<Pick::B>();
const auto second = fsm.wait();
Void::Testing::expect_eq(*second, 2); // B
}
// ── runtime mutation (Idle only) ────────────────────────────────────────────
namespace
{
class Mut : public Fsm::Machine<Mut>
{
public:
using Success = int;
struct A : State {
Task<Step> run() { co_return succeed(1); }
};
struct B : State {
Task<Step> run() { co_return succeed(2); }
};
struct C : State {
Task<Step> run() { co_return succeed(3); }
};
// Not in States<> — added via replace<>.
struct Extra : State {
Task<Step> run() { co_return succeed(7); }
};
using States = Fsm::States<A, B, C>;
};
} // namespace
Void::Testing::Test remove_and_set_initial_before_start()
{
Mut fsm;
Void::Testing::expect(fsm.has_state<Mut::C>());
fsm.remove<Mut::C>();
Void::Testing::expect(!fsm.has_state<Mut::C>());
Void::Testing::expect_eq(fsm.describe().size(), std::size_t{2});
fsm.set_initial<Mut::B>();
const auto outcome = fsm.run_blocking();
Void::Testing::expect_eq(*outcome, 2);
}
Void::Testing::Test replace_swaps_a_state_and_keeps_initial()
{
Mut fsm;
fsm.replace<Mut::A, Mut::Extra>(); // A was initial -> Extra becomes initial
Void::Testing::expect(!fsm.has_state<Mut::A>());
Void::Testing::expect(fsm.has_state<Mut::Extra>());
const auto outcome = fsm.run_blocking();
Void::Testing::expect_eq(*outcome, 7);
}
// ── pause / resume ──────────────────────────────────────────────────────────
namespace
{
struct Go {
};
class Sleeper : public Fsm::Machine<Sleeper>
{
public:
using Success = int;
struct W : State {
Task<Step> run()
{
co_await await_event<Go>();
co_return succeed(5);
}
};
using States = Fsm::States<W>;
};
} // namespace
// @tag integration
Void::Testing::Test pause_holds_events_until_resume()
{
Sleeper fsm;
fsm.start();
fsm.pause();
fsm.post(Go{}); // buffered while paused, not delivered
std::this_thread::sleep_for(20ms);
Void::Testing::expect(!fsm.finished()); // still parked, event held
fsm.resume();
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 5);
}