#pragma once #include "distributed_host/replicated_kv.hpp" #include #include #include #include #include #include #include #include #include #include namespace DistributedHost { struct ClusterFixtureConfig { /// Required bearer token for every peer and admin route. std::string token{"peer-secret"}; std::size_t max_connections{8}; std::chrono::milliseconds shutdown_grace{ std::chrono::seconds{2}}; }; /// Three-node replicated KV reference over real loopback HTTP listeners. /// /// Each voter owns one `Void::Net::HttpServer`. Peer routes drive the /// deterministic replication core (elect, propose, read, heartbeat); an /// authenticated admin route models partitions for crash/partition /// histories. The transport is real; consensus timing is deterministic. /// /// Routes (all require `Authorization: Bearer `): /// GET /peer/health role/term/commit/applied /// POST /peer/elect become a candidate /// PUT /peer/propose?key=K body = value /// DELETE /peer/propose?key=K erase /// GET /peer/read?key=K consistent quorum read /// POST /peer/heartbeat replicate log and commit index /// POST /admin/partition?node=X&partition=N /// POST /admin/crash?node=X /// POST /admin/restart?node=X class ClusterFixture { public: ClusterFixture(const ClusterFixture&) = delete; ClusterFixture& operator=(const ClusterFixture&) = delete; ClusterFixture(ClusterFixture&&) = delete; ClusterFixture& operator=(ClusterFixture&&) = delete; ~ClusterFixture(); [[nodiscard]] static Void::Result::Result< std::unique_ptr> create(ClusterFixtureConfig config = {}); /// Bind and start every listener. Construction alone opens none. [[nodiscard]] Void::Result::Result start(); /// Bounded, idempotent shutdown of every listener. void stop() noexcept; /// `http://127.0.0.1:` for one voter, or an empty string /// when it is unknown or not listening. [[nodiscard]] std::string endpoint(std::string_view node) const; [[nodiscard]] const std::vector& voters() const noexcept; private: struct PeerServer { std::string id; std::unique_ptr server; }; explicit ClusterFixture(ClusterFixtureConfig config); [[nodiscard]] Void::Result::Result register_routes(PeerServer& peer); ClusterFixtureConfig m_config; ReplicatedKvCluster m_cluster; mutable std::mutex m_mutex; std::vector> m_peers; }; } // namespace DistributedHost