117 lines
3.9 KiB
C++
117 lines
3.9 KiB
C++
#include <Void/Plugin/Manager.hpp>
|
|
#include <Void/Plugin/Loader.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);
|
|
}
|
|
|
|
Void::Testing::Test activation_failure_keeps_current_instance()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_cfg_ready"};
|
|
const auto so = tmp.path() / "plugin.so";
|
|
std::filesystem::copy_file(VOID_CONFIGURABLE_PATH, so);
|
|
|
|
std::string payload{"accepted"};
|
|
PluginManager mgr{{.config_provider = [&payload](std::string_view) {
|
|
return payload;
|
|
}}};
|
|
Void::Testing::expect(mgr.load(so).has_value());
|
|
auto before = mgr.acquire("ConfigurablePlugin");
|
|
Void::Testing::expect_eq(configurable(before)->last_config(),
|
|
std::string_view{"accepted"});
|
|
|
|
payload = "reject";
|
|
const auto result = mgr.reload("ConfigurablePlugin");
|
|
Void::Testing::expect(!result.has_value());
|
|
Void::Testing::expect(result.error() == LoadError::ActivationFailed);
|
|
auto after = mgr.acquire("ConfigurablePlugin");
|
|
Void::Testing::expect(after.get() == before.get());
|
|
Void::Testing::expect_eq(configurable(after)->last_config(),
|
|
std::string_view{"accepted"});
|
|
}
|
|
|
|
Void::Testing::Test host_validation_failure_keeps_current_instance()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_cfg_validator"};
|
|
const auto so = tmp.path() / "plugin.so";
|
|
std::filesystem::copy_file(VOID_CONFIGURABLE_PATH, so);
|
|
|
|
bool accepted = true;
|
|
PluginManager mgr{{.instance_validator = [&accepted](IPlugin&,
|
|
const PluginInfo&) { return accepted; }}};
|
|
Void::Testing::expect(mgr.load(so).has_value());
|
|
auto before = mgr.acquire("ConfigurablePlugin");
|
|
|
|
accepted = false;
|
|
const auto result = mgr.reload("ConfigurablePlugin");
|
|
Void::Testing::expect(!result.has_value());
|
|
Void::Testing::expect(result.error() == LoadError::ActivationFailed);
|
|
auto after = mgr.acquire("ConfigurablePlugin");
|
|
Void::Testing::expect(after.get() == before.get());
|
|
}
|
|
|
|
#endif // configurable plugin available
|