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>
212 lines
5.9 KiB
C++
212 lines
5.9 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::Step;
|
|
using Void::Fsm::Transition;
|
|
using namespace std::chrono_literals;
|
|
|
|
// ── retry / next_or
|
|
// ───────────────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
// Attempt fails the first `fail_n` times, then succeeds. with_retry
|
|
// keeps trying; next_or routes exhaustion to a fallback.
|
|
class Flaky : public Void::Fsm::Machine<Flaky>
|
|
{
|
|
public:
|
|
|
|
using Success = std::string;
|
|
using Failure = Void::Fsm::FsmError;
|
|
|
|
int fail_n = 0;
|
|
int attempts = 0;
|
|
int retries; // how many tries with_retry gets
|
|
|
|
struct Try : State {
|
|
Task<Step> run()
|
|
{
|
|
auto& m = self();
|
|
const bool ok = co_await with_retry(
|
|
m.retries, 1ms, [&m]() -> Task<bool> {
|
|
++m.attempts;
|
|
co_return m.attempts > m.fail_n;
|
|
});
|
|
co_return next_or<Fallback>(
|
|
ok ? succeed(std::string{"ok"})
|
|
: fail("exhausted"));
|
|
}
|
|
};
|
|
|
|
struct Fallback : State {
|
|
Task<Step> run()
|
|
{
|
|
co_return succeed(std::string{"fallback"});
|
|
}
|
|
};
|
|
|
|
using States = Void::Fsm::States<Try, Fallback>;
|
|
|
|
explicit Flaky(int fails, int tries) : fail_n(fails), retries(tries)
|
|
{
|
|
} // own worker
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test retry_eventually_succeeds()
|
|
{
|
|
Flaky fsm{2, 5}; // fails twice, 5 tries available -> succeeds on 3rd
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, std::string{"ok"});
|
|
Void::Testing::expect_eq(fsm.attempts, 3);
|
|
}
|
|
|
|
// @tag integration
|
|
Void::Testing::Test retry_exhausts_routes_to_fallback()
|
|
{
|
|
Flaky fsm{10, 3}; // always fails within 3 tries -> next_or<Fallback>
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, std::string{"fallback"});
|
|
Void::Testing::expect_eq(fsm.attempts, 3);
|
|
}
|
|
|
|
// ── interceptors
|
|
// ──────────────────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
class Pipe : public Void::Fsm::Machine<Pipe>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
struct A : State {
|
|
Task<Step> run() { co_return next<B>(); }
|
|
};
|
|
struct B : State {
|
|
Task<Step> run() { co_return succeed(1); }
|
|
};
|
|
using States = Void::Fsm::States<A, B>;
|
|
Pipe()
|
|
: Void::Fsm::Machine<Pipe>(MachineOptions{.own_worker = false})
|
|
{
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test interceptor_before_after_order()
|
|
{
|
|
Pipe fsm;
|
|
std::vector<std::string> events;
|
|
fsm.add_interceptor(
|
|
{.before =
|
|
[&](const Transition& t) { events.push_back("before:" + t.to); },
|
|
.after =
|
|
[&](std::string_view s) {
|
|
events.push_back("after:" + std::string{s});
|
|
}});
|
|
(void)fsm.run_blocking();
|
|
|
|
// A entered (before:A), A runs+finishes (after:A), B entered
|
|
// (before:B), B runs (after:B).
|
|
const std::vector<std::string> expected{"before:A", "after:A", "before:B",
|
|
"after:B"};
|
|
Void::Testing::expect(events == expected);
|
|
}
|
|
|
|
Void::Testing::Test interceptors_run_in_registration_order()
|
|
{
|
|
Pipe fsm;
|
|
std::vector<int> order;
|
|
fsm.add_interceptor(
|
|
{.before = [&](const Transition&) { order.push_back(1); }});
|
|
fsm.add_interceptor(
|
|
{.before = [&](const Transition&) { order.push_back(2); }});
|
|
(void)fsm.run_blocking();
|
|
// Two transitions (A, B) x two interceptors → 1,2,1,2.
|
|
const std::vector<int> expected{1, 2, 1, 2};
|
|
Void::Testing::expect(order == expected);
|
|
}
|
|
|
|
// ── introspection / diagram
|
|
// ───────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
struct Go {
|
|
};
|
|
struct Cancel {
|
|
};
|
|
|
|
class Flow : public Void::Fsm::Machine<Flow>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
struct Idle : State {
|
|
void transitions(TransitionBuilder& on)
|
|
{
|
|
on.event<Go>()
|
|
.guard([](Flow&, const Go&) { return true; })
|
|
.template to<Active>();
|
|
on.event<Cancel>().template to<Done>();
|
|
on.after(1h).template to<Done>();
|
|
}
|
|
};
|
|
struct Active : State {
|
|
Task<Step> run() { co_return next<Done>(); }
|
|
};
|
|
struct Done : State {
|
|
Task<Step> run() { co_return succeed(0); }
|
|
};
|
|
using States = Void::Fsm::States<Idle, Active, Done>;
|
|
Flow() {}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test describe_lists_states_and_reactive_edges()
|
|
{
|
|
Flow fsm;
|
|
const auto states = fsm.describe();
|
|
Void::Testing::expect_eq(states.size(), std::size_t{3});
|
|
|
|
// Idle is reactive + initial, with Go/Cancel/timeout edges.
|
|
const auto& idle = states[0];
|
|
Void::Testing::expect_eq(idle.name, std::string{"Idle"});
|
|
Void::Testing::expect(idle.reactive);
|
|
Void::Testing::expect(idle.initial);
|
|
Void::Testing::expect_eq(idle.edges.size(), std::size_t{3});
|
|
Void::Testing::expect_eq(idle.edges[0].event, std::string{"Go"});
|
|
Void::Testing::expect_eq(idle.edges[0].target, std::string{"Active"});
|
|
Void::Testing::expect(idle.edges[0].guarded);
|
|
Void::Testing::expect_eq(idle.edges[2].event, std::string{"(timeout)"});
|
|
|
|
// Active is imperative → no declarable edges.
|
|
Void::Testing::expect(!states[1].reactive);
|
|
Void::Testing::expect(states[1].edges.empty());
|
|
}
|
|
|
|
Void::Testing::Test to_mermaid_contains_edges()
|
|
{
|
|
Flow fsm;
|
|
const auto diagram = fsm.to_mermaid();
|
|
Void::Testing::expect(diagram.find("stateDiagram-v2") != std::string::npos);
|
|
Void::Testing::expect(diagram.find("[*] --> Idle") != std::string::npos);
|
|
Void::Testing::expect(diagram.find("Idle --> Active : Go [guard]") !=
|
|
std::string::npos);
|
|
Void::Testing::expect(diagram.find("Idle --> Done : Cancel") !=
|
|
std::string::npos);
|
|
}
|