Files
Library/tests/standalone/Service/Registry.cpp
T
faumarayandClaude Fable 5 ee4486e1d2 Plugin: discovery, unload, config entry, multi-observer reloads, state API
Host-side additions for applications embedding a plugin system:

- PluginManager::load_directory(dir) loads every shared-library file in
  a directory (filename order) with per-file expected results.
- PluginManager::unload(name) drops the registry entry; the instance
  dies with its last pinned handle (RCU semantics, as on reload).
- Optional C entry void_plugin_configure(inst, config): fed from
  PluginManagerOptions::config_provider after create and after each
  reload swap, before on_load().
- on_reloaded() is now multi-observer: returns a ReloadCallbackId,
  remove_on_reloaded() unregisters. Manual reload() now notifies
  observers too (previously only watched reloads did, contradicting
  the documented contract). PluginServiceBinding deregisters its
  callback on destruction (was a dangling-callback hazard).
- PluginManager::serialize_state/deserialize_state expose the plugin's
  v2 state exports so an embedder can carry plugin state through its
  own hot reload.
- ServiceRegistry::remove<I>() withdraws a provided service (child
  removal re-exposes a parent's entry).
- EventBus::subscribe_scoped<E>() returns a move-only RAII
  Subscription that unsubscribes on destruction.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018yj92cCihyaYtf4gKCT3nv
2026-07-11 23:39:10 +03:00

169 lines
4.6 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;
};
// A plain, non-polymorphic value type registered directly as a service.
struct Config {
int port = 8080;
std::string host = "localhost";
};
} // namespace
Void::Testing::Test registry_provides_non_polymorphic_service()
{
ServiceRegistry reg;
reg.provide<Config>(std::make_shared<Config>(Config{.port = 9000}));
auto cfg = reg.get<Config>();
Void::Testing::expect(cfg != nullptr);
Void::Testing::expect_eq(cfg->port, 9000);
Void::Testing::expect(reg.has<Config>());
// No IService lifecycle aspect, but start_all/stop_all must not choke.
reg.start_all();
reg.stop_all();
}
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_remove_withdraws_entry()
{
ServiceRegistry reg;
auto clock = std::make_shared<FixedClock>(7);
reg.provide<IClock>(clock);
// A consumer resolved before the removal keeps its instance.
auto held = reg.get<IClock>();
Void::Testing::expect(reg.remove<IClock>());
Void::Testing::expect(!reg.has<IClock>());
Void::Testing::expect(reg.get<IClock>() == nullptr);
Void::Testing::expect_eq(reg.size(), std::size_t{0});
Void::Testing::expect_eq(held->now(), 7);
// Removing again reports nothing to remove.
Void::Testing::expect(!reg.remove<IClock>());
// Re-providing after removal works (fresh lifecycle position).
reg.provide<IClock>(std::make_shared<FixedClock>(9));
Void::Testing::expect_eq(reg.require<IClock>().now(), 9);
}
Void::Testing::Test registry_remove_in_child_reexposes_parent()
{
ServiceRegistry parent;
parent.provide<IClock>(std::make_shared<FixedClock>(1));
ServiceRegistry child{&parent};
child.provide<IClock>(std::make_shared<FixedClock>(2));
Void::Testing::expect_eq(child.require<IClock>().now(), 2);
// Removing the child's shadow re-exposes the parent's entry.
Void::Testing::expect(child.remove<IClock>());
Void::Testing::expect_eq(child.require<IClock>().now(), 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"});
}