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
156 lines
5.0 KiB
C++
156 lines
5.0 KiB
C++
#include <Void/Async/EventBus.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
using namespace Void::EventBus;
|
|
|
|
struct Clicked { int x, y; };
|
|
struct TextChanged { std::string text; };
|
|
|
|
// ── subscribe / publish ───────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_publish_reaches_subscriber()
|
|
{
|
|
EventBus bus;
|
|
int received = 0;
|
|
bus.subscribe<Clicked>([&](const Clicked& e) { received = e.x; });
|
|
bus.publish(Clicked{42, 0});
|
|
Void::Testing::expect_eq(received, 42);
|
|
}
|
|
|
|
Void::Testing::Test bus_multiple_subscribers_same_type()
|
|
{
|
|
EventBus bus;
|
|
int count = 0;
|
|
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
|
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
|
bus.publish(Clicked{0, 0});
|
|
Void::Testing::expect_eq(count, 2);
|
|
}
|
|
|
|
Void::Testing::Test bus_different_event_types_isolated()
|
|
{
|
|
EventBus bus;
|
|
bool got_click = false;
|
|
bool got_text = false;
|
|
bus.subscribe<Clicked>([&](const Clicked&) { got_click = true; });
|
|
bus.subscribe<TextChanged>([&](const TextChanged&) { got_text = true; });
|
|
bus.publish(Clicked{0, 0});
|
|
Void::Testing::expect(got_click);
|
|
Void::Testing::expect(!got_text);
|
|
}
|
|
|
|
Void::Testing::Test bus_no_subscribers_publish_is_noop()
|
|
{
|
|
EventBus bus;
|
|
bus.publish(Clicked{1, 2});
|
|
}
|
|
|
|
// ── unsubscribe ───────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_unsubscribe_stops_delivery()
|
|
{
|
|
EventBus bus;
|
|
int count = 0;
|
|
const auto id = bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
|
bus.publish(Clicked{});
|
|
bus.unsubscribe(id);
|
|
bus.publish(Clicked{});
|
|
Void::Testing::expect_eq(count, 1);
|
|
}
|
|
|
|
Void::Testing::Test bus_unsubscribe_nonexistent_is_noop()
|
|
{
|
|
EventBus bus;
|
|
bus.unsubscribe(999);
|
|
}
|
|
|
|
// ── clear ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_clear_event_type()
|
|
{
|
|
EventBus bus;
|
|
int count = 0;
|
|
bus.subscribe<Clicked>([&](const Clicked&) { ++count; });
|
|
bus.clear<Clicked>();
|
|
bus.publish(Clicked{});
|
|
Void::Testing::expect_eq(count, 0);
|
|
}
|
|
|
|
Void::Testing::Test bus_clear_all()
|
|
{
|
|
EventBus bus;
|
|
int a = 0, b = 0;
|
|
bus.subscribe<Clicked>([&](const Clicked&) { ++a; });
|
|
bus.subscribe<TextChanged>([&](const TextChanged&) { ++b; });
|
|
bus.clear_all();
|
|
bus.publish(Clicked{});
|
|
bus.publish(TextChanged{});
|
|
Void::Testing::expect_eq(a, 0);
|
|
Void::Testing::expect_eq(b, 0);
|
|
}
|
|
|
|
// ── subscriber_count ──────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_subscriber_count()
|
|
{
|
|
EventBus bus;
|
|
Void::Testing::expect_eq(bus.subscriber_count<Clicked>(), std::size_t{0});
|
|
bus.subscribe<Clicked>([](const Clicked&) {});
|
|
bus.subscribe<Clicked>([](const Clicked&) {});
|
|
Void::Testing::expect_eq(bus.subscriber_count<Clicked>(), std::size_t{2});
|
|
}
|
|
|
|
// ── event data forwarded correctly ────────────────────────────────────────────
|
|
|
|
Void::Testing::Test bus_event_data_forwarded()
|
|
{
|
|
EventBus bus;
|
|
std::string received;
|
|
bus.subscribe<TextChanged>([&](const TextChanged& e) { received = e.text; });
|
|
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);
|
|
}
|