Adds imperative ergonomics for driving and reshaping a machine. The engine
isn't preemptible mid-state (coroutines aren't interruptible), so external
controls apply at transition boundaries and mutation is Idle-only — both
documented.
- Fluent events: send(e1, e2, …) variadic post.
- Free runtime jumps: in-state goto_state<S>() reaches any registered state
with no declared transition ("each state can be moved to another state");
external jump<S>() requests a redirect applied by drive() at the next
boundary (race-safe via an atomic, non-preemptive).
- Lifecycle: reset() returns a finished/idle machine to Idle (drops the
owned ctx, clears outcome/current/jump); restart() / restart_at<S>()
re-run; pause() / resume() (ExecutionContext gains a paused flag — the
worker stops draining and pump() no-ops while paused; posted events
buffer until resume()).
- Runtime mutation (Idle only): remove<S>() (re-packs indices,
fixes the initial), set_initial<S>(), replace<S, T>().
8 new tests (Direct): send batch, goto_state skip, boundary jump (embedder
mode), restart / restart_at, remove + set_initial, replace, pause holds
events until resume. 995 green, ASan+UBSan clean, 0 flaky in 15 runs,
-Werror clean. Completes the FSM refactor plan (1-3).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
279 lines
6.6 KiB
C++
279 lines
6.6 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <thread>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::Step;
|
|
namespace Fsm = Void::Fsm;
|
|
using namespace std::chrono_literals;
|
|
|
|
// ── fluent events + free jumps ──────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
struct Add {
|
|
int n;
|
|
};
|
|
|
|
// Sums two Add events, posted in one fluent send() call.
|
|
class Sum : public Fsm::Machine<Sum, int>
|
|
{
|
|
public:
|
|
|
|
int total = 0;
|
|
|
|
struct Take : State {
|
|
Task<Step> run() override
|
|
{
|
|
auto a = co_await await_event<Add>();
|
|
self().total += a.n;
|
|
auto b = co_await await_event<Add>();
|
|
self().total += b.n;
|
|
co_return succeed(self().total);
|
|
}
|
|
};
|
|
using States = Fsm::States<Take>;
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test send_posts_multiple_events()
|
|
{
|
|
Sum fsm;
|
|
fsm.start();
|
|
fsm.send(Add{3}, Add{4}); // one fluent call, two events
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 7);
|
|
}
|
|
|
|
namespace
|
|
{
|
|
// A and C are not connected by any declared edge; goto_state<C>() jumps
|
|
// from A to C freely.
|
|
class Hop : public Fsm::Machine<Hop, int>
|
|
{
|
|
public:
|
|
|
|
struct A : State {
|
|
Task<Step> run() override { co_return goto_state<C>(); }
|
|
};
|
|
struct B : State {
|
|
Task<Step> run() override { co_return succeed(1); }
|
|
};
|
|
struct C : State {
|
|
Task<Step> run() override { co_return succeed(3); }
|
|
};
|
|
using States = Fsm::States<A, B, C>;
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test goto_state_jumps_to_any_registered_state()
|
|
{
|
|
Hop fsm;
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 3); // went A -> C, skipping B
|
|
}
|
|
|
|
namespace
|
|
{
|
|
struct Tick {
|
|
};
|
|
|
|
// Loops in Loop on each Tick; an external jump<Done>() redirects at the
|
|
// next boundary.
|
|
class Jumper : public Fsm::Machine<Jumper, int>
|
|
{
|
|
public:
|
|
|
|
struct Loop : State {
|
|
Task<Step> run() override
|
|
{
|
|
co_await await_event<Tick>();
|
|
co_return next<Loop>();
|
|
}
|
|
};
|
|
struct Done : State {
|
|
Task<Step> run() override { co_return succeed(99); }
|
|
};
|
|
using States = Fsm::States<Loop, Done>;
|
|
|
|
explicit Jumper(MachineOptions o = {})
|
|
: Fsm::Machine<Jumper, int>(o)
|
|
{
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test jump_redirects_at_next_boundary()
|
|
{
|
|
Jumper fsm{MachineOptions{.own_worker = false}};
|
|
fsm.start();
|
|
while (fsm.pump() > 0) {
|
|
} // enter Loop, park on Tick
|
|
fsm.send(Tick{});
|
|
while (fsm.pump() > 0) {
|
|
} // Loop -> next<Loop> -> park again
|
|
|
|
fsm.jump<Jumper::Done>(); // request a jump
|
|
fsm.send(Tick{});
|
|
while (fsm.pump() > 0) {
|
|
} // Loop finishes -> boundary applies the jump -> Done
|
|
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 99);
|
|
}
|
|
|
|
// ── lifecycle control ───────────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
// Increments a machine-owned counter each run (data survives reset()).
|
|
class Counter : public Fsm::Machine<Counter, int>
|
|
{
|
|
public:
|
|
|
|
int runs = 0;
|
|
|
|
struct Once : State {
|
|
Task<Step> run() override
|
|
{
|
|
co_return succeed(++self().runs);
|
|
}
|
|
};
|
|
using States = Fsm::States<Once>;
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test restart_reruns_the_machine()
|
|
{
|
|
Counter fsm;
|
|
const auto first = fsm.run_blocking();
|
|
Void::Testing::expect_eq(*first, 1);
|
|
fsm.restart();
|
|
const auto second = fsm.wait();
|
|
Void::Testing::expect_eq(*second, 2);
|
|
}
|
|
|
|
namespace
|
|
{
|
|
class Pick : public Fsm::Machine<Pick, int>
|
|
{
|
|
public:
|
|
|
|
struct A : State {
|
|
Task<Step> run() override { co_return succeed(1); }
|
|
};
|
|
struct B : State {
|
|
Task<Step> run() override { co_return succeed(2); }
|
|
};
|
|
using States = Fsm::States<A, B>; // initial = A
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test restart_at_changes_initial_state()
|
|
{
|
|
Pick fsm;
|
|
const auto first = fsm.run_blocking();
|
|
Void::Testing::expect_eq(*first, 1); // A
|
|
fsm.restart_at<Pick::B>();
|
|
const auto second = fsm.wait();
|
|
Void::Testing::expect_eq(*second, 2); // B
|
|
}
|
|
|
|
// ── runtime mutation (Idle only) ────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
class Mut : public Fsm::Machine<Mut, int>
|
|
{
|
|
public:
|
|
|
|
struct A : State {
|
|
Task<Step> run() override { co_return succeed(1); }
|
|
};
|
|
struct B : State {
|
|
Task<Step> run() override { co_return succeed(2); }
|
|
};
|
|
struct C : State {
|
|
Task<Step> run() override { co_return succeed(3); }
|
|
};
|
|
// Not in States<> — added via replace<>.
|
|
struct Extra : State {
|
|
Task<Step> run() override { co_return succeed(7); }
|
|
};
|
|
using States = Fsm::States<A, B, C>;
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test remove_and_set_initial_before_start()
|
|
{
|
|
Mut fsm;
|
|
Void::Testing::expect(fsm.has_state<Mut::C>());
|
|
fsm.remove<Mut::C>();
|
|
Void::Testing::expect(!fsm.has_state<Mut::C>());
|
|
Void::Testing::expect_eq(fsm.describe().size(), std::size_t{2});
|
|
|
|
fsm.set_initial<Mut::B>();
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect_eq(*outcome, 2);
|
|
}
|
|
|
|
Void::Testing::Test replace_swaps_a_state_and_keeps_initial()
|
|
{
|
|
Mut fsm;
|
|
fsm.replace<Mut::A, Mut::Extra>(); // A was initial -> Extra becomes initial
|
|
Void::Testing::expect(!fsm.has_state<Mut::A>());
|
|
Void::Testing::expect(fsm.has_state<Mut::Extra>());
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect_eq(*outcome, 7);
|
|
}
|
|
|
|
// ── pause / resume ──────────────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
struct Go {
|
|
};
|
|
|
|
class Sleeper : public Fsm::Machine<Sleeper, int>
|
|
{
|
|
public:
|
|
|
|
struct W : State {
|
|
Task<Step> run() override
|
|
{
|
|
co_await await_event<Go>();
|
|
co_return succeed(5);
|
|
}
|
|
};
|
|
using States = Fsm::States<W>;
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test pause_holds_events_until_resume()
|
|
{
|
|
Sleeper fsm;
|
|
fsm.start();
|
|
fsm.pause();
|
|
fsm.post(Go{}); // buffered while paused, not delivered
|
|
|
|
std::this_thread::sleep_for(20ms);
|
|
Void::Testing::expect(!fsm.finished()); // still parked, event held
|
|
|
|
fsm.resume();
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 5);
|
|
}
|