Files
Library/tests/standalone/Fsm/Reactive.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

177 lines
4.2 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <string>
#include <thread>
#include <vector>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::Step;
using namespace std::chrono_literals;
namespace
{
struct Go {
int amount;
};
struct Stop {
};
// A declarative machine: Idle reacts to events with guards/actions; on
// timeout it bails out.
class Gate : public Void::Fsm::Machine<Gate>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
int total = 0;
std::vector<std::string> trace;
struct Idle : State {
void transitions(TransitionBuilder& on)
{
// Guarded: only positive amounts advance.
on.event<Go>()
.guard(
[](Gate&, const Go& g) { return g.amount > 0; })
.action([](Gate& m, const Go& g) {
m.total += g.amount;
m.trace.push_back("go" +
std::to_string(g.amount));
})
.template to<Idle>();
on.event<Stop>().template to<Done>();
on.after(10s).fail("idle timeout");
}
};
// Imperative terminal state: reports the accumulated total.
struct Done : State {
Task<Step> run() { co_return succeed(self().total); }
};
using States = Void::Fsm::States<Idle, Done>;
Gate() {} // own worker
};
} // namespace
// Mixes a reactive (declarative, guarded) state with an imperative one.
Void::Testing::Test reactive_guard_and_action()
{
Gate fsm;
fsm.start();
fsm.post(Go{5});
fsm.post(Go{-1}); // blocked by guard, dropped
fsm.post(Go{3});
fsm.post(Stop{}); // -> Done -> succeed(total)
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 8); // 5 + 3, the -1 guarded out
const std::vector<std::string> expected{"go5", "go3"};
Void::Testing::expect(fsm.trace == expected);
}
// ── timeouts
// ──────────────────────────────────────────────────────────────────
namespace
{
struct Wake {
};
// Imperative state using await_event_for: times out → expired.
class Sleeper : public Void::Fsm::Machine<Sleeper>
{
public:
using Success = std::string;
struct Waiting : State {
Task<Step> run()
{
auto w = co_await await_event_for<Wake>(15ms);
if (w.has_value()) {
co_return succeed(std::string{"woken"});
}
co_return succeed(std::string{"timed_out"});
}
};
using States = Void::Fsm::States<Waiting>;
Sleeper() {} // own worker
};
} // namespace
// @tag integration
Void::Testing::Test await_event_for_times_out()
{
Sleeper fsm;
const auto outcome = fsm.run_blocking(); // no Wake posted
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"timed_out"});
}
// @tag integration
Void::Testing::Test await_event_for_receives_event()
{
Sleeper fsm;
fsm.start();
fsm.post(Wake{});
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"woken"});
}
namespace
{
// Reactive timeout transition (on.after().to<>).
class Deadline : public Void::Fsm::Machine<Deadline>
{
public:
using Success = int;
struct Pending : State {
void transitions(TransitionBuilder& on)
{
on.event<Wake>().template to<Resolved>();
on.after(15ms).template to<Expired>();
}
};
struct Resolved : State {
Task<Step> run() { co_return succeed(1); }
};
struct Expired : State {
Task<Step> run() { co_return succeed(2); }
};
using States = Void::Fsm::States<Pending, Resolved, Expired>;
Deadline() {}
};
} // namespace
// @tag integration
Void::Testing::Test reactive_after_transition_fires()
{
Deadline fsm;
const auto outcome = fsm.run_blocking(); // no Wake → Expired
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 2);
}
// @tag integration
Void::Testing::Test reactive_event_beats_timeout()
{
Deadline fsm;
fsm.start();
fsm.post(Wake{}); // arrives before the 15ms deadline
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 1);
}