A round of new universal capabilities (all generic — templated/reflection based, no app coupling), verified by the test suite (1103 passing): Data reflection: - Reflect.hpp now (de)serializes chrono::duration, chrono::time_point, std::variant (tagged), std::array, and non-string-keyed std::map; Meta.hpp gains is_time_point_v et al. Async: - EventBus gains Mode::Async (worker-thread dispatch + flush()). - ThreadPool gains a bounded queue, try_submit(), and rejection stats. New modules / containers: - Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker, ResourceMonitor. - Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V> (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats). - System: process/CPU/memory metrics (Metrics.hpp) and a streaming Process API. Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions, history states, deferred events), Configuration/Logging reflection helpers, and the accompanying test suites. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
212 lines
5.9 KiB
C++
212 lines
5.9 KiB
C++
#include <Void/Fsm/Fsm.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <chrono>
|
|
#include <string>
|
|
#include <variant>
|
|
#include <vector>
|
|
#include <iostream>
|
|
|
|
using Void::Coro::Task;
|
|
using Void::Fsm::MachineOptions;
|
|
using Void::Fsm::State;
|
|
using Void::Fsm::StateKey;
|
|
using Void::Fsm::Transition;
|
|
using namespace std::chrono_literals;
|
|
|
|
// ── retry / next_or
|
|
// ───────────────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
// Attempt fails the first `fail_n` times, then succeeds. with_retry
|
|
// keeps trying; next_or routes exhaustion to a fallback.
|
|
class Flaky : public Void::Fsm::Machine<Flaky>
|
|
{
|
|
public:
|
|
|
|
using Success = std::string;
|
|
using Failure = Void::Fsm::FsmError;
|
|
|
|
int fail_n = 0;
|
|
int attempts = 0;
|
|
int retries; // how many tries with_retry gets
|
|
|
|
struct States {
|
|
struct Try : State {
|
|
Task<StateKey> run()
|
|
{
|
|
auto& m = self();
|
|
const bool ok = co_await with_retry(
|
|
m.retries, 1ms, [&m]() -> Task<bool> {
|
|
++m.attempts;
|
|
co_return m.attempts > m.fail_n;
|
|
});
|
|
co_return next_or<Fallback>(
|
|
ok ? succeed(std::string{"ok"})
|
|
: fail("exhausted"));
|
|
}
|
|
};
|
|
|
|
struct Fallback : State {
|
|
Task<StateKey> run()
|
|
{
|
|
co_return succeed(std::string{"fallback"});
|
|
}
|
|
};
|
|
};
|
|
|
|
explicit Flaky(int fails, int tries) : fail_n(fails), retries(tries)
|
|
{
|
|
} // own worker
|
|
};
|
|
} // namespace
|
|
|
|
// @tag integration
|
|
Void::Testing::Test retry_eventually_succeeds()
|
|
{
|
|
Flaky fsm{2, 5}; // fails twice, 5 tries available -> succeeds on 3rd
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, std::string{"ok"});
|
|
Void::Testing::expect_eq(fsm.attempts, 3);
|
|
}
|
|
|
|
// @tag integration
|
|
Void::Testing::Test retry_exhausts_routes_to_fallback()
|
|
{
|
|
Flaky fsm{10, 3}; // always fails within 3 tries -> next_or<Fallback>
|
|
const auto outcome = fsm.run_blocking();
|
|
Void::Testing::expect(outcome.has_value());
|
|
Void::Testing::expect_eq(*outcome, std::string{"fallback"});
|
|
Void::Testing::expect_eq(fsm.attempts, 3);
|
|
}
|
|
|
|
// ── interceptors
|
|
// ──────────────────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
class Pipe : public Void::Fsm::Machine<Pipe>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
struct States {
|
|
struct A : State {
|
|
Task<StateKey> run() { co_return next<B>(); }
|
|
};
|
|
struct B : State {
|
|
Task<StateKey> run() { co_return succeed(1); }
|
|
};
|
|
};
|
|
Pipe()
|
|
: Void::Fsm::Machine<Pipe>(MachineOptions{.own_worker = false})
|
|
{
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
Void::Testing::Test interceptor_before_after_order()
|
|
{
|
|
Pipe fsm;
|
|
std::vector<std::string> events;
|
|
fsm.add_interceptor(
|
|
{.before =
|
|
[&](const Transition& t) { events.push_back("before:" + t.to); },
|
|
.after =
|
|
[&](std::string_view s) {
|
|
events.push_back("after:" + std::string{s});
|
|
}});
|
|
(void)fsm.run_blocking();
|
|
|
|
// A entered (before:A), A runs+finishes (after:A), B entered
|
|
// (before:B), B runs (after:B).
|
|
const std::vector<std::string> expected{"before:A", "after:A", "before:B",
|
|
"after:B"};
|
|
Void::Testing::expect(events == expected);
|
|
}
|
|
|
|
Void::Testing::Test interceptors_run_in_registration_order()
|
|
{
|
|
Pipe fsm;
|
|
std::vector<int> order;
|
|
fsm.add_interceptor(
|
|
{.before = [&](const Transition&) { order.push_back(1); }});
|
|
fsm.add_interceptor(
|
|
{.before = [&](const Transition&) { order.push_back(2); }});
|
|
(void)fsm.run_blocking();
|
|
// Two transitions (A, B) x two interceptors → 1,2,1,2.
|
|
const std::vector<int> expected{1, 2, 1, 2};
|
|
Void::Testing::expect(order == expected);
|
|
}
|
|
|
|
// ── introspection / diagram
|
|
// ───────────────────────────────────────────────────
|
|
|
|
namespace
|
|
{
|
|
struct Go {
|
|
};
|
|
struct Cancel {
|
|
};
|
|
|
|
// Imperative event handling: Idle awaits Go or Cancel and routes.
|
|
class Flow : public Void::Fsm::Machine<Flow>
|
|
{
|
|
public:
|
|
|
|
using Success = int;
|
|
|
|
struct States {
|
|
struct Idle : State {
|
|
Task<StateKey> run()
|
|
{
|
|
auto ev = co_await await_any<Go, Cancel>();
|
|
co_return std::holds_alternative<Go>(ev)
|
|
? next<Active>()
|
|
: next<Done>();
|
|
}
|
|
};
|
|
struct Active : State {
|
|
Task<StateKey> run() { co_return next<Done>(); }
|
|
};
|
|
struct Done : State {
|
|
Task<StateKey> run() { co_return succeed(0); }
|
|
};
|
|
};
|
|
Flow() {}
|
|
};
|
|
} // namespace
|
|
|
|
// describe() is node-level: behaviour (and therefore transitions) is chosen
|
|
// imperatively inside each state's entry, so edges are not statically knowable.
|
|
Void::Testing::Test describe_lists_states_node_level()
|
|
{
|
|
Flow fsm;
|
|
const auto states = fsm.describe();
|
|
Void::Testing::expect_eq(states.size(), std::size_t{3});
|
|
|
|
// Registration order; Idle is the initial state.
|
|
Void::Testing::expect_eq(states[0].name, std::string{"Idle"});
|
|
Void::Testing::expect(states[0].initial);
|
|
Void::Testing::expect_eq(states[1].name, std::string{"Active"});
|
|
Void::Testing::expect(!states[1].initial);
|
|
Void::Testing::expect_eq(states[2].name, std::string{"Done"});
|
|
}
|
|
|
|
Void::Testing::Test to_mermaid_lists_nodes_and_initial()
|
|
{
|
|
Flow fsm;
|
|
const auto diagram = fsm.to_mermaid();
|
|
std::cout << diagram << std::endl;
|
|
Void::Testing::expect(diagram.find("stateDiagram-v2") != std::string::npos);
|
|
Void::Testing::expect(diagram.find("[*] --> Idle") != std::string::npos);
|
|
// One node per registered state.
|
|
Void::Testing::expect(diagram.find("\n Idle\n") != std::string::npos);
|
|
Void::Testing::expect(diagram.find("\n Active\n") != std::string::npos);
|
|
Void::Testing::expect(diagram.find("\n Done\n") != std::string::npos);
|
|
}
|