Add Service registry and rewrite Fsm as an inheritance/coroutine workflow
Service module (Void::Service):
- IService base (service_name + on_start/on_stop lifecycle).
- ServiceRegistry: type-keyed container — provide<I>(impl), get<I>()
(nullptr if absent), require<I>() (throws), has<I>(), start_all/
stop_all. Consumers depend only on the interface I and resolve at
runtime (dependency inversion); a concrete that derives IService is
recovered for lifecycle via dynamic_cast even when registered under a
plain interface. shared_mutex; reads are the hot path.
New Void::Fsm — a single inheritance + coroutine workflow FSM replacing
the deleted statechart/compile-time FSMs:
- Derive Machine<ResultT>; declare states as nested structs deriving
State; each implements Coro::Task<Step> run() returning next<S>() /
succeed(...) / fail(...).
- Async by default: a non-template Detail::ExecutionContext owns one
ManualExecutor + worker thread + a lock-free MPSC event queue + a typed
event mailbox; a single consumer touches all mutable state so state
code is lock-free/single-threaded. Embedder mode (own_worker=false)
drives via pump() for deterministic tests.
- Events: co_await await_event<E>() consumes the oldest queued E (typed
mailbox), suspending until one arrives; post<E>(e) is lock-free from
any thread.
- Nesting: co_await run_child<Sub>(args...) runs a child on the same
executor (shared events/services) and yields its Outcome.
- Dynamic: add<S>() / add_if<S>(cond) register states; next<unregistered>
fails; has_state<S>() lets states branch.
- Services: set_services(registry); state.service<I>() resolves through
the injected context.
- Observation: current_state() is a lock-free atomic-index read of an
immutable name table (P2996 identifier_of, fallback to indexed names).
- Persistence: serialize() captures {state name, on_serialize data};
restore() on an un-started machine resumes at the named state.
26 new tests (Service registry; FSM basic/success/fail, events incl.
cross-thread worker + order, nesting incl. failure propagation,
serialize/restore, dynamic registration, service resolution, and a
6-producer x 5k concurrent-post stress + lock-free current_state reader
check). 960 green, ASan+UBSan clean, stable across 10 runs, -Werror clean.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
04cf1998bd
commit
cfeaf93a53
14 files changed
+1744
No files matched your search
@@ -0,0 +1,181 @@
|
||||
#include <Void/Fsm/Fsm.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::MachineOptions;
|
||||
using Void::Fsm::Step;
|
||||
|
||||
namespace
|
||||
{
|
||||
// A linear workflow: Start -> Work (loops 3x) -> success(count).
|
||||
class Counter : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
int count = 0;
|
||||
std::vector<std::string>* log = nullptr;
|
||||
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Counter&>(machine());
|
||||
if (m.log) {
|
||||
m.log->push_back("start");
|
||||
}
|
||||
co_return next<Work>();
|
||||
}
|
||||
};
|
||||
|
||||
struct Work : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Counter&>(machine());
|
||||
++m.count;
|
||||
if (m.log) {
|
||||
m.log->push_back("work" + std::to_string(m.count));
|
||||
}
|
||||
if (m.count >= 3) {
|
||||
co_return succeed(m.count);
|
||||
}
|
||||
co_return next<Work>();
|
||||
}
|
||||
};
|
||||
|
||||
Counter()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>();
|
||||
add<Work>();
|
||||
}
|
||||
};
|
||||
|
||||
// A workflow that fails.
|
||||
class Failing : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
struct Boom : State {
|
||||
Task<Step> run() override { co_return fail("kaboom"); }
|
||||
};
|
||||
|
||||
Failing()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Boom>();
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test fsm_runs_to_success_with_result()
|
||||
{
|
||||
std::vector<std::string> log;
|
||||
Counter fsm;
|
||||
fsm.log = &log;
|
||||
const auto outcome = fsm.run_blocking();
|
||||
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 3);
|
||||
Void::Testing::expect(fsm.status() == Void::Fsm::Status::Succeeded);
|
||||
|
||||
const std::vector<std::string> expected{"start", "work1", "work2", "work3"};
|
||||
Void::Testing::expect(log == expected);
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_failure_outcome()
|
||||
{
|
||||
Failing fsm;
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(!outcome.has_value());
|
||||
Void::Testing::expect_eq(outcome.error().message, std::string{"kaboom"});
|
||||
Void::Testing::expect(fsm.status() == Void::Fsm::Status::Failed);
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_pump_drives_to_completion()
|
||||
{
|
||||
Counter fsm;
|
||||
fsm.start();
|
||||
Void::Testing::expect(!fsm.finished()); // driver queued, not yet run
|
||||
fsm.pump(); // Start -> Work* -> success
|
||||
Void::Testing::expect(fsm.finished());
|
||||
Void::Testing::expect_eq(*fsm.wait(), 3);
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_transition_to_unregistered_fails()
|
||||
{
|
||||
struct Lonely : Void::Fsm::Machine<void> {
|
||||
struct Other : State {
|
||||
Task<Step> run() override { co_return succeed(); }
|
||||
};
|
||||
struct Start : State {
|
||||
Task<Step> run() override { co_return next<Other>(); }
|
||||
};
|
||||
Lonely()
|
||||
: Void::Fsm::Machine<void>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>(); // Other intentionally not registered
|
||||
}
|
||||
} fsm;
|
||||
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(!outcome.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_void_result_succeeds()
|
||||
{
|
||||
struct Trivial : Void::Fsm::Machine<void> {
|
||||
struct Done : State {
|
||||
Task<Step> run() override { co_return succeed(); }
|
||||
};
|
||||
Trivial()
|
||||
: Void::Fsm::Machine<void>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Done>();
|
||||
}
|
||||
} fsm;
|
||||
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_dynamic_state_registration()
|
||||
{
|
||||
// Two configurations of the same machine type: with and without the
|
||||
// optional Express state.
|
||||
struct Flow : Void::Fsm::Machine<int> {
|
||||
bool express;
|
||||
struct Express : State {
|
||||
Task<Step> run() override { co_return succeed(99); }
|
||||
};
|
||||
struct Normal : State {
|
||||
Task<Step> run() override { co_return succeed(1); }
|
||||
};
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Flow&>(machine());
|
||||
if (m.has_state<Express>()) {
|
||||
co_return next<Express>();
|
||||
}
|
||||
co_return next<Normal>();
|
||||
}
|
||||
};
|
||||
explicit Flow(bool e)
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false}),
|
||||
express(e)
|
||||
{
|
||||
initial<Start>();
|
||||
add<Normal>();
|
||||
add_if<Express>(express);
|
||||
}
|
||||
};
|
||||
|
||||
Flow with{true};
|
||||
Void::Testing::expect_eq(*with.run_blocking(), 99);
|
||||
|
||||
Flow without{false};
|
||||
Void::Testing::expect_eq(*without.run_blocking(), 1);
|
||||
}
|
||||
Reference in new issue
Block a user