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
@@ -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
|
||||
Reference in new issue
Block a user