Plugin: discovery, unload, config entry, multi-observer reloads, state API

Host-side additions for applications embedding a plugin system:

- PluginManager::load_directory(dir) loads every shared-library file in
  a directory (filename order) with per-file expected results.
- PluginManager::unload(name) drops the registry entry; the instance
  dies with its last pinned handle (RCU semantics, as on reload).
- Optional C entry void_plugin_configure(inst, config): fed from
  PluginManagerOptions::config_provider after create and after each
  reload swap, before on_load().
- on_reloaded() is now multi-observer: returns a ReloadCallbackId,
  remove_on_reloaded() unregisters. Manual reload() now notifies
  observers too (previously only watched reloads did, contradicting
  the documented contract). PluginServiceBinding deregisters its
  callback on destruction (was a dangling-callback hazard).
- PluginManager::serialize_state/deserialize_state expose the plugin's
  v2 state exports so an embedder can carry plugin state through its
  own hot reload.
- ServiceRegistry::remove<I>() withdraws a provided service (child
  removal re-exposes a parent's entry).
- EventBus::subscribe_scoped<E>() returns a move-only RAII
  Subscription that unsubscribes on destruction.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018yj92cCihyaYtf4gKCT3nv
This commit is contained in:
faumarayandClaude Fable 5 committed 2026-07-11 23:39:10 +03:00
1 parent 7ca0e1b62e
commit ee4486e1d2
15 files changed
+750 -24

No files matched your search

+70
View File
@@ -0,0 +1,70 @@
#include <Void/Plugin/Manager.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include "Configurable.hpp"
#include <filesystem>
#include <string>
using namespace Void::Plugin;
#if defined(VOID_CONFIGURABLE_PATH) && __has_include(<dlfcn.h>)
namespace
{
VoidTest::IConfigurable* configurable(const PluginHandle& h)
{
return VoidTest::as_configurable(h.get());
}
} // namespace
Void::Testing::Test configure_called_with_provider_payload_before_on_load()
{
PluginManager mgr{{.config_provider = [](std::string_view name) {
return std::string{"{\"plugin\":\""} + std::string{name} + "\"}";
}}};
const auto name = mgr.load(VOID_CONFIGURABLE_PATH);
Void::Testing::expect(name.has_value());
auto h = mgr.acquire("ConfigurablePlugin");
Void::Testing::expect_eq(configurable(h)->configure_calls(), 1);
Void::Testing::expect(configurable(h)->configured_before_on_load());
Void::Testing::expect_eq(
configurable(h)->last_config(),
std::string_view{"{\"plugin\":\"ConfigurablePlugin\"}"});
}
Void::Testing::Test configure_reruns_on_reload_with_fresh_payload()
{
Void::FileSystem::TempDir tmp{"void_cfg"};
const auto so = tmp.path() / "plugin.so";
std::filesystem::copy_file(VOID_CONFIGURABLE_PATH, so);
int generation = 0;
PluginManager mgr{{.config_provider = [&generation](std::string_view) {
return std::string{"gen="} + std::to_string(generation);
}}};
Void::Testing::expect(mgr.load(so).has_value());
generation = 1;
Void::Testing::expect(mgr.reload("ConfigurablePlugin").has_value());
auto h = mgr.acquire("ConfigurablePlugin");
// The reloaded instance is fresh: one configure call, new payload.
Void::Testing::expect_eq(configurable(h)->configure_calls(), 1);
Void::Testing::expect_eq(configurable(h)->last_config(),
std::string_view{"gen=1"});
Void::Testing::expect(configurable(h)->configured_before_on_load());
}
Void::Testing::Test configure_skipped_without_provider()
{
PluginManager mgr;
Void::Testing::expect(mgr.load(VOID_CONFIGURABLE_PATH).has_value());
auto h = mgr.acquire("ConfigurablePlugin");
Void::Testing::expect_eq(configurable(h)->configure_calls(), 0);
}
#endif // configurable plugin available
+72
View File
@@ -0,0 +1,72 @@
#include <Void/Plugin/Loader.hpp>
#include <Void/Plugin/Manager.hpp>
#include <Void/System/TempDir.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <filesystem>
#include <fstream>
#include <string>
using namespace Void::Plugin;
#if defined(VOID_TEST_PLUGIN_PATH) && defined(VOID_STATEFUL_V1_PATH) && \
__has_include(<dlfcn.h>)
Void::Testing::Test discovery_loads_all_plugins_in_filename_order()
{
Void::FileSystem::TempDir tmp{"void_disc"};
std::filesystem::copy_file(VOID_STATEFUL_V1_PATH,
tmp.path() / "b_stateful.so");
std::filesystem::copy_file(VOID_TEST_PLUGIN_PATH, tmp.path() / "a_test.so");
{
std::ofstream decoy{tmp.path() / "readme.txt"};
decoy << "not a plugin";
}
std::filesystem::create_directory(tmp.path() / "subdir");
PluginManager mgr;
const auto results = mgr.load_directory(tmp.path());
// Only the two .so files count, sorted by filename.
Void::Testing::expect_eq(results.size(), std::size_t{2});
Void::Testing::expect_eq(results[0].first.filename().string(),
std::string{"a_test.so"});
Void::Testing::expect(results[0].second.has_value());
Void::Testing::expect_eq(*results[0].second, std::string{"TestPlugin"});
Void::Testing::expect_eq(results[1].first.filename().string(),
std::string{"b_stateful.so"});
Void::Testing::expect(results[1].second.has_value());
Void::Testing::expect_eq(*results[1].second, std::string{"StatefulPlugin"});
Void::Testing::expect(static_cast<bool>(mgr.acquire("TestPlugin")));
Void::Testing::expect(static_cast<bool>(mgr.acquire("StatefulPlugin")));
}
Void::Testing::Test discovery_reports_per_file_errors()
{
Void::FileSystem::TempDir tmp{"void_disc"};
std::filesystem::copy_file(VOID_TEST_PLUGIN_PATH, tmp.path() / "ok.so");
{
std::ofstream bogus{tmp.path() / "broken.so"};
bogus << "ELF? no.";
}
PluginManager mgr;
const auto results = mgr.load_directory(tmp.path());
Void::Testing::expect_eq(results.size(), std::size_t{2});
// broken.so sorts first and fails to dlopen; ok.so loads.
Void::Testing::expect(!results[0].second.has_value());
Void::Testing::expect(results[0].second.error() == LoadError::DlopenFailed);
Void::Testing::expect(results[1].second.has_value());
}
Void::Testing::Test discovery_missing_directory_yields_empty()
{
PluginManager mgr;
const auto results = mgr.load_directory("/nonexistent/void_discovery_test");
Void::Testing::expect(results.empty());
}
#endif // test plugins available
+75
View File
@@ -166,4 +166,79 @@ Void::Testing::Test manager_watch_auto_reloads_on_file_change()
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_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