#include #include #include #include #include #include #include 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 { public: using Success = int; using Failure = Void::Fsm::FsmError; int total = 0; std::vector trace; struct Idle : Reactive { void transitions(TransitionBuilder& on) override { // Guarded: only positive amounts advance. on.event() .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(); on.event().template to(); on.after(10s).fail("idle timeout"); } }; // Imperative terminal state: reports the accumulated total. struct Done : State { Task run() override { co_return succeed(self().total); } }; using States = Void::Fsm::States; 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 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 { public: using Success = std::string; struct Waiting : State { Task run() override { auto w = co_await await_event_for(15ms); if (w.has_value()) { co_return succeed(std::string{"woken"}); } co_return succeed(std::string{"timed_out"}); } }; using States = Void::Fsm::States; 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 { public: using Success = int; struct Pending : Reactive { void transitions(TransitionBuilder& on) override { on.event().template to(); on.after(15ms).template to(); } }; struct Resolved : State { Task run() override { co_return succeed(1); } }; struct Expired : State { Task run() override { co_return succeed(2); } }; using States = Void::Fsm::States; 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); }