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

118 lines
2.7 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;
namespace
{
struct Input {
int value;
};
struct Stop {
};
// Waits for Input events, accumulating until a Stop event arrives.
class Accumulator : public Void::Fsm::Machine<Accumulator>
{
public:
using Success = int;
int total = 0;
std::vector<int>* order = nullptr;
struct Waiting : State {
Task<Step> run()
{
auto& m = self();
// Two typed mailboxes: pull whichever arrives.
auto in = co_await await_event<Input>();
m.total += in.value;
if (m.order) {
m.order->push_back(in.value);
}
if (in.value == 0) {
co_return succeed(m.total);
}
co_return next<Waiting>();
}
};
explicit Accumulator(bool own_worker)
: Void::Fsm::Machine<Accumulator>(
MachineOptions{.own_worker = own_worker})
{
initial<Waiting>();
}
};
} // namespace
Void::Testing::Test fsm_await_event_buffered_then_consumed()
{
// Post before pumping — events buffer in the typed mailbox.
Accumulator fsm{false};
fsm.start();
fsm.post(Input{5});
fsm.post(Input{7});
fsm.post(Input{0}); // terminator
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 12);
}
Void::Testing::Test fsm_await_event_preserves_order()
{
std::vector<int> order;
Accumulator fsm{false};
fsm.order = &order;
fsm.start();
fsm.post(Input{1});
fsm.post(Input{2});
fsm.post(Input{3});
fsm.post(Input{0});
(void)fsm.run_blocking();
const std::vector<int> expected{1, 2, 3, 0};
Void::Testing::expect(order == expected);
}
// @tag integration
Void::Testing::Test fsm_await_event_cross_thread_with_worker()
{
Accumulator fsm{true}; // own worker thread
fsm.start();
// Feed events from another thread while the worker awaits them.
std::thread producer([&] {
for (int i = 1; i <= 4; ++i) {
fsm.post(Input{i});
std::this_thread::sleep_for(std::chrono::milliseconds{2});
}
fsm.post(Input{0});
});
const auto outcome = fsm.wait(); // blocks until Stop terminator
producer.join();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 10); // 1+2+3+4
}
Void::Testing::Test fsm_current_state_during_event_wait()
{
Accumulator fsm{false};
fsm.start();
fsm.pump(); // runs Waiting::run until it suspends on await_event<Input>
Void::Testing::expect_eq(fsm.current_state(), std::string{"Waiting"});
Void::Testing::expect(!fsm.finished());
fsm.post(Input{0});
(void)fsm.wait();
Void::Testing::expect(fsm.finished());
}