Fsm/PluginBehavior.hpp: IFsmBehaviorPack::register_into(Builder&) lets a plugin contribute states/events/guards/actions; plugins export it via extern "C" void_plugin_fsm_behaviors(). build_machine_def(pack) runs the pack through a Builder and validates. PluginManager::raw_symbol(name, symbol) resolves an optional extra entry point from a plugin's current .so (returns nullptr if absent), so the host can fetch the behaviour factory without widening the frozen IPlugin vtable. Integration flow (tests/internal/FsmPluginHotReload.cpp): build a machine def from the plugin pack, run it; on reload, rebuild from the reloaded pack and swap_definition between RTC steps. Same state names across versions ⇒ the live configuration is adopted seamlessly (machine keeps running, queue intact); v2's new "skip" transition goes live. Safety: plugin actions are std::functions whose code lives in the .so, so a PluginHandle is pinned for the machine's lifetime and dlclose_on_swap= false keeps the code pages mapped under in-flight frames. Tests: FsmPluginV1/V2 (same idle/busy/done states; v2 adds skip). Definition-swap test proves configuration survival + new behaviour; the stress test runs 4 producer threads posting while 5 v1<->v2 rebuild+swaps happen concurrently, then asserts exactly one active leaf. 967 tests green, stable across runs, ASan+UBSan clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
159 lines
5.0 KiB
C++
159 lines
5.0 KiB
C++
#include <Void/Fsm/Machine.hpp>
|
|
#include <Void/Fsm/PluginBehavior.hpp>
|
|
#include <Void/Plugin/Manager.hpp>
|
|
#include <Void/System/TempDir.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <filesystem>
|
|
#include <memory>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#if defined(VOID_FSM_PLUGIN_V1_PATH) && defined(VOID_FSM_PLUGIN_V2_PATH) && \
|
|
__has_include(<dlfcn.h>)
|
|
|
|
using namespace Void::Fsm;
|
|
using Void::Plugin::PluginManager;
|
|
|
|
namespace
|
|
{
|
|
void install(const std::filesystem::path& src,
|
|
const std::filesystem::path& dst)
|
|
{
|
|
auto staging = dst;
|
|
staging += ".incoming";
|
|
std::filesystem::copy_file(
|
|
src, staging, std::filesystem::copy_options::overwrite_existing);
|
|
std::filesystem::rename(staging, dst);
|
|
}
|
|
|
|
// Build a machine definition from the plugin's behaviour pack.
|
|
std::shared_ptr<const MachineDef> def_from(const PluginManager& mgr)
|
|
{
|
|
auto* sym = reinterpret_cast<fsm_behaviors_fn>(
|
|
mgr.raw_symbol("FsmPlugin", kFsmBehaviorsSymbol));
|
|
Void::Testing::expect(sym != nullptr);
|
|
if (sym == nullptr) {
|
|
return nullptr;
|
|
}
|
|
auto built = build_machine_def(*sym());
|
|
Void::Testing::expect(built.has_value());
|
|
return built.value_or(nullptr);
|
|
}
|
|
} // namespace
|
|
|
|
// A machine whose states and actions come entirely from a plugin keeps
|
|
// running across a plugin reload: the new definition is built from the
|
|
// reloaded behaviour pack and swapped in between run-to-completion steps.
|
|
Void::Testing::Test internal_fsm_plugin_definition_swap()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_fsmplugin"};
|
|
const auto so = tmp.path() / "fsm.so";
|
|
std::filesystem::copy_file(VOID_FSM_PLUGIN_V1_PATH, so);
|
|
|
|
PluginManager mgr;
|
|
Void::Testing::expect(mgr.load(so).has_value());
|
|
|
|
auto def = def_from(mgr);
|
|
Void::Testing::expect(def != nullptr);
|
|
|
|
Machine machine{def, MachineOptions{.own_worker = false}};
|
|
machine.start();
|
|
machine.post(Event{*def->event_id("start"), {}});
|
|
machine.run_pending();
|
|
Void::Testing::expect(machine.is_in("busy"));
|
|
|
|
// Reload to v2 (adds a "skip" transition; same state names) and swap
|
|
// the definition in. The machine keeps its active "busy" leaf.
|
|
install(VOID_FSM_PLUGIN_V2_PATH, so);
|
|
Void::Testing::expect(mgr.reload("FsmPlugin").has_value());
|
|
auto def2 = def_from(mgr);
|
|
Void::Testing::expect(def2 != nullptr);
|
|
machine.swap_definition(def2);
|
|
machine.run_pending();
|
|
Void::Testing::expect(machine.is_in("busy")); // configuration survived
|
|
|
|
// Finish the v1 cycle, then exercise the v2-only "skip" transition.
|
|
machine.post(Event{*def2->event_id("finish"), {}});
|
|
machine.run_pending();
|
|
Void::Testing::expect(machine.is_in("done"));
|
|
machine.post(Event{*def2->event_id("reset"), {}});
|
|
machine.run_pending();
|
|
Void::Testing::expect(machine.is_in("idle"));
|
|
machine.post(Event{*def2->event_id("skip"), {}}); // new in v2
|
|
machine.run_pending();
|
|
Void::Testing::expect(machine.is_in("done"));
|
|
}
|
|
|
|
// @tag stress, concurrency
|
|
Void::Testing::Test internal_fsm_plugin_hot_reload_under_load()
|
|
{
|
|
Void::FileSystem::TempDir tmp{"void_fsmplugin"};
|
|
const auto so = tmp.path() / "fsm.so";
|
|
std::filesystem::copy_file(VOID_FSM_PLUGIN_V1_PATH, so);
|
|
|
|
PluginManager mgr;
|
|
Void::Testing::expect(mgr.load(so).has_value());
|
|
auto def = def_from(mgr);
|
|
|
|
// Worker-driven machine; the plugin's .so stays mapped (the manager's
|
|
// dlclose_on_swap=false) so its action code remains valid for frames
|
|
// in flight. Pin a handle for the machine's lifetime as belt-and-
|
|
// suspenders against the .so unmapping.
|
|
auto pin = mgr.acquire("FsmPlugin");
|
|
Void::Testing::expect(static_cast<bool>(pin));
|
|
|
|
Machine machine{def};
|
|
machine.start();
|
|
|
|
const auto start = *def->event_id("start");
|
|
const auto finish = *def->event_id("finish");
|
|
const auto reset = *def->event_id("reset");
|
|
|
|
std::atomic<bool> stop{false};
|
|
std::atomic<long long> posts{0};
|
|
std::vector<std::thread> producers;
|
|
producers.reserve(4);
|
|
for (int p = 0; p < 4; ++p) {
|
|
producers.emplace_back([&] {
|
|
while (!stop.load(std::memory_order_relaxed)) {
|
|
machine.post(Event{start, {}});
|
|
machine.post(Event{finish, {}});
|
|
machine.post(Event{reset, {}});
|
|
posts.fetch_add(3, std::memory_order_relaxed);
|
|
std::this_thread::yield();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Rebuild + swap the definition repeatedly while events flow.
|
|
for (int i = 0; i < 5; ++i) {
|
|
const char* src =
|
|
(i % 2 == 0) ? VOID_FSM_PLUGIN_V2_PATH : VOID_FSM_PLUGIN_V1_PATH;
|
|
install(src, so);
|
|
Void::Testing::expect(mgr.reload("FsmPlugin").has_value());
|
|
machine.swap_definition(def_from(mgr));
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{3});
|
|
}
|
|
|
|
stop.store(true);
|
|
for (auto& t : producers) {
|
|
t.join();
|
|
}
|
|
|
|
// Drain and confirm the machine is still coherent: exactly one of the
|
|
// three leaves is active.
|
|
(void)machine.send_wait(Event{reset, {}});
|
|
const int active = static_cast<int>(machine.is_in("idle")) +
|
|
static_cast<int>(machine.is_in("busy")) +
|
|
static_cast<int>(machine.is_in("done"));
|
|
Void::Testing::expect_eq(active, 1);
|
|
Void::Testing::expect(posts.load() > 0);
|
|
machine.stop();
|
|
}
|
|
|
|
#endif // FSM plugins available
|