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);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
#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<long long>
|
||||
{
|
||||
public:
|
||||
|
||||
long long total = 0;
|
||||
long long target;
|
||||
|
||||
struct Run : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Summer&>(machine());
|
||||
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<long long>(), 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<int> {
|
||||
struct Go : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
co_return succeed(service<IDoubler>().twice(21));
|
||||
}
|
||||
};
|
||||
Calc()
|
||||
: Void::Fsm::Machine<int>(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);
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
#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;
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Input {
|
||||
int value;
|
||||
};
|
||||
struct Stop {
|
||||
};
|
||||
|
||||
// Waits for Input events, accumulating until a Stop event arrives.
|
||||
class Accumulator : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
int total = 0;
|
||||
std::vector<int>* order = nullptr;
|
||||
|
||||
struct Waiting : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Accumulator&>(machine());
|
||||
// Two typed mailboxes: pull whichever arrives.
|
||||
auto in = co_await await_event<Input>();
|
||||
m.total += in.value;
|
||||
if (m.order) {
|
||||
m.order->push_back(in.value);
|
||||
}
|
||||
if (in.value == 0) {
|
||||
co_return succeed(m.total);
|
||||
}
|
||||
co_return next<Waiting>();
|
||||
}
|
||||
};
|
||||
|
||||
explicit Accumulator(bool own_worker)
|
||||
: Void::Fsm::Machine<int>(
|
||||
MachineOptions{.own_worker = own_worker})
|
||||
{
|
||||
initial<Waiting>();
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test fsm_await_event_buffered_then_consumed()
|
||||
{
|
||||
// Post before pumping — events buffer in the typed mailbox.
|
||||
Accumulator fsm{false};
|
||||
fsm.start();
|
||||
fsm.post(Input{5});
|
||||
fsm.post(Input{7});
|
||||
fsm.post(Input{0}); // terminator
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 12);
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_await_event_preserves_order()
|
||||
{
|
||||
std::vector<int> order;
|
||||
Accumulator fsm{false};
|
||||
fsm.order = ℴ
|
||||
fsm.start();
|
||||
fsm.post(Input{1});
|
||||
fsm.post(Input{2});
|
||||
fsm.post(Input{3});
|
||||
fsm.post(Input{0});
|
||||
(void)fsm.run_blocking();
|
||||
const std::vector<int> expected{1, 2, 3, 0};
|
||||
Void::Testing::expect(order == expected);
|
||||
}
|
||||
|
||||
// @tag integration
|
||||
Void::Testing::Test fsm_await_event_cross_thread_with_worker()
|
||||
{
|
||||
Accumulator fsm{true}; // own worker thread
|
||||
fsm.start();
|
||||
|
||||
// Feed events from another thread while the worker awaits them.
|
||||
std::thread producer([&] {
|
||||
for (int i = 1; i <= 4; ++i) {
|
||||
fsm.post(Input{i});
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
||||
}
|
||||
fsm.post(Input{0});
|
||||
});
|
||||
|
||||
const auto outcome = fsm.wait(); // blocks until Stop terminator
|
||||
producer.join();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 10); // 1+2+3+4
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_current_state_during_event_wait()
|
||||
{
|
||||
Accumulator fsm{false};
|
||||
fsm.start();
|
||||
fsm.pump(); // runs Waiting::run until it suspends on await_event<Input>
|
||||
Void::Testing::expect_eq(fsm.current_state(), std::string{"Waiting"});
|
||||
Void::Testing::expect(!fsm.finished());
|
||||
fsm.post(Input{0});
|
||||
(void)fsm.wait();
|
||||
Void::Testing::expect(fsm.finished());
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
#include <Void/Fsm/Fsm.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::MachineOptions;
|
||||
using Void::Fsm::Step;
|
||||
|
||||
namespace
|
||||
{
|
||||
// Child machine: computes a value and succeeds.
|
||||
class Subtotal : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
int seed;
|
||||
|
||||
struct Compute : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Subtotal&>(machine());
|
||||
co_return succeed(m.seed * 10);
|
||||
}
|
||||
};
|
||||
|
||||
explicit Subtotal(int s)
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false}),
|
||||
seed(s)
|
||||
{
|
||||
initial<Compute>();
|
||||
}
|
||||
};
|
||||
|
||||
// Child machine that fails.
|
||||
class FailingChild : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
struct Boom : State {
|
||||
Task<Step> run() override { co_return fail("child boom"); }
|
||||
};
|
||||
FailingChild()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Boom>();
|
||||
}
|
||||
};
|
||||
|
||||
// Parent: enters a child, uses its result to decide the outcome.
|
||||
class Parent : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto a = co_await run_child<Subtotal>(3); // 30
|
||||
auto b = co_await run_child<Subtotal>(4); // 40
|
||||
if (!a.has_value() || !b.has_value()) {
|
||||
co_return fail("child failed");
|
||||
}
|
||||
co_return succeed(*a + *b);
|
||||
}
|
||||
};
|
||||
|
||||
Parent()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>();
|
||||
}
|
||||
};
|
||||
|
||||
class ParentOfFailure : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto r = co_await run_child<FailingChild>();
|
||||
if (!r.has_value()) {
|
||||
co_return fail("propagated: " + r.error().message);
|
||||
}
|
||||
co_return succeed(*r);
|
||||
}
|
||||
};
|
||||
|
||||
ParentOfFailure()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Start>();
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test fsm_nested_child_result_drives_parent()
|
||||
{
|
||||
Parent fsm;
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 70); // 30 + 40
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_nested_child_failure_propagates()
|
||||
{
|
||||
ParentOfFailure fsm;
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(!outcome.has_value());
|
||||
Void::Testing::expect_eq(outcome.error().message,
|
||||
std::string{"propagated: child boom"});
|
||||
}
|
||||
|
||||
// @tag integration
|
||||
Void::Testing::Test fsm_nesting_with_worker_thread()
|
||||
{
|
||||
Parent fsm{}; // embedder by default here
|
||||
// Re-run as worker-driven to exercise the shared executor + worker.
|
||||
struct WorkerParent : Void::Fsm::Machine<int> {
|
||||
struct Start : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto a = co_await run_child<Subtotal>(5); // 50
|
||||
co_return succeed(a.value_or(-1));
|
||||
}
|
||||
};
|
||||
WorkerParent() { initial<Start>(); } // own worker
|
||||
} wfsm;
|
||||
const auto outcome = wfsm.run_blocking();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 50);
|
||||
(void)fsm;
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <Void/Fsm/Fsm.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::MachineOptions;
|
||||
using Void::Fsm::Step;
|
||||
|
||||
namespace
|
||||
{
|
||||
// A machine that parks at a named state and exposes its data for
|
||||
// serialize/restore.
|
||||
class Pipeline : public Void::Fsm::Machine<int>
|
||||
{
|
||||
public:
|
||||
|
||||
struct Data {
|
||||
int processed = 0;
|
||||
std::string stage = "init";
|
||||
};
|
||||
Data data;
|
||||
|
||||
struct Ingest : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
co_await await_event<int>(); // park here
|
||||
co_return next<Process>();
|
||||
}
|
||||
};
|
||||
struct Process : State {
|
||||
Task<Step> run() override
|
||||
{
|
||||
auto& m = static_cast<Pipeline&>(machine());
|
||||
m.data.processed += 1;
|
||||
co_return succeed(m.data.processed);
|
||||
}
|
||||
};
|
||||
|
||||
Pipeline()
|
||||
: Void::Fsm::Machine<int>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
initial<Ingest>();
|
||||
add<Process>();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
void on_serialize(Void::Json::Value& out) const override
|
||||
{
|
||||
out = Void::Json::to_json(data);
|
||||
}
|
||||
void on_restore(const Void::Json::Value& in) override
|
||||
{
|
||||
if (auto r = Void::Json::from_json<Data>(in); r.has_value()) {
|
||||
data = std::move(*r);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test fsm_serialize_captures_state_and_data()
|
||||
{
|
||||
Pipeline fsm;
|
||||
fsm.data.processed = 5;
|
||||
fsm.data.stage = "running";
|
||||
fsm.start();
|
||||
fsm.pump(); // enters Ingest, parks on await_event<int>
|
||||
|
||||
const auto json = fsm.serialize();
|
||||
Void::Testing::expect_eq(json["state"].as_string(), std::string{"Ingest"});
|
||||
Void::Testing::expect_eq(json["data"]["processed"].as_int(),
|
||||
std::int64_t{5});
|
||||
Void::Testing::expect_eq(json["data"]["stage"].as_string(),
|
||||
std::string{"running"});
|
||||
fsm.stop();
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_restore_resumes_at_named_state()
|
||||
{
|
||||
const auto saved = Void::Json::parse(
|
||||
R"({"state":"Process","data":{"processed":9,"stage":"mid"}})");
|
||||
Void::Testing::expect(saved.has_value());
|
||||
|
||||
Pipeline fsm;
|
||||
const auto r = fsm.restore(*saved);
|
||||
Void::Testing::expect(r.has_value());
|
||||
|
||||
// Starts directly at Process (skipping Ingest's event wait), with the
|
||||
// restored data → succeeds with processed+1.
|
||||
const auto outcome = fsm.run_blocking();
|
||||
Void::Testing::expect(outcome.has_value());
|
||||
Void::Testing::expect_eq(*outcome, 10);
|
||||
}
|
||||
|
||||
Void::Testing::Test fsm_restore_rejects_unknown_state_and_running()
|
||||
{
|
||||
const auto bogus = Void::Json::parse(R"({"state":"NoSuch","data":{}})");
|
||||
Void::Testing::expect(bogus.has_value());
|
||||
|
||||
Pipeline fsm;
|
||||
Void::Testing::expect(!fsm.restore(*bogus).has_value());
|
||||
|
||||
fsm.start();
|
||||
Void::Testing::expect(!fsm.restore(*bogus).has_value()); // running
|
||||
fsm.stop();
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
#include <Void/Service/Registry.hpp>
|
||||
#include <Void/Service/Service.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using Void::Service::IService;
|
||||
using Void::Service::ServiceRegistry;
|
||||
|
||||
namespace
|
||||
{
|
||||
// A plain interface (does not derive IService) — still resolvable.
|
||||
class IClock
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~IClock() = default;
|
||||
[[nodiscard]] virtual int now() const = 0;
|
||||
};
|
||||
|
||||
class FixedClock final : public IClock
|
||||
{
|
||||
public:
|
||||
|
||||
explicit FixedClock(int t) : m_t(t) {}
|
||||
[[nodiscard]] int now() const override { return m_t; }
|
||||
int m_t;
|
||||
};
|
||||
|
||||
// A lifecycle service (derives IService).
|
||||
class IGreeter
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~IGreeter() = default;
|
||||
[[nodiscard]] virtual std::string greet() const = 0;
|
||||
};
|
||||
|
||||
class Greeter final : public IService, public IGreeter
|
||||
{
|
||||
public:
|
||||
|
||||
explicit Greeter(std::vector<std::string>* log) : m_log(log) {}
|
||||
[[nodiscard]] std::string_view service_name() const override
|
||||
{
|
||||
return "Greeter";
|
||||
}
|
||||
void on_start() override { m_log->push_back("start"); }
|
||||
void on_stop() override { m_log->push_back("stop"); }
|
||||
[[nodiscard]] std::string greet() const override { return "hi"; }
|
||||
std::vector<std::string>* m_log;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test registry_provide_and_get()
|
||||
{
|
||||
ServiceRegistry reg;
|
||||
reg.provide<IClock>(std::make_shared<FixedClock>(42));
|
||||
auto clock = reg.get<IClock>();
|
||||
Void::Testing::expect(clock != nullptr);
|
||||
Void::Testing::expect_eq(clock->now(), 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test registry_missing_get_is_null_require_throws()
|
||||
{
|
||||
ServiceRegistry reg;
|
||||
Void::Testing::expect(reg.get<IClock>() == nullptr);
|
||||
Void::Testing::expect(!reg.has<IClock>());
|
||||
bool threw = false;
|
||||
try {
|
||||
(void)reg.require<IClock>();
|
||||
} catch (const std::exception&) {
|
||||
threw = true;
|
||||
}
|
||||
Void::Testing::expect(threw);
|
||||
}
|
||||
|
||||
Void::Testing::Test registry_require_returns_reference()
|
||||
{
|
||||
ServiceRegistry reg;
|
||||
reg.provide<IClock>(std::make_shared<FixedClock>(7));
|
||||
Void::Testing::expect_eq(reg.require<IClock>().now(), 7);
|
||||
Void::Testing::expect(reg.has<IClock>());
|
||||
}
|
||||
|
||||
Void::Testing::Test registry_replace_provider()
|
||||
{
|
||||
ServiceRegistry reg;
|
||||
reg.provide<IClock>(std::make_shared<FixedClock>(1));
|
||||
reg.provide<IClock>(std::make_shared<FixedClock>(2));
|
||||
Void::Testing::expect_eq(reg.require<IClock>().now(), 2);
|
||||
Void::Testing::expect_eq(reg.size(), std::size_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test registry_lifecycle_order()
|
||||
{
|
||||
std::vector<std::string> log;
|
||||
ServiceRegistry reg;
|
||||
reg.provide<IGreeter>(std::make_shared<Greeter>(&log));
|
||||
reg.start_all();
|
||||
reg.stop_all();
|
||||
Void::Testing::expect_eq(log.size(), std::size_t{2});
|
||||
Void::Testing::expect_eq(log[0], std::string{"start"});
|
||||
Void::Testing::expect_eq(log[1], std::string{"stop"});
|
||||
|
||||
// Resolvable through the IGreeter interface.
|
||||
Void::Testing::expect_eq(reg.require<IGreeter>().greet(),
|
||||
std::string{"hi"});
|
||||
}
|
||||
Reference in new issue
Block a user