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:
1 parent
7ca0e1b62e
commit
ee4486e1d2
15 files changed
+750
-24
No files matched your search
@@ -98,6 +98,18 @@ if(TARGET Void::Plugin)
|
||||
add_dependencies(VoidTests VoidTestPlugin)
|
||||
endif()
|
||||
|
||||
# Configurable plugin — exercises the optional void_plugin_configure
|
||||
# entry point driven by PluginManagerOptions::config_provider.
|
||||
add_library(VoidConfigurablePlugin SHARED
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins/ConfigurablePlugin.cpp")
|
||||
target_link_libraries(VoidConfigurablePlugin PRIVATE Void::Plugin)
|
||||
target_include_directories(VoidConfigurablePlugin PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/plugins")
|
||||
target_compile_definitions(VoidTests PRIVATE
|
||||
"VOID_CONFIGURABLE_PATH=\"$<TARGET_FILE:VoidConfigurablePlugin>\""
|
||||
)
|
||||
add_dependencies(VoidTests VoidConfigurablePlugin)
|
||||
|
||||
# Stateful plugins (schema v1 + v2) for hot-reload/state-migration
|
||||
# tests. Same plugin name; v2 adds a field and migrates v1 JSON.
|
||||
add_library(VoidStatefulPluginV1 SHARED
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Plugin/Plugin.hpp>
|
||||
|
||||
#include <string_view>
|
||||
|
||||
// Shared interface for the configure-entry-point test plugin. Same
|
||||
// single-inheritance static_cast recovery pattern as Stateful.hpp.
|
||||
namespace VoidTest
|
||||
{
|
||||
class IConfigurable : public Void::Plugin::IPlugin
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] virtual std::string_view last_config() const = 0;
|
||||
[[nodiscard]] virtual int configure_calls() const = 0;
|
||||
[[nodiscard]] virtual bool configured_before_on_load() const = 0;
|
||||
};
|
||||
|
||||
[[nodiscard]] inline IConfigurable*
|
||||
as_configurable(Void::Plugin::IPlugin* p)
|
||||
{
|
||||
return static_cast<IConfigurable*>(p);
|
||||
}
|
||||
} // namespace VoidTest
|
||||
@@ -0,0 +1,78 @@
|
||||
#include "Configurable.hpp"
|
||||
|
||||
#include <Void/Plugin/Plugin.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
// A plugin that records what the host passes through the optional
|
||||
// void_plugin_configure entry point, and when (relative to on_load).
|
||||
|
||||
namespace
|
||||
{
|
||||
class ConfigurablePlugin final : public VoidTest::IConfigurable
|
||||
{
|
||||
public:
|
||||
|
||||
[[nodiscard]] std::string_view name() const noexcept override
|
||||
{
|
||||
return "ConfigurablePlugin";
|
||||
}
|
||||
|
||||
void on_load() override { loaded_ = true; }
|
||||
|
||||
[[nodiscard]] std::string_view last_config() const override
|
||||
{
|
||||
return config_;
|
||||
}
|
||||
|
||||
[[nodiscard]] int configure_calls() const override
|
||||
{
|
||||
return calls_;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool configured_before_on_load() const override
|
||||
{
|
||||
return before_on_load_;
|
||||
}
|
||||
|
||||
void apply_config(const char* config)
|
||||
{
|
||||
config_ = config != nullptr ? config : "";
|
||||
++calls_;
|
||||
if (!loaded_) {
|
||||
before_on_load_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
std::string config_;
|
||||
int calls_{0};
|
||||
bool loaded_{false};
|
||||
bool before_on_load_{false};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
extern "C" {
|
||||
void* void_plugin_create() { return new ConfigurablePlugin; }
|
||||
|
||||
void void_plugin_destroy(void* p)
|
||||
{
|
||||
delete static_cast<ConfigurablePlugin*>(p);
|
||||
}
|
||||
|
||||
Void::Plugin::PluginInfo void_plugin_info()
|
||||
{
|
||||
return {
|
||||
.abi_version = Void::Plugin::ABI_VERSION,
|
||||
.name = "ConfigurablePlugin",
|
||||
.plugin_version = "1.0.0",
|
||||
.description = "Configure entry-point test plugin",
|
||||
};
|
||||
}
|
||||
|
||||
void void_plugin_configure(void* instance, const char* config)
|
||||
{
|
||||
static_cast<ConfigurablePlugin*>(instance)->apply_config(config);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
using namespace Void::EventBus;
|
||||
|
||||
@@ -113,3 +114,42 @@ Void::Testing::Test bus_event_data_forwarded()
|
||||
bus.publish(TextChanged{"hello"});
|
||||
Void::Testing::expect_eq(received, std::string{"hello"});
|
||||
}
|
||||
|
||||
// ── scoped (RAII) subscriptions ───────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bus_scoped_subscription_unsubscribes_on_destruction()
|
||||
{
|
||||
EventBus bus;
|
||||
int count = 0;
|
||||
{
|
||||
auto sub =
|
||||
bus.subscribe_scoped<Clicked>([&](const Clicked&) { ++count; });
|
||||
Void::Testing::expect(static_cast<bool>(sub));
|
||||
bus.publish(Clicked{0, 0});
|
||||
Void::Testing::expect_eq(count, 1);
|
||||
}
|
||||
// Handler removed with the subscription.
|
||||
bus.publish(Clicked{0, 0});
|
||||
Void::Testing::expect_eq(count, 1);
|
||||
Void::Testing::expect_eq(bus.subscriber_count<Clicked>(), std::size_t{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test bus_scoped_subscription_moves_ownership()
|
||||
{
|
||||
EventBus bus;
|
||||
int count = 0;
|
||||
auto outer = EventBus::Subscription{};
|
||||
{
|
||||
auto inner =
|
||||
bus.subscribe_scoped<Clicked>([&](const Clicked&) { ++count; });
|
||||
outer = std::move(inner);
|
||||
Void::Testing::expect(!static_cast<bool>(inner));
|
||||
}
|
||||
// The moved-from handle's death did not unsubscribe.
|
||||
bus.publish(Clicked{0, 0});
|
||||
Void::Testing::expect_eq(count, 1);
|
||||
|
||||
outer.reset();
|
||||
bus.publish(Clicked{0, 0});
|
||||
Void::Testing::expect_eq(count, 1);
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -114,6 +114,43 @@ Void::Testing::Test registry_replace_provider()
|
||||
Void::Testing::expect_eq(reg.size(), std::size_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test registry_remove_withdraws_entry()
|
||||
{
|
||||
ServiceRegistry reg;
|
||||
auto clock = std::make_shared<FixedClock>(7);
|
||||
reg.provide<IClock>(clock);
|
||||
|
||||
// A consumer resolved before the removal keeps its instance.
|
||||
auto held = reg.get<IClock>();
|
||||
|
||||
Void::Testing::expect(reg.remove<IClock>());
|
||||
Void::Testing::expect(!reg.has<IClock>());
|
||||
Void::Testing::expect(reg.get<IClock>() == nullptr);
|
||||
Void::Testing::expect_eq(reg.size(), std::size_t{0});
|
||||
Void::Testing::expect_eq(held->now(), 7);
|
||||
|
||||
// Removing again reports nothing to remove.
|
||||
Void::Testing::expect(!reg.remove<IClock>());
|
||||
|
||||
// Re-providing after removal works (fresh lifecycle position).
|
||||
reg.provide<IClock>(std::make_shared<FixedClock>(9));
|
||||
Void::Testing::expect_eq(reg.require<IClock>().now(), 9);
|
||||
}
|
||||
|
||||
Void::Testing::Test registry_remove_in_child_reexposes_parent()
|
||||
{
|
||||
ServiceRegistry parent;
|
||||
parent.provide<IClock>(std::make_shared<FixedClock>(1));
|
||||
|
||||
ServiceRegistry child{&parent};
|
||||
child.provide<IClock>(std::make_shared<FixedClock>(2));
|
||||
Void::Testing::expect_eq(child.require<IClock>().now(), 2);
|
||||
|
||||
// Removing the child's shadow re-exposes the parent's entry.
|
||||
Void::Testing::expect(child.remove<IClock>());
|
||||
Void::Testing::expect_eq(child.require<IClock>().now(), 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test registry_lifecycle_order()
|
||||
{
|
||||
std::vector<std::string> log;
|
||||
|
||||
Reference in new issue
Block a user