Files
Library/tests/standalone/Fsm/Events.cpp
T
faumarayandClaude Fable 5 e941a48007 FSM v2 (A): CRTP typed access, States<> auto-registration, lifecycle, observers
Maintainability core, breaking the state API (all FSM tests migrated):
- Machine<Derived, ResultT> (CRTP). State::self() returns Derived& — drops
  the static_cast<Derived&>(machine()) boilerplate from every state.
- `using States = Fsm::States<...>` auto-registers every state in the base
  ctor (first entry = initial unless `using Initial = S`); no more manual
  add<S>()/initial<S>() for static machines. next<S>() static_asserts S is
  in the list (unregistered targets are now a COMPILE error, not a runtime
  fail); next_dynamic<S>() + add<S>()/add_if<S>() remain for dynamic
  machines.
- Per-state lifecycle: StateBase gains virtual on_enter()/on_exit()
  (Coro::Task<void>, default no-op) run around run(); the engine visit is
  enter -> run -> exit, so entry/exit actions split out of run(). run()
  now has a default (fails) so reactive states (Phase B) needn't define it.
- Observability: virtual on_transition(from,to) (default logs via Void::Log)
  + observe(fn) called on every transition (and the initial entry) with a
  Transition{from,to,step}. state_path()/state_names() added.

Migrated Basic/Events/Nesting/Serialize/Concurrency + the Orchestrator and
service-hot-reload tests to CRTP + self() + States<>; new Lifecycle suite
(enter/run/exit ordering, observer edges, default-run failure). 968 green,
ASan+UBSan clean, -Werror clean.

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

116 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, int>
{
public:
int total = 0;
std::vector<int>* order = nullptr;
struct Waiting : State {
Task<Step> run() override
{
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, int>(
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());
}