Definition (Builder -> immutable shared MachineDef): - Named hierarchical states, events by name or enum identifier (P2996), states_from_enum, initial/parallel composites, shallow/deep history, deferred events, per-state timeouts, guards, entry/exit/transition actions (coroutine Task<void> or sync, wrapped), internal transitions. - build() validates (initial children, transition endpoints) and flattens into index-addressed vectors with depth info for LCA. Machine (one consumer, many producers): - post() is lock-free from any thread: Vyukov MPSC push + counting- semaphore park/wake; the single consumer (owned worker, or embedder via run_pending) owns all mutable machine state with zero locks. - Observation via seqlock over a trivially-copyable StateSnapshot of active leaves; readers retry, writer is wait-free, no heap. - Run-to-completion: innermost-first transition resolution per region, LCA-based exit/entry ordering, history snapshotted at exit time, deferred events re-injected FIFO after each state change, timers via deadline park (generation counters kill stale fires), orthogonal regions dispatched independently on the single consumer. - co_await machine.send(ev) / send_wait() resolve with SendResult after the step; actions are driven to completion on the consumer's ManualExecutor before the step ends. - swap_definition: atomic<shared_ptr> definition swap between steps; identical active-leaf names adopt seamlessly (no entry re-runs, queue intact), otherwise initial configuration; history remapped by name. - serialize_state()/restore_state(): JSON by state *names* for hot-reload and persistence; restore re-activates silently. - start() returns only once the initial configuration is entered. 40 new tests: semantics (ordering, guards, internal/self transitions), hierarchy + both history kinds, defer FIFO, parallel regions, timers, coroutine actions, awaitable/blocking send, serialize/restore round trips, seamless + fallback hot swap, and stress: 8 producers x 10k with per-producer FIFO assertion, 4-reader seqlock tear check, 50 definition swaps under sustained load. Green under ASan+UBSan, stable across repeated runs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
187 lines
5.9 KiB
C++
187 lines
5.9 KiB
C++
#include <Void/Fsm/Definition.hpp>
|
|
#include <Void/Fsm/Machine.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
|
|
using namespace Void::Fsm;
|
|
|
|
namespace
|
|
{
|
|
// work{ sub{ s1, s2 } }, pause — with deep history on work.
|
|
std::shared_ptr<const MachineDef> make_def()
|
|
{
|
|
Builder b;
|
|
auto work = b.state("work");
|
|
auto sub = b.state("sub", work);
|
|
auto s1 = b.state("s1", sub);
|
|
auto s2 = b.state("s2", sub);
|
|
auto pause = b.state("pause");
|
|
b.initial(work);
|
|
b.initial(sub);
|
|
b.initial(s1);
|
|
b.history(work, History::Deep);
|
|
b.transition(s1, b.event("next"), s2);
|
|
b.transition(work, b.event("stop"), pause);
|
|
b.transition(pause, b.event("resume"), work);
|
|
auto def = b.build();
|
|
Void::Testing::expect(def.has_value());
|
|
return *def;
|
|
}
|
|
} // namespace
|
|
|
|
Void::Testing::Test serialize_captures_leaves_by_name()
|
|
{
|
|
auto def = make_def();
|
|
Machine m{def, MachineOptions{.own_worker = false}};
|
|
m.start();
|
|
m.post(Event{*def->event_id("next"), {}});
|
|
m.run_pending();
|
|
|
|
const auto json = m.serialize_state();
|
|
Void::Testing::expect(json.is_object());
|
|
const auto& leaves = json["leaves"].as_array();
|
|
Void::Testing::expect_eq(leaves.size(), std::size_t{1});
|
|
Void::Testing::expect_eq(leaves[0].as_string(), std::string{"s2"});
|
|
}
|
|
|
|
Void::Testing::Test serialize_restore_round_trip()
|
|
{
|
|
auto def = make_def();
|
|
Void::Json::Value saved;
|
|
{
|
|
Machine m{def, MachineOptions{.own_worker = false}};
|
|
m.start();
|
|
m.post(Event{*def->event_id("next"), {}}); // s1 -> s2
|
|
m.run_pending();
|
|
saved = m.serialize_state();
|
|
m.stop();
|
|
}
|
|
|
|
Machine restored{def, MachineOptions{.own_worker = false}};
|
|
Void::Testing::expect(restored.restore_state(saved).has_value());
|
|
restored.start();
|
|
|
|
Void::Testing::expect(restored.is_in("s2"));
|
|
Void::Testing::expect(restored.is_in("work"));
|
|
Void::Testing::expect(!restored.is_in("s1"));
|
|
|
|
// The restored machine behaves identically: stop/resume goes back
|
|
// to s2 via deep history captured in the serialized state.
|
|
restored.post(Event{*def->event_id("stop"), {}});
|
|
restored.post(Event{*def->event_id("resume"), {}});
|
|
restored.run_pending();
|
|
Void::Testing::expect(restored.is_in("s2"));
|
|
}
|
|
|
|
Void::Testing::Test serialize_preserves_history_records()
|
|
{
|
|
auto def = make_def();
|
|
Void::Json::Value saved;
|
|
{
|
|
Machine m{def, MachineOptions{.own_worker = false}};
|
|
m.start();
|
|
m.post(Event{*def->event_id("next"), {}}); // -> s2
|
|
m.post(Event{*def->event_id("stop"), {}}); // -> pause, hist {s2}
|
|
m.run_pending();
|
|
saved = m.serialize_state();
|
|
m.stop();
|
|
}
|
|
|
|
Machine restored{def, MachineOptions{.own_worker = false}};
|
|
Void::Testing::expect(restored.restore_state(saved).has_value());
|
|
restored.start();
|
|
Void::Testing::expect(restored.is_in("pause"));
|
|
|
|
restored.post(Event{*def->event_id("resume"), {}});
|
|
restored.run_pending();
|
|
Void::Testing::expect(restored.is_in("s2")); // history survived
|
|
}
|
|
|
|
Void::Testing::Test restore_rejects_running_machine_and_garbage()
|
|
{
|
|
auto def = make_def();
|
|
Machine m{def, MachineOptions{.own_worker = false}};
|
|
Void::Testing::expect(
|
|
!m.restore_state(Void::Json::Value{42}).has_value());
|
|
m.start();
|
|
const auto state = m.serialize_state();
|
|
Void::Testing::expect(!m.restore_state(state).has_value());
|
|
}
|
|
|
|
Void::Testing::Test restore_with_unknown_states_falls_back_to_initial()
|
|
{
|
|
auto def = make_def();
|
|
auto bogus = Void::Json::parse(
|
|
R"({"__fsm":1,"leaves":["no_such_state"],"history":{}})");
|
|
Void::Testing::expect(bogus.has_value());
|
|
|
|
Machine m{def, MachineOptions{.own_worker = false}};
|
|
Void::Testing::expect(m.restore_state(*bogus).has_value());
|
|
m.start();
|
|
Void::Testing::expect(m.is_in("s1")); // initial configuration
|
|
}
|
|
|
|
// ── hot definition swap ───────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test swap_same_names_keeps_configuration()
|
|
{
|
|
auto def = make_def();
|
|
Machine m{def, MachineOptions{.own_worker = false}};
|
|
m.start();
|
|
m.post(Event{*def->event_id("next"), {}}); // -> s2
|
|
m.run_pending();
|
|
|
|
int entries = 0;
|
|
// New definition: same state names, one extra transition + an entry
|
|
// probe that must NOT run during a seamless swap.
|
|
Builder b;
|
|
auto work = b.state("work");
|
|
auto sub = b.state("sub", work);
|
|
auto s1 = b.state("s1", sub);
|
|
auto s2 = b.state("s2", sub);
|
|
auto pause = b.state("pause");
|
|
b.initial(work);
|
|
b.initial(sub);
|
|
b.initial(s1);
|
|
b.on_entry(s2, SyncAction{[&](Machine&, const Event&) {
|
|
++entries;
|
|
}});
|
|
b.transition(s2, b.event("back"), s1); // new behavior
|
|
b.transition(work, b.event("stop"), pause);
|
|
auto next_def = b.build();
|
|
Void::Testing::expect(next_def.has_value());
|
|
|
|
m.swap_definition(*next_def);
|
|
m.run_pending();
|
|
|
|
Void::Testing::expect(m.is_in("s2")); // configuration survived
|
|
Void::Testing::expect_eq(entries, 0); // no re-entry side effects
|
|
|
|
// New transition is live.
|
|
m.post(Event{*(*next_def)->event_id("back"), {}});
|
|
m.run_pending();
|
|
Void::Testing::expect(m.is_in("s1"));
|
|
}
|
|
|
|
Void::Testing::Test swap_with_missing_states_reenters_initial()
|
|
{
|
|
auto def = make_def();
|
|
Machine m{def, MachineOptions{.own_worker = false}};
|
|
m.start();
|
|
m.post(Event{*def->event_id("next"), {}}); // -> s2
|
|
m.run_pending();
|
|
|
|
Builder b;
|
|
(void)b.state("fresh_a");
|
|
(void)b.state("fresh_b");
|
|
auto next_def = b.build();
|
|
Void::Testing::expect(next_def.has_value());
|
|
|
|
m.swap_definition(*next_def);
|
|
m.run_pending();
|
|
Void::Testing::expect(m.is_in("fresh_a"));
|
|
}
|