ServiceRegistry::provide<I> cross-cast to IService via dynamic_cast, which fails to compile when I is a plain (non-polymorphic) value type. Guard the cross-cast with std::is_polymorphic_v<I>; non-polymorphic services are stored and resolvable but carry no lifecycle aspect. Test added. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
132 lines
3.4 KiB
C++
132 lines
3.4 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_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"});
|
|
}
|