#include #include #include #include #include #include #include #include #include #include #include #include namespace { using Void::Service::ExpectedEpoch; using Void::Service::IManagedService; using Void::Service::IService; using Void::Service::ProviderSet; using Void::Service::ServiceErrorCode; using Void::Service::ServiceLimits; using Void::Service::ServiceRegistry; using Void::Time::ManualClock; struct IA { virtual ~IA() = default; [[nodiscard]] virtual int id() const = 0; }; struct IB { virtual ~IB() = default; [[nodiscard]] virtual int tag() const = 0; }; struct IC { virtual ~IC() = default; [[nodiscard]] virtual int rank() const = 0; }; class TrackedService final : public IManagedService, public IA { public: TrackedService(std::string name, int val, std::vector* log, bool fail_start = false, bool fail_ready = false, bool throw_stop = false) : m_name(std::move(name)), m_val(val), m_log(log), m_fail_start(fail_start), m_fail_ready(fail_ready), m_throw_stop(throw_stop) { } [[nodiscard]] std::string_view service_name() const override { return m_name; } [[nodiscard]] int id() const override { return m_val; } [[nodiscard]] Void::Result::Result start_checked() override { if (m_log != nullptr) { m_log->push_back("start:" + m_name); } if (m_fail_start) { return Void::Result::fail(Void::Service::make_service_error( ServiceErrorCode::start_failed, "start failed for " + m_name)); } return {}; } [[nodiscard]] Void::Result::Result check_readiness() const override { if (m_fail_ready) { return Void::Result::fail(Void::Service::make_service_error( ServiceErrorCode::not_ready, "not ready: " + m_name)); } return {}; } void abort_start() noexcept override { if (m_log != nullptr) { m_log->push_back("abort:" + m_name); } } [[nodiscard]] Void::Result::Result stop_checked() override { if (m_log != nullptr) { m_log->push_back("stop:" + m_name); } if (m_throw_stop) { return Void::Result::fail(Void::Service::make_service_error( ServiceErrorCode::stop_failed, "stop boom: " + m_name)); } return {}; } private: std::string m_name; int m_val; std::vector* m_log; bool m_fail_start; bool m_fail_ready; bool m_throw_stop; }; class DualInterfaceService final : public IManagedService, public IA, public IB { public: explicit DualInterfaceService(std::vector* log) : m_log(log) { } [[nodiscard]] std::string_view service_name() const override { return "Dual"; } [[nodiscard]] int id() const override { return 10; } [[nodiscard]] int tag() const override { return 20; } [[nodiscard]] Void::Result::Result start_checked() override { ++starts; if (m_log != nullptr) m_log->push_back("dual:start"); return {}; } [[nodiscard]] Void::Result::Result stop_checked() override { ++stops; if (m_log != nullptr) m_log->push_back("dual:stop"); return {}; } int starts{0}; int stops{0}; std::vector* m_log; }; class ReentrantDtorProvider final : public IA { public: ReentrantDtorProvider(ServiceRegistry& reg, int val, std::atomic* observations) : m_reg(reg), m_val(val), m_obs(observations) { } ~ReentrantDtorProvider() override { if (auto h = m_reg.acquire(); h) { if (m_obs != nullptr) { m_obs->fetch_add((*h)->id(), std::memory_order_relaxed); } } } [[nodiscard]] int id() const override { return m_val; } private: ServiceRegistry& m_reg; int m_val; std::atomic* m_obs; }; class ReentrantStartService final : public IService, public IB { public: ReentrantStartService(ServiceRegistry& reg, std::atomic* seen) : m_reg(reg), m_seen(seen) { } [[nodiscard]] std::string_view service_name() const override { return "ReentrantStart"; } void on_start() override { if (auto a = m_reg.acquire(); a) { m_seen->store((*a)->id(), std::memory_order_relaxed); } m_reg.provide(std::make_shared(99)); } [[nodiscard]] int tag() const override { return 7; } private: struct SimpleC final : IC { explicit SimpleC(int r) : rank_val(r) {} [[nodiscard]] int rank() const override { return rank_val; } int rank_val; }; ServiceRegistry& m_reg; std::atomic* m_seen; }; } // namespace Void::Testing::Test service_concurrent_remove_acquire_and_reentrant_dtor() { ServiceRegistry reg; std::atomic dtor_observations{0}; reg.provide( std::make_shared(reg, 1, &dtor_observations)); auto held_first = reg.acquire(); Void::Testing::expect(held_first.has_value()); Void::Testing::expect_eq((*held_first)->id(), 1); // Replace with another ReentrantDtorProvider; releasing held_first // later must invoke ~ReentrantDtorProvider cleanly without deadlocking. std::atomic stop{false}; std::thread writer([&] { for (int i = 2; i < 60 && !stop.load(std::memory_order_relaxed); ++i) { reg.provide(std::make_shared( reg, i, &dtor_observations)); if (i % 7 == 0) { (void)reg.remove(); } } }); std::thread reader([&] { for (int i = 0; i < 200; ++i) { if (auto h = reg.acquire(); h) { Void::Testing::expect((*h)->id() >= 1); } } stop.store(true, std::memory_order_relaxed); }); writer.join(); reader.join(); Void::Testing::expect_eq((*held_first)->id(), 1); held_first = Void::Result::fail(Void::Service::make_service_error( ServiceErrorCode::missing_service, "drop")); Void::Testing::expect(dtor_observations.load(std::memory_order_relaxed) >= 0); } Void::Testing::Test service_start_all_allows_reentrant_acquire_and_provide() { ServiceRegistry reg; std::vector log; std::atomic seen{0}; reg.provide(std::make_shared("A", 42, &log)); reg.provide(std::make_shared(reg, &seen)); reg.start_all(); Void::Testing::expect_eq(seen.load(std::memory_order_relaxed), 42); auto c = reg.acquire(); Void::Testing::expect(c.has_value()); Void::Testing::expect_eq((*c)->rank(), 99); } Void::Testing::Test service_dag_ordering_aliases_cycles_and_rollback() { std::vector log; ServiceRegistry reg; // Baseline snapshot that must remain untouched if staged batch fails. { ProviderSet base; base.add("baseline", std::make_shared("base", 100, &log)); auto ep = reg.start_and_publish(std::move(base)); Void::Testing::expect(ep.has_value()); Void::Testing::expect_eq(*ep, std::uint64_t{1}); } log.clear(); // Missing dependency rejected. { ProviderSet bad; bad.add("B", std::make_shared(&log), {std::type_index{typeid(IC)}}); auto prep = reg.prepare_batch(std::move(bad)); Void::Testing::expect(!prep.has_value()); Void::Testing::expect_eq(prep.error().code().value, static_cast( ServiceErrorCode::missing_dependency)); } // Cycle rejected. { ProviderSet cyc; auto dual = std::make_shared(&log); cyc.add("A", std::make_shared("A", 1, &log), {std::type_index{typeid(IB)}}); cyc.add("B", dual, {std::type_index{typeid(IA)}}); auto prep = reg.prepare_batch(std::move(cyc)); Void::Testing::expect(!prep.has_value()); Void::Testing::expect_eq( prep.error().code().value, static_cast(ServiceErrorCode::cycle_detected)); } // Middle node failure aborts failed node and rolls back started nodes in // reverse dependency order, leaving baseline untouched. { struct RankImpl final : IManagedService, IC { RankImpl(std::string n, std::vector* l, bool fail) : impl(std::move(n), 3, l, fail) {} std::string_view service_name() const override { return impl.service_name(); } Void::Result::Result start_checked() override { return impl.start_checked(); } Void::Result::Result check_readiness() const override { return impl.check_readiness(); } void abort_start() noexcept override { impl.abort_start(); } Void::Result::Result stop_checked() override { return impl.stop_checked(); } int rank() const override { return 3; } TrackedService impl; }; auto dual = std::make_shared(&log); ProviderSet fail_batch; // IB depends on IA; IA registered second so DAG must still start IA before IB. fail_batch.add("B", dual, {std::type_index{typeid(IA)}}); fail_batch.add("A", std::make_shared("A", 1, &log)); fail_batch.add( "C", std::make_shared("C", &log, true), {std::type_index{typeid(IB)}}); auto res = reg.start_and_publish(std::move(fail_batch)); Void::Testing::expect(!res.has_value()); Void::Testing::expect_eq( res.error().code().value, static_cast(ServiceErrorCode::start_failed)); // Baseline handle still resolves to id 100. auto active = reg.acquire(); Void::Testing::expect(active.has_value()); Void::Testing::expect_eq((*active)->id(), 100); // Sequence: start:A, dual:start, start:C, abort:C, dual:stop, stop:A. Void::Testing::expect_eq(log.size(), std::size_t{6}); Void::Testing::expect_eq(log[0], std::string{"start:A"}); Void::Testing::expect_eq(log[1], std::string{"dual:start"}); Void::Testing::expect_eq(log[2], std::string{"start:C"}); Void::Testing::expect_eq(log[3], std::string{"abort:C"}); Void::Testing::expect_eq(log[4], std::string{"dual:stop"}); Void::Testing::expect_eq(log[5], std::string{"stop:A"}); } // Alias of one lifecycle object starts/stops ONCE. log.clear(); auto dual = std::make_shared(&log); ProviderSet alias_batch; alias_batch.add("dual.svc", dual); alias_batch.add_alias("dual.svc.alias", dual); auto ep = reg.start_and_publish(std::move(alias_batch)); Void::Testing::expect(ep.has_value()); Void::Testing::expect_eq(dual->starts, 1); Void::Testing::expect_eq(reg.acquire().value()->id(), 10); Void::Testing::expect_eq(reg.acquire().value()->tag(), 20); } Void::Testing::Test service_snapshot_parent_survives_parent_destruction() { std::unique_ptr child; { ServiceRegistry parent; ProviderSet ps; ps.add("A", std::make_shared("A", 77, nullptr)); Void::Testing::expect( parent.start_and_publish(std::move(ps)).has_value()); auto built = ServiceRegistry::from_parent_snapshot(parent.snapshot()); Void::Testing::expect(built.has_value()); child = std::move(*built); } // parent destroyed here! auto h = child->acquire(); Void::Testing::expect(h.has_value()); Void::Testing::expect_eq((*h)->id(), 77); } Void::Testing::Test service_retention_cap_and_close_incomplete_owners() { ManualClock clock; ServiceLimits limits{}; limits.max_retained_generations = 4; ServiceRegistry reg{limits, clock}; std::vector log; std::vector> pinned; for (int gen = 1; gen <= 4; ++gen) { ProviderSet ps; ps.add( "A", std::make_shared( "A" + std::to_string(gen), gen, &log, false, false, gen == 4)); auto ep = reg.start_and_publish(std::move(ps)); Void::Testing::expect(ep.has_value()); auto h = reg.acquire(); Void::Testing::expect(h.has_value()); Void::Testing::expect_eq(h->generation(), *ep); pinned.push_back(std::move(*h)); } // 4 distinct generations pinned -> 5th publication rejected with retention_limit. { ProviderSet ps5; ps5.add("A", std::make_shared("A5", 5, &log)); auto prep5 = reg.prepare_batch(std::move(ps5)); Void::Testing::expect(!prep5.has_value()); Void::Testing::expect_eq( prep5.error().code().value, static_cast(ServiceErrorCode::retention_limit)); } // Dropping oldest handle frees generation 1, allowing generation 5. pinned.erase(pinned.begin()); { ProviderSet ps5; ps5.add("A", std::make_shared("A5", 5, &log)); auto ep5 = reg.start_and_publish(std::move(ps5)); Void::Testing::expect(ep5.has_value()); } // close_and_join closes admission, reports any stop error, and reports // surviving pinned generations in incomplete_owners without freeing them. auto rep = reg.close_and_join(); Void::Testing::expect(rep.has_value()); Void::Testing::expect(!rep->clean()); Void::Testing::expect(!rep->incomplete_owners.empty()); auto post_close = reg.acquire(); Void::Testing::expect(!post_close.has_value()); Void::Testing::expect_eq( post_close.error().code().value, static_cast(ServiceErrorCode::service_stopping)); // Pinned handles remain dereferenceable until dropped. Void::Testing::expect_eq(pinned.front()->id(), 2); }