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>
162 lines
3.4 KiB
C++
162 lines
3.4 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Service/Registry.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::Step;
|
|
|
|
namespace
|
|
{
|
|
struct Tick {
|
|
int n;
|
|
};
|
|
|
|
// Sums many Tick events delivered from several threads, finishing when
|
|
// the running total reaches a target. Single consumer (the worker) so
|
|
// the total is race-free without the FSM holding any lock.
|
|
class Summer : public Void::Fsm::Machine<Summer>
|
|
{
|
|
public:
|
|
|
|
using Success = long long;
|
|
|
|
long long total = 0;
|
|
long long target;
|
|
|
|
struct Run : State {
|
|
Task<Step> run()
|
|
{
|
|
auto& m = self();
|
|
while (m.total < m.target) {
|
|
auto t = co_await await_event<Tick>();
|
|
m.total += t.n;
|
|
}
|
|
co_return succeed(m.total);
|
|
}
|
|
};
|
|
|
|
explicit Summer(long long tgt)
|
|
: Void::Fsm::Machine<Summer>(), target(tgt)
|
|
{
|
|
initial<Run>();
|
|
}
|
|
};
|
|
|
|
// A service the FSM resolves through its injected context.
|
|
class IDoubler
|
|
{
|
|
public:
|
|
|
|
virtual ~IDoubler() = default;
|
|
[[nodiscard]] virtual int twice(int x) const = 0;
|
|
};
|
|
class Doubler final : public IDoubler
|
|
{
|
|
public:
|
|
|
|
[[nodiscard]] int twice(int x) const override { return x * 2; }
|
|
};
|
|
} // namespace
|
|
|
|
// @tag stress, concurrency
|
|
Void::Testing::Test fsm_concurrent_event_posting()
|
|
{
|
|
constexpr int producers = 6;
|
|
constexpr int per_producer = 5000;
|
|
const long long target =
|
|
static_cast<long long>(producers) * per_producer; // each Tick n=1
|
|
|
|
Summer fsm{target};
|
|
fsm.start();
|
|
|
|
std::vector<std::thread> threads;
|
|
threads.reserve(producers);
|
|
for (int p = 0; p < producers; ++p) {
|
|
threads.emplace_back([&fsm] {
|
|
for (int i = 0; i < per_producer; ++i) {
|
|
fsm.post(Tick{1});
|
|
}
|
|
});
|
|
}
|
|
for (auto& t : threads) {
|
|
t.join();
|
|
}
|
|
|
|
const auto outcome = fsm.wait();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, target);
|
|
}
|
|
|
|
// @tag concurrency
|
|
Void::Testing::Test fsm_current_state_reads_never_tear_under_load()
|
|
{
|
|
Summer fsm{1'000'000};
|
|
fsm.start();
|
|
|
|
std::atomic<bool> stop{false};
|
|
std::atomic<long long> reads{0};
|
|
bool sane = true;
|
|
std::vector<std::thread> readers;
|
|
for (int r = 0; r < 3; ++r) {
|
|
readers.emplace_back([&] {
|
|
while (!stop.load(std::memory_order_relaxed)) {
|
|
const auto name = fsm.current_state();
|
|
if (!name.empty() && name != "Run") {
|
|
sane = false;
|
|
}
|
|
++reads;
|
|
}
|
|
});
|
|
}
|
|
|
|
for (int i = 0; i < 200'000; ++i) {
|
|
fsm.post(Tick{1});
|
|
}
|
|
fsm.post(Tick{1'000'000}); // push over target → finish
|
|
|
|
const auto outcome = fsm.wait();
|
|
stop = true;
|
|
for (auto& t : readers) {
|
|
t.join();
|
|
}
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect(sane);
|
|
Void::Testing::expect(reads.load() > 0);
|
|
}
|
|
|
|
Void::Testing::Test fsm_resolves_service_through_context()
|
|
{
|
|
struct Calc : Void::Fsm::Machine<Calc> {
|
|
using Success = int;
|
|
|
|
struct Go : State {
|
|
Task<Step> run()
|
|
{
|
|
co_return succeed(service<IDoubler>().twice(21));
|
|
}
|
|
};
|
|
Calc()
|
|
: Void::Fsm::Machine<Calc>(MachineOptions{.own_worker = false})
|
|
{
|
|
initial<Go>();
|
|
}
|
|
};
|
|
|
|
Void::Service::ServiceRegistry registry;
|
|
registry.provide<IDoubler>(std::make_shared<Doubler>());
|
|
|
|
Calc fsm;
|
|
fsm.set_services(registry);
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 42);
|
|
}
|