342 lines
12 KiB
C++
342 lines
12 KiB
C++
#include <Void/Plugin/Manager.hpp>
|
|
#include <Void/Plugin/Loader.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include "Stateful.hpp"
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <filesystem>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
using namespace Void::Plugin;
|
|
|
|
#if defined(VOID_STATEFUL_V1_PATH) && defined(VOID_STATEFUL_V2_PATH) && \
|
|
__has_include(<dlfcn.h>)
|
|
|
|
namespace
|
|
{
|
|
VoidTest::IStateful* stateful(const PluginHandle& h)
|
|
{
|
|
return VoidTest::as_stateful(h.get());
|
|
}
|
|
|
|
// Install a plugin version by atomic rename (fresh inode). Overwriting
|
|
// a mapped .so in place would corrupt the running instance's mapping
|
|
// (SIGBUS) — deployments must swap the file, not rewrite it.
|
|
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);
|
|
}
|
|
} // namespace
|
|
|
|
Void::Testing::Test manager_loads_and_acquires()
|
|
{
|
|
PluginManager mgr;
|
|
const auto name = mgr.load(VOID_STATEFUL_V1_PATH);
|
|
Void::Testing::expect(name.has_value());
|
|
Void::Testing::expect_eq(*name, std::string{"StatefulPlugin"});
|
|
|
|
auto h = mgr.acquire("StatefulPlugin");
|
|
Void::Testing::expect(static_cast<bool>(h));
|
|
Void::Testing::expect_eq(stateful(h)->schema_version(), 1);
|
|
stateful(h)->bump();
|
|
stateful(h)->bump();
|
|
Void::Testing::expect_eq(stateful(h)->counter(), 2);
|
|
}
|
|
|
|
Void::Testing::Test manager_unknown_plugin_yields_empty_handle()
|
|
{
|
|
PluginManager mgr;
|
|
Void::Testing::expect(!static_cast<bool>(mgr.acquire("nope")));
|
|
Void::Testing::expect(mgr.reload("nope").error() ==
|
|
LoadError::UnknownPlugin);
|
|
}
|
|
|
|
Void::Testing::Test manager_candidate_validator_rejects_initial_load()
|
|
{
|
|
int validations = 0;
|
|
PluginManager manager{
|
|
{.candidate_validator = [&](const PluginHandle& candidate) {
|
|
++validations;
|
|
Void::Testing::expect(static_cast<bool>(candidate));
|
|
Void::Testing::expect(candidate.raw_symbol(kInfoSymbol) != nullptr);
|
|
return false;
|
|
}}};
|
|
const auto loaded = manager.load(VOID_STATEFUL_V1_PATH);
|
|
Void::Testing::expect(!loaded.has_value());
|
|
Void::Testing::expect(loaded.error() == LoadError::ActivationFailed);
|
|
Void::Testing::expect_eq(validations, 1);
|
|
Void::Testing::expect(!manager.acquire("StatefulPlugin"));
|
|
}
|
|
|
|
Void::Testing::Test manager_candidate_validator_rejects_reload_transactionally()
|
|
{
|
|
bool accept = true;
|
|
PluginManager manager{
|
|
{.candidate_validator = [&](const PluginHandle&) { return accept; }}};
|
|
Void::Testing::require(manager.load(VOID_STATEFUL_V1_PATH).has_value());
|
|
auto before = manager.acquire("StatefulPlugin");
|
|
stateful(before)->bump();
|
|
accept = false;
|
|
const auto replaced =
|
|
manager.replace("StatefulPlugin", VOID_STATEFUL_V2_PATH);
|
|
Void::Testing::expect(!replaced.has_value());
|
|
Void::Testing::expect(replaced.error() == LoadError::ActivationFailed);
|
|
auto after = manager.acquire("StatefulPlugin");
|
|
Void::Testing::expect_eq(stateful(after)->schema_version(), 1);
|
|
Void::Testing::expect_eq(stateful(after)->counter(), 1);
|
|
}
|
|
|
|
Void::Testing::Test manager_reload_migrates_v1_state_to_v2()
|
|
{
|
|
// Copy v1 into a temp path we control, then overwrite with v2 and
|
|
// reload — exactly the in-place upgrade a deployment performs.
|
|
Void::FileSystem::TempDir tmp{"void_mgr"};
|
|
const auto so = tmp.path() / "plugin.so";
|
|
std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, so);
|
|
|
|
PluginManager mgr;
|
|
const auto name = mgr.load(so);
|
|
Void::Testing::expect(name.has_value());
|
|
|
|
{
|
|
auto h = mgr.acquire("StatefulPlugin");
|
|
Void::Testing::expect_eq(stateful(h)->schema_version(), 1);
|
|
stateful(h)->bump();
|
|
stateful(h)->bump();
|
|
stateful(h)->bump(); // count = 3
|
|
}
|
|
|
|
install(VOID_STATEFUL_V2_PATH, so);
|
|
const auto reloaded = mgr.reload("StatefulPlugin");
|
|
Void::Testing::expect(reloaded.has_value());
|
|
|
|
auto h = mgr.acquire("StatefulPlugin");
|
|
Void::Testing::expect_eq(stateful(h)->schema_version(), 2);
|
|
Void::Testing::expect_eq(stateful(h)->counter(), 3); // migrated
|
|
}
|
|
|
|
Void::Testing::Test manager_replace_migrates_from_immutable_new_path()
|
|
{
|
|
PluginManager manager;
|
|
Void::Testing::require(manager.load(VOID_STATEFUL_V1_PATH).has_value());
|
|
auto pinned = manager.acquire("StatefulPlugin");
|
|
stateful(pinned)->bump();
|
|
stateful(pinned)->bump();
|
|
auto replaced = manager.replace("StatefulPlugin", VOID_STATEFUL_V2_PATH);
|
|
Void::Testing::require(replaced.has_value());
|
|
Void::Testing::expect_eq(stateful(pinned)->schema_version(), 1);
|
|
auto current = manager.acquire("StatefulPlugin");
|
|
Void::Testing::expect_eq(stateful(current)->schema_version(), 2);
|
|
Void::Testing::expect_eq(stateful(current)->counter(), 2);
|
|
}
|
|
|
|
Void::Testing::Test manager_replace_failure_keeps_current_generation()
|
|
{
|
|
PluginManager manager;
|
|
Void::Testing::require(manager.load(VOID_STATEFUL_V1_PATH).has_value());
|
|
auto before = manager.acquire("StatefulPlugin");
|
|
stateful(before)->bump();
|
|
#ifdef VOID_TEST_PLUGIN_PATH
|
|
auto mismatch = manager.replace("StatefulPlugin", VOID_TEST_PLUGIN_PATH);
|
|
Void::Testing::expect(!mismatch.has_value());
|
|
Void::Testing::expect(mismatch.error() == LoadError::NameMismatch);
|
|
#endif
|
|
auto missing = manager.replace("StatefulPlugin", "/no/such/plugin.so");
|
|
Void::Testing::expect(!missing.has_value());
|
|
Void::Testing::expect(missing.error() == LoadError::FileNotFound);
|
|
auto after = manager.acquire("StatefulPlugin");
|
|
Void::Testing::expect_eq(stateful(after)->schema_version(), 1);
|
|
Void::Testing::expect_eq(stateful(after)->counter(), 1);
|
|
}
|
|
|
|
Void::Testing::Test manager_pinned_handle_survives_reload()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_mgr"};
|
|
const auto so = tmp.path() / "plugin.so";
|
|
std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, so);
|
|
|
|
PluginManager mgr;
|
|
Void::Testing::expect(mgr.load(so).has_value());
|
|
|
|
// Pin the v1 instance, then reload to v2 while still holding it.
|
|
auto old = mgr.acquire("StatefulPlugin");
|
|
stateful(old)->bump();
|
|
stateful(old)->bump(); // old count = 2
|
|
|
|
install(VOID_STATEFUL_V2_PATH, so);
|
|
Void::Testing::expect(mgr.reload("StatefulPlugin").has_value());
|
|
|
|
// The pinned handle still addresses the live v1 instance (no torn
|
|
// state, no use-after-free — the old .so stays mapped while pinned).
|
|
Void::Testing::expect_eq(stateful(old)->schema_version(), 1);
|
|
Void::Testing::expect_eq(stateful(old)->counter(), 2);
|
|
|
|
// A fresh acquire gets the migrated v2 instance.
|
|
auto fresh = mgr.acquire("StatefulPlugin");
|
|
Void::Testing::expect_eq(stateful(fresh)->schema_version(), 2);
|
|
Void::Testing::expect_eq(stateful(fresh)->counter(), 2);
|
|
}
|
|
|
|
Void::Testing::Test manager_legacy_transfer_state_plugin_still_reloads()
|
|
{
|
|
// The original TestPlugin uses only transfer_state (no v2 protocol);
|
|
// the manager must still reload it via the fallback path.
|
|
#ifdef VOID_TEST_PLUGIN_PATH
|
|
Void::FileSystem::TempDir tmp{"void_mgr"};
|
|
const auto so = tmp.path() / "legacy.so";
|
|
std::filesystem::copy_file(VOID_TEST_PLUGIN_PATH, so);
|
|
|
|
PluginManager mgr;
|
|
const auto name = mgr.load(so);
|
|
Void::Testing::expect(name.has_value());
|
|
Void::Testing::expect(mgr.reload(*name).has_value());
|
|
Void::Testing::expect(static_cast<bool>(mgr.acquire("TestPlugin")));
|
|
#endif
|
|
}
|
|
|
|
// @tag integration
|
|
Void::Testing::Test manager_watch_auto_reloads_on_file_change()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_mgr"};
|
|
const auto so = tmp.path() / "plugin.so";
|
|
std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, so);
|
|
|
|
PluginManager mgr{
|
|
{.poll_interval = std::chrono::milliseconds{10}, .debounce_polls = 2}};
|
|
Void::Testing::expect(mgr.load(so).has_value());
|
|
|
|
std::atomic<int> reloads{0};
|
|
mgr.on_reloaded([&](std::string_view) { ++reloads; });
|
|
mgr.watch("StatefulPlugin");
|
|
|
|
Void::Testing::expect_eq(
|
|
mgr.acquire("StatefulPlugin").info().plugin_version,
|
|
std::string_view{"1.0.0"});
|
|
|
|
// Replace the file with v2 (atomic rename); the watcher reloads it.
|
|
install(VOID_STATEFUL_V2_PATH, so);
|
|
|
|
for (int i = 0; i < 300 && reloads.load() == 0; ++i) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
}
|
|
Void::Testing::expect(reloads.load() >= 1);
|
|
Void::Testing::expect_eq(mgr.acquire("StatefulPlugin")->name(),
|
|
std::string_view{"StatefulPlugin"});
|
|
mgr.unwatch("StatefulPlugin");
|
|
}
|
|
|
|
Void::Testing::Test manager_multiple_reload_observers_coexist()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_mgr"};
|
|
const auto so = tmp.path() / "plugin.so";
|
|
std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, so);
|
|
|
|
PluginManager mgr;
|
|
Void::Testing::expect(mgr.load(so).has_value());
|
|
|
|
int first = 0;
|
|
int second = 0;
|
|
std::string seen;
|
|
const auto id1 = mgr.on_reloaded([&](std::string_view n) {
|
|
++first;
|
|
seen = std::string{n};
|
|
});
|
|
(void)mgr.on_reloaded([&](std::string_view) { ++second; });
|
|
|
|
// Manual reloads notify every observer (not only watched ones).
|
|
install(VOID_STATEFUL_V2_PATH, so);
|
|
Void::Testing::expect(mgr.reload("StatefulPlugin").has_value());
|
|
Void::Testing::expect_eq(first, 1);
|
|
Void::Testing::expect_eq(second, 1);
|
|
Void::Testing::expect_eq(seen, std::string{"StatefulPlugin"});
|
|
|
|
// A removed observer stays silent; the other keeps firing.
|
|
mgr.remove_on_reloaded(id1);
|
|
Void::Testing::expect(mgr.reload("StatefulPlugin").has_value());
|
|
Void::Testing::expect_eq(first, 1);
|
|
Void::Testing::expect_eq(second, 2);
|
|
}
|
|
|
|
Void::Testing::Test manager_unload_drops_entry_but_pinned_handle_survives()
|
|
{
|
|
PluginManager mgr;
|
|
Void::Testing::expect(mgr.load(VOID_STATEFUL_V1_PATH).has_value());
|
|
|
|
auto pinned = mgr.acquire("StatefulPlugin");
|
|
stateful(pinned)->bump();
|
|
|
|
Void::Testing::expect(mgr.unload("StatefulPlugin").has_value());
|
|
// Gone from the registry…
|
|
Void::Testing::expect(!static_cast<bool>(mgr.acquire("StatefulPlugin")));
|
|
Void::Testing::expect(mgr.unload("StatefulPlugin").error() ==
|
|
LoadError::UnknownPlugin);
|
|
// …but the pinned instance still works until the handle drops.
|
|
Void::Testing::expect_eq(stateful(pinned)->counter(), 1);
|
|
}
|
|
|
|
Void::Testing::Test manager_copy_on_load_survives_in_place_overwrite()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_mgr"};
|
|
const auto so = tmp.path() / "plugin.so";
|
|
std::filesystem::copy_file(VOID_STATEFUL_V1_PATH, so);
|
|
|
|
PluginManager mgr{{.copy_on_load = true}};
|
|
Void::Testing::expect(mgr.load(so).has_value());
|
|
|
|
auto h = mgr.acquire("StatefulPlugin");
|
|
stateful(h)->bump();
|
|
|
|
// Overwrite the original file IN PLACE (what a build does). With
|
|
// copy_on_load the mapped image is a private unlinked copy, so the
|
|
// running instance is unaffected.
|
|
std::filesystem::copy_file(
|
|
VOID_STATEFUL_V2_PATH, so,
|
|
std::filesystem::copy_options::overwrite_existing);
|
|
Void::Testing::expect_eq(stateful(h)->schema_version(), 1);
|
|
Void::Testing::expect_eq(stateful(h)->counter(), 1);
|
|
|
|
// Reload still tracks the original path and picks up the new bytes.
|
|
Void::Testing::expect(mgr.reload("StatefulPlugin").has_value());
|
|
auto fresh = mgr.acquire("StatefulPlugin");
|
|
Void::Testing::expect_eq(stateful(fresh)->schema_version(), 2);
|
|
Void::Testing::expect_eq(stateful(fresh)->counter(), 1);
|
|
}
|
|
|
|
Void::Testing::Test manager_state_snapshot_round_trip()
|
|
{
|
|
PluginManager mgr;
|
|
Void::Testing::expect(mgr.load(VOID_STATEFUL_V1_PATH).has_value());
|
|
|
|
auto h = mgr.acquire("StatefulPlugin");
|
|
stateful(h)->bump();
|
|
stateful(h)->bump(); // counter = 2
|
|
|
|
const auto snapshot = mgr.serialize_state("StatefulPlugin");
|
|
Void::Testing::expect(snapshot.has_value());
|
|
Void::Testing::expect_eq(snapshot->version, std::uint32_t{1});
|
|
|
|
stateful(h)->bump(); // counter = 3
|
|
|
|
// Injecting the snapshot restores the earlier state.
|
|
Void::Testing::expect(mgr.deserialize_state(
|
|
"StatefulPlugin", snapshot->json, snapshot->version));
|
|
Void::Testing::expect_eq(stateful(h)->counter(), 2);
|
|
|
|
// Unknown plugins produce no snapshot and reject injection.
|
|
Void::Testing::expect(!mgr.serialize_state("nope").has_value());
|
|
Void::Testing::expect(
|
|
!mgr.deserialize_state("nope", snapshot->json, snapshot->version));
|
|
}
|
|
|
|
#endif // stateful plugins available
|