Files
Library/tests/standalone/Fsm/Reactive.cpp
T
faumarayandClaude Fable 5 422a7705c5 FSM v2 (B): timers, await_event_for, reactive/declarative states + guards
Engine: ExecutionContext gains a timer set (add_timer/cancel_timer); the
worker parks with try_acquire_until(earliest deadline) instead of a bare
acquire, and fires due timers in pump()/run_loop. The event mailbox waiter
is now multi-type (await any of N event types) and carries an optional
timeout — deliver() matches a waiting state directly (event needn't sit in
a buffer) and cancels the paired timer; the timer's fire resumes the
waiter with timed_out set. Single consumer still owns all mailbox/timer
state, so it stays lock-free for state code.

Machine:
- await_event_for<E>(duration) -> std::optional<E> (nullopt on timeout);
  await_any_event(types, deadline) for reactive states; existing
  await_event<E> rebuilt on the multi-type API.
- Reactive state base: instead of run(), a state overrides
  transitions(TransitionBuilder&) declaratively —
  on.event<E>().guard(fn).action(fn).to<S>() / .finish(v) / .fail(msg),
  and on.after(d).to<S>()/.fail(). The framework-provided run() builds the
  table once and loops: await any declared event or the timeout, evaluate
  matching entries in declaration order, check guard, run action,
  transition. Guards = bool(Derived&, const E&), actions sync or
  Coro::Task<void>. to<S>() static-asserts S in States<>. Reactive and
  imperative State subclasses mix freely in one machine.

8 new tests (Reactive): guarded reactive state + action ordering mixed
with an imperative terminal; await_event_for timeout vs event; reactive
after().to<> firing vs event beating the deadline. 973 green, ASan+UBSan
clean, timer tests stable across 12 runs, -Werror clean.

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

173 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, int>
{
public:
int total = 0;
std::vector<std::string> trace;
struct Idle : Reactive {
void transitions(TransitionBuilder& on) override
{
// 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() override
{
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, std::string>
{
public:
struct Waiting : State {
Task<Step> run() override
{
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, int>
{
public:
struct Pending : Reactive {
void transitions(TransitionBuilder& on) override
{
on.event<Wake>().template to<Resolved>();
on.after(15ms).template to<Expired>();
}
};
struct Resolved : State {
Task<Step> run() override { co_return succeed(1); }
};
struct Expired : State {
Task<Step> run() override { 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);
}