#include #include #include #include #include #include #include using namespace Void::Fsm; #ifdef VOID_HAS_ENUM_REFLECTION namespace { enum class Phase { Ping, Pong }; enum class Signal { Flip }; } // namespace Void::Testing::Test enum_reflection_builds_states_and_events() { Builder b; b.states_from_enum(); b.initial(b.state_of(Phase::Ping)); const auto flip = b.event(Signal::Flip); b.transition(b.state_of(Phase::Ping), flip, b.state_of(Phase::Pong)); b.transition(b.state_of(Phase::Pong), flip, b.state_of(Phase::Ping)); auto def = b.build(); Void::Testing::expect(def.has_value()); Void::Testing::expect((*def)->state_id("Ping").has_value()); Void::Testing::expect((*def)->event_id("Flip").has_value()); Machine m{*def, MachineOptions{.own_worker = false}}; m.start(); Void::Testing::expect(m.is_in("Ping")); Void::Testing::expect(m.post(Signal::Flip)); m.run_pending(); Void::Testing::expect(m.is_in("Pong")); // Serialized state uses enumerator names — stable across reloads. const auto json = m.serialize_state(); Void::Testing::expect_eq(json["leaves"][0].as_string(), std::string{"Pong"}); } #endif // VOID_HAS_ENUM_REFLECTION // @tag stress, concurrency Void::Testing::Test concurrent_posts_all_processed_in_producer_order() { constexpr int producers = 8; constexpr int per_producer = 10'000; // counter state with an internal self-loop that consumes "bump" // events carrying {producer, seq} payloads. struct Payload { int producer; int seq; }; std::vector next_seq(producers, 0); std::atomic handled{0}; bool order_ok = true; Builder b; auto counting = b.state("counting"); b.initial(counting); const auto bump = b.event("bump"); b.internal(counting, bump) .action(SyncAction{[&](Machine&, const Event& e) { const auto p = std::any_cast(e.payload); // Per-producer FIFO must hold (single consumer). if (next_seq[static_cast(p.producer)] != p.seq) { order_ok = false; } ++next_seq[static_cast(p.producer)]; ++handled; }}); auto def = b.build(); Void::Testing::expect(def.has_value()); Machine m{*def}; // own worker m.start(); std::atomic go{false}; std::vector threads; threads.reserve(producers); for (int p = 0; p < producers; ++p) { threads.emplace_back([&m, &go, bump, p] { go.wait(false); for (int i = 0; i < per_producer; ++i) { while (!m.post(Event{bump, Payload{p, i}})) { std::this_thread::yield(); } } }); } go = true; go.notify_all(); for (auto& t : threads) { t.join(); } // Everything posted before this send_wait is processed before it. (void)m.send_wait(Event{bump, Payload{0, per_producer}}); ++next_seq[0]; // account for the flush event m.stop(); Void::Testing::expect_eq( handled.load(), static_cast(producers) * per_producer + 1); Void::Testing::expect(order_ok); } // @tag stress, concurrency Void::Testing::Test snapshot_reads_never_tear_under_load() { Builder b; auto ping = b.state("ping"); auto pong = b.state("pong"); b.initial(ping); const auto flip = b.event("flip"); b.transition(ping, flip, pong); b.transition(pong, flip, ping); auto def = b.build(); Void::Testing::expect(def.has_value()); Machine m{*def}; m.start(); std::atomic stop{false}; std::atomic reads{0}; bool sane = true; std::vector readers; readers.reserve(4); for (int r = 0; r < 4; ++r) { readers.emplace_back([&] { while (!stop.load(std::memory_order_relaxed)) { const auto snap = m.current(); // Exactly one leaf, and it is one of the two states. if (snap.leaf_count != 1) { sane = false; } ++reads; } }); } for (int i = 0; i < 20'000; ++i) { m.post(Event{flip, {}}); } (void)m.send_wait(Event{flip, {}}); stop = true; for (auto& t : readers) { t.join(); } m.stop(); Void::Testing::expect(sane); Void::Testing::expect(reads.load() > 0); } // @tag stress, concurrency Void::Testing::Test hot_swap_under_sustained_load() { auto make = [](bool variant) { Builder b; auto ping = b.state("ping"); auto pong = b.state("pong"); b.initial(ping); const auto flip = b.event("flip"); b.transition(ping, flip, pong); b.transition(pong, flip, ping); if (variant) { // identical topology, extra no-op event (void)b.event("extra"); } auto def = b.build(); Void::Testing::expect(def.has_value()); return *def; }; Machine m{make(false)}; m.start(); std::atomic stop{false}; std::vector producers; producers.reserve(4); const auto flip = *m.definition()->event_id("flip"); for (int p = 0; p < 4; ++p) { producers.emplace_back([&m, &stop, flip] { while (!stop.load(std::memory_order_relaxed)) { (void)m.post(Event{flip, {}}); // Pace producers so the backlog stays bounded — the test // exercises swap-under-load, not queue growth. std::this_thread::yield(); } }); } for (int i = 0; i < 50; ++i) { m.swap_definition(make(i % 2 == 1)); std::this_thread::sleep_for(std::chrono::microseconds{200}); } stop = true; for (auto& t : producers) { t.join(); } (void)m.send_wait(Event{flip, {}}); // Machine is still coherent: exactly one of the two states active. const bool in_ping = m.is_in("ping"); const bool in_pong = m.is_in("pong"); Void::Testing::expect(in_ping != in_pong); m.stop(); }