#include #include #include #include #include #include #include #include #include #include #include #include #if defined(VOID_FSM_PLUGIN_V1_PATH) && defined(VOID_FSM_PLUGIN_V2_PATH) && \ __has_include() 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 def_from(const PluginManager& mgr) { auto* sym = reinterpret_cast( 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(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 stop{false}; std::atomic posts{0}; std::vector 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(machine.is_in("idle")) + static_cast(machine.is_in("busy")) + static_cast(machine.is_in("done")); Void::Testing::expect_eq(active, 1); Void::Testing::expect(posts.load() > 0); machine.stop(); } #endif // FSM plugins available