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
71 lines
2.2 KiB
C++
71 lines
2.2 KiB
C++
#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
|