Files
Library/tests/standalone/Service/Registry.cpp
T
faumarayandClaude Fable 5 cfeaf93a53 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>
2026-06-14 00:39:05 +03:00

113 lines
2.8 KiB
C++

#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"});
}