Files
Library/tests/standalone/Plugin/Discovery.cpp
T
faumarayandClaude Fable 5 ee4486e1d2 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
2026-07-11 23:39:10 +03:00

73 lines
2.4 KiB
C++

#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