Files
Library/tests/internal/FsmServiceHotReload.cpp
faumaray 053ee5b296
CI / GCC 16 / C++26 / sanitizers (push) Failing after 4s
CI / GCC 16 / C++26 / werror (push) Failing after 3s
CI / GCC 16 / C++26 / default (push) Failing after 1m48s
update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:42:52 +03:00

239 lines
7.5 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Fsm/Orchestrator.hpp>
#include <Void/Plugin/Manager.hpp>
#include <Void/Service/PluginService.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include "ServiceInterface.hpp"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <filesystem>
#include <memory>
#include <mutex>
#include <string>
#include <thread>
#include <vector>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::State;
using Void::Fsm::StateKey;
#if defined(VOID_SERVICE_V1_PATH) && defined(VOID_SERVICE_V2_PATH) && \
__has_include(<dlfcn.h>)
namespace
{
void install(const std::filesystem::path& src,
const std::filesystem::path& dst)
{
auto staging = dst;
staging += ".incoming";
std::filesystem::copy_file(
src, staging, std::filesystem::copy_options::overwrite_existing);
std::filesystem::rename(staging, dst);
}
// An FSM that drives the plugin-provided ICalc service: each Work step
// accumulates and loops until a target total is reached. It resolves
// the service every step, so it picks up a reloaded implementation.
class Accumulate : public Void::Fsm::Machine<Accumulate>
{
public:
using Success = int;
struct Work : State {
Task<StateKey> run()
{
auto& svc = service<VoidTest::ICalc>();
const int total = svc.accumulate(1);
if (total >= 500) {
co_return succeed(total);
}
co_return next<Work>();
}
};
Accumulate() { initial<Work>(); } // own worker
};
} // namespace
// A plugin provides the ICalc service; an FSM (via the orchestrator's
// registry) calls it continuously while the plugin is hot-reloaded v1<->v2
// under load. The running total survives every migration and the FSM keeps
// progressing to completion.
Void::Testing::Test internal_fsm_service_hot_reload_under_load()
{
Void::FileSystem::TempDir tmp{"void_svcplugin"};
const auto so = tmp.path() / "calc.so";
std::filesystem::copy_file(VOID_SERVICE_V1_PATH, so);
Void::Fsm::Orchestrator orch;
Void::Plugin::PluginManager plugins;
Void::Testing::expect(plugins.load(so).has_value());
// Bind the plugin's service into the orchestrator's registry, with
// hot-reload re-binding.
Void::Service::PluginServiceBinding<VoidTest::ICalc> binding{
plugins, orch.services(), "CalcPlugin"};
Void::Testing::expect(binding.bind());
// Launch the FSM; it resolves ICalc each step.
auto handle = orch.launch<Accumulate>();
Void::Testing::expect(static_cast<bool>(handle));
// Hammer reloads v1<->v2 while the FSM runs.
std::atomic<bool> stop{false};
std::thread reloader([&] {
int i = 0;
while (!stop.load(std::memory_order_relaxed)) {
const char* src =
(i % 2 == 0) ? VOID_SERVICE_V2_PATH : VOID_SERVICE_V1_PATH;
install(src, so);
if (plugins.reload("CalcPlugin").has_value()) {
(void)binding.bind(); // re-register the new instance
}
++i;
std::this_thread::sleep_for(std::chrono::milliseconds{3});
}
});
// Wait for the FSM to finish (it accumulates to >= 500).
for (int i = 0; i < 1000 && !handle.finished(); ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds{2});
}
stop.store(true);
reloader.join();
Void::Testing::expect(handle.status() == Void::Fsm::Status::Succeeded);
// The accumulated total survived migrations: a fresh resolve sees a
// total at least as large as the FSM's completion threshold.
auto calc = orch.services().get<VoidTest::ICalc>();
Void::Testing::expect(calc != nullptr);
Void::Testing::expect(calc->accumulate(0) >= 500);
orch.stop_all();
}
// The seamless-swap path without load: bind v1, reload to v2, confirm the
// service the FSM resolves is now v2 with the v1 total migrated.
Void::Testing::Test internal_service_reload_migrates_and_swaps()
{
Void::FileSystem::TempDir tmp{"void_svcplugin"};
const auto so = tmp.path() / "calc.so";
std::filesystem::copy_file(VOID_SERVICE_V1_PATH, so);
Void::Service::ServiceRegistry registry;
Void::Plugin::PluginManager plugins;
Void::Testing::expect(plugins.load(so).has_value());
Void::Service::PluginServiceBinding<VoidTest::ICalc> binding{
plugins, registry, "CalcPlugin"};
Void::Testing::expect(binding.bind());
auto v1 = registry.get<VoidTest::ICalc>();
Void::Testing::expect_eq(v1->version(), 1);
v1->accumulate(40); // total = 40
install(VOID_SERVICE_V2_PATH, so);
Void::Testing::expect(plugins.reload("CalcPlugin").has_value());
Void::Testing::expect(binding.bind());
auto v2 = registry.get<VoidTest::ICalc>();
Void::Testing::expect_eq(v2->version(), 2); // implementation swapped
Void::Testing::expect_eq(v2->accumulate(0), 40); // state migrated
}
// A validator rejection during the automatic reload callback must preserve
// the last accepted service instead of publishing the rejected candidate.
Void::Testing::Test plugin_service_reload_validation_preserves_last_good()
{
Void::FileSystem::TempDir tmp{"void_svcplugin_validate"};
const auto so = tmp.path() / "calc.so";
std::filesystem::copy_file(VOID_SERVICE_V1_PATH, so);
Void::Service::ServiceRegistry registry;
Void::Plugin::PluginManager plugins;
Void::Testing::expect(plugins.load(so).has_value());
Void::Service::PluginServiceBinding<VoidTest::ICalc> binding{
plugins, registry, "CalcPlugin",
[](const std::shared_ptr<VoidTest::ICalc>& service) {
return service->version() == 1;
}};
Void::Testing::expect(binding.bind());
binding.enable_hot_reload();
install(VOID_SERVICE_V2_PATH, so);
Void::Testing::expect(plugins.reload("CalcPlugin").has_value());
auto service = registry.get<VoidTest::ICalc>();
Void::Testing::expect(service != nullptr);
Void::Testing::expect_eq(service->version(), 1);
}
// PluginManager intentionally invokes a copied callback list outside its
// mutex. Destroying a binding after that copy must make its callback inert.
Void::Testing::Test plugin_service_callback_is_safe_after_binding_destruction()
{
Void::FileSystem::TempDir tmp{"void_svcplugin_lifetime"};
const auto so = tmp.path() / "calc.so";
std::filesystem::copy_file(VOID_SERVICE_V1_PATH, so);
Void::Service::ServiceRegistry registry;
Void::Plugin::PluginManager plugins;
Void::Testing::expect(plugins.load(so).has_value());
std::mutex gateMutex;
std::condition_variable gateChanged;
bool callbackEntered = false;
bool releaseCallback = false;
const auto blocker = plugins.on_reloaded([&](std::string_view) {
std::unique_lock lock{gateMutex};
callbackEntered = true;
gateChanged.notify_all();
gateChanged.wait(lock, [&] { return releaseCallback; });
});
auto binding =
std::make_unique<Void::Service::PluginServiceBinding<VoidTest::ICalc>>(
plugins, registry, "CalcPlugin");
Void::Testing::expect(binding->bind());
binding->enable_hot_reload();
install(VOID_SERVICE_V2_PATH, so);
std::atomic<bool> reloadSucceeded{false};
std::thread reloader{[&] {
reloadSucceeded.store(plugins.reload("CalcPlugin").has_value(),
std::memory_order_release);
}};
bool callbackWasObserved = false;
{
std::unique_lock lock{gateMutex};
callbackWasObserved = gateChanged.wait_for(
lock, std::chrono::seconds{5}, [&] { return callbackEntered; });
}
binding.reset();
{
std::lock_guard lock{gateMutex};
releaseCallback = true;
}
gateChanged.notify_all();
reloader.join();
plugins.remove_on_reloaded(blocker);
Void::Testing::expect(callbackWasObserved);
Void::Testing::expect(reloadSucceeded.load(std::memory_order_acquire));
auto service = registry.get<VoidTest::ICalc>();
Void::Testing::expect(service != nullptr);
Void::Testing::expect_eq(service->version(), 1);
}
#endif // service plugins available