A machine is now Machine<Derived> (dropping the ResultT template param) and
declares its result types as nested types reflected *by name* — Success and
Failure (a struct or a using-alias), each defaulting to void:
class Login : public Machine<Login> {
using Success = Token; // or a nested struct; default void
struct Failure { int code; };
};
- Outcome = std::expected<Success, Failure> (void failure -> std::monostate
error arm). succeed(Success{...}) / fail(Failure{...}) carry the typed
values (nullary when void). Resolved by plain dependent-name lookup
(Detail/Result.hpp), no P2996.
- Engine/system faults (no initial state, unregistered target, no entry)
travel a SEPARATE FsmError channel: status()==Faulted + system_error(),
never the typed Failure. New Step::Kind::Fault carries it.
- CRTP wrinkle: Derived is incomplete at base instantiation, so the engine
stores the result type-erased (std::any) and rebuilds the typed Outcome in
wait()/run_blocking()/take_outcome() — function templates defaulted on
Derived. run_child/run_region/start_machine reconstruct OutcomeOf<Sub> at
the boundary (Sub is complete).
- Step carries Success/Failure payloads as std::any; the reactive DSL
(finish/fail) and Continue<Sub> are reparameterised off the nested types.
Interim: Continue<Sub> keeps the named on_success/on_failure form (Phase 2
replaces it with signature-inferred ChildSuccess/ChildFailure). All FSM
tests migrated to the nested-type form. 995 tests (992 green, 3 skipped),
ASan clean, -Werror clean; the lone intermittent failure is the
pre-existing racy plugin hot-reload-under-load test (flaky on HEAD too).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
118 lines
2.7 KiB
C++
118 lines
2.7 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::Step;
|
|
|
|
namespace
|
|
{
|
|
struct Input {
|
|
int value;
|
|
};
|
|
struct Stop {
|
|
};
|
|
|
|
// Waits for Input events, accumulating until a Stop event arrives.
|
|
class Accumulator : public Void::Fsm::Machine<Accumulator>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
int total = 0;
|
|
std::vector<int>* order = nullptr;
|
|
|
|
struct Waiting : State {
|
|
Task<Step> run() override
|
|
{
|
|
auto& m = self();
|
|
// Two typed mailboxes: pull whichever arrives.
|
|
auto in = co_await await_event<Input>();
|
|
m.total += in.value;
|
|
if (m.order) {
|
|
m.order->push_back(in.value);
|
|
}
|
|
if (in.value == 0) {
|
|
co_return succeed(m.total);
|
|
}
|
|
co_return next<Waiting>();
|
|
}
|
|
};
|
|
|
|
explicit Accumulator(bool own_worker)
|
|
: Void::Fsm::Machine<Accumulator>(
|
|
MachineOptions{.own_worker = own_worker})
|
|
{
|
|
initial<Waiting>();
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test fsm_await_event_buffered_then_consumed()
|
|
{
|
|
// Post before pumping — events buffer in the typed mailbox.
|
|
Accumulator fsm{false};
|
|
fsm.start();
|
|
fsm.post(Input{5});
|
|
fsm.post(Input{7});
|
|
fsm.post(Input{0}); // terminator
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 12);
|
|
}
|
|
|
|
Void::Testing::Test fsm_await_event_preserves_order()
|
|
{
|
|
std::vector<int> order;
|
|
Accumulator fsm{false};
|
|
fsm.order = ℴ
|
|
fsm.start();
|
|
fsm.post(Input{1});
|
|
fsm.post(Input{2});
|
|
fsm.post(Input{3});
|
|
fsm.post(Input{0});
|
|
(void)fsm.run_blocking();
|
|
const std::vector<int> expected{1, 2, 3, 0};
|
|
Void::Testing::expect(order == expected);
|
|
}
|
|
|
|
// @tag integration
|
|
Void::Testing::Test fsm_await_event_cross_thread_with_worker()
|
|
{
|
|
Accumulator fsm{true}; // own worker thread
|
|
fsm.start();
|
|
|
|
// Feed events from another thread while the worker awaits them.
|
|
std::thread producer([&] {
|
|
for (int i = 1; i <= 4; ++i) {
|
|
fsm.post(Input{i});
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
|
}
|
|
fsm.post(Input{0});
|
|
});
|
|
|
|
const auto outcome = fsm.wait(); // blocks until Stop terminator
|
|
producer.join();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, 10); // 1+2+3+4
|
|
}
|
|
|
|
Void::Testing::Test fsm_current_state_during_event_wait()
|
|
{
|
|
Accumulator fsm{false};
|
|
fsm.start();
|
|
fsm.pump(); // runs Waiting::run until it suspends on await_event<Input>
|
|
Void::Testing::expect_eq(fsm.current_state(), std::string{"Waiting"});
|
|
Void::Testing::expect(!fsm.finished());
|
|
fsm.post(Input{0});
|
|
(void)fsm.wait();
|
|
Void::Testing::expect(fsm.finished());
|
|
}
|