A machine is now Machine<Derived> (dropping the ResultT template param) and
declares its result types as nested types reflected *by name* — Success and
Failure (a struct or a using-alias), each defaulting to void:
class Login : public Machine<Login> {
using Success = Token; // or a nested struct; default void
struct Failure { int code; };
};
- Outcome = std::expected<Success, Failure> (void failure -> std::monostate
error arm). succeed(Success{...}) / fail(Failure{...}) carry the typed
values (nullary when void). Resolved by plain dependent-name lookup
(Detail/Result.hpp), no P2996.
- Engine/system faults (no initial state, unregistered target, no entry)
travel a SEPARATE FsmError channel: status()==Faulted + system_error(),
never the typed Failure. New Step::Kind::Fault carries it.
- CRTP wrinkle: Derived is incomplete at base instantiation, so the engine
stores the result type-erased (std::any) and rebuilds the typed Outcome in
wait()/run_blocking()/take_outcome() — function templates defaulted on
Derived. run_child/run_region/start_machine reconstruct OutcomeOf<Sub> at
the boundary (Sub is complete).
- Step carries Success/Failure payloads as std::any; the reactive DSL
(finish/fail) and Continue<Sub> are reparameterised off the nested types.
Interim: Continue<Sub> keeps the named on_success/on_failure form (Phase 2
replaces it with signature-inferred ChildSuccess/ChildFailure). All FSM
tests migrated to the nested-type form. 995 tests (992 green, 3 skipped),
ASan clean, -Werror clean; the lone intermittent failure is the
pre-existing racy plugin hot-reload-under-load test (flaky on HEAD too).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
163 lines
3.8 KiB
C++
163 lines
3.8 KiB
C++
#include <Void/Fsm/Orchestrator.hpp>
|
|
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Service/Service.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::Orchestrator;
|
|
using Void::Fsm::Status;
|
|
using Void::Fsm::Step;
|
|
|
|
namespace
|
|
{
|
|
// A service the FSMs resolve by interface.
|
|
class ICounter
|
|
{
|
|
public:
|
|
|
|
virtual ~ICounter() = default;
|
|
virtual int inc() = 0;
|
|
[[nodiscard]] virtual int value() const = 0;
|
|
};
|
|
|
|
class AtomicCounter final : public Void::Service::IService, public ICounter
|
|
{
|
|
public:
|
|
|
|
explicit AtomicCounter(std::vector<std::string>* log) : m_log(log)
|
|
{
|
|
}
|
|
[[nodiscard]] std::string_view service_name() const override
|
|
{
|
|
return "Counter";
|
|
}
|
|
void on_start() override
|
|
{
|
|
if (m_log) {
|
|
m_log->push_back("counter-start");
|
|
}
|
|
}
|
|
void on_stop() override
|
|
{
|
|
if (m_log) {
|
|
m_log->push_back("counter-stop");
|
|
}
|
|
}
|
|
int inc() override
|
|
{
|
|
return m_v.fetch_add(1, std::memory_order_relaxed) + 1;
|
|
}
|
|
[[nodiscard]] int value() const override
|
|
{
|
|
return m_v.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
std::atomic<int> m_v{0};
|
|
std::vector<std::string>* m_log;
|
|
};
|
|
|
|
// An FSM that bumps the counter service N times then succeeds.
|
|
class Worker : public Void::Fsm::Machine<Worker>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
int times;
|
|
|
|
struct Run : State {
|
|
Task<Step> run() override
|
|
{
|
|
auto& m = self();
|
|
int last = 0;
|
|
for (int i = 0; i < m.times; ++i) {
|
|
last = service<ICounter>().inc();
|
|
}
|
|
co_return succeed(last);
|
|
}
|
|
};
|
|
|
|
explicit Worker(int t)
|
|
: Void::Fsm::Machine<Worker>(
|
|
MachineOptions{.own_worker = false}),
|
|
times(t)
|
|
{
|
|
initial<Run>();
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test orchestrator_injects_services_into_fsm()
|
|
{
|
|
Orchestrator orch;
|
|
orch.services().provide<ICounter>(std::make_shared<AtomicCounter>(nullptr));
|
|
|
|
// An embedder FSM driven directly, using the orchestrator's registry —
|
|
// confirms the same injection path launch() uses.
|
|
Worker w{3};
|
|
w.set_services(orch.services());
|
|
Void::Testing::expect_eq(*w.run_blocking(), 3);
|
|
}
|
|
|
|
Void::Testing::Test orchestrator_launches_and_supervises()
|
|
{
|
|
// Worker-backed FSM so the orchestrator's start() spins a worker and
|
|
// the FSM runs to completion on its own.
|
|
struct AsyncWorker : Void::Fsm::Machine<AsyncWorker> {
|
|
using Success = int;
|
|
|
|
struct Run : State {
|
|
Task<Step> run() override
|
|
{
|
|
int last = 0;
|
|
for (int i = 0; i < 5; ++i) {
|
|
last = service<ICounter>().inc();
|
|
}
|
|
co_return succeed(last);
|
|
}
|
|
};
|
|
AsyncWorker() { initial<Run>(); } // own worker
|
|
};
|
|
|
|
Orchestrator orch;
|
|
auto counter = std::make_shared<AtomicCounter>(nullptr);
|
|
orch.services().provide<ICounter>(counter);
|
|
|
|
auto h = orch.launch<AsyncWorker>();
|
|
Void::Testing::expect(static_cast<bool>(h));
|
|
Void::Testing::expect_eq(orch.running_count(), std::size_t{1});
|
|
|
|
for (int i = 0; i < 200 && !h.finished(); ++i) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
|
}
|
|
Void::Testing::expect(h.status() == Status::Succeeded);
|
|
Void::Testing::expect_eq(counter->value(), 5);
|
|
|
|
orch.stop_all();
|
|
Void::Testing::expect_eq(orch.running_count(), std::size_t{0});
|
|
}
|
|
|
|
Void::Testing::Test orchestrator_service_lifecycle()
|
|
{
|
|
std::vector<std::string> log;
|
|
{
|
|
Orchestrator orch;
|
|
orch.services().provide<ICounter>(
|
|
std::make_shared<AtomicCounter>(&log));
|
|
orch.start_services();
|
|
orch.stop_all();
|
|
}
|
|
Void::Testing::expect_eq(log.size(), std::size_t{2});
|
|
Void::Testing::expect_eq(log[0], std::string{"counter-start"});
|
|
Void::Testing::expect_eq(log[1], std::string{"counter-stop"});
|
|
}
|