Files
faumarayandClaude Opus 4.8 80293ee56f Add async/resilience/reflection primitives + FSM v2 rewrite
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>
2026-06-22 15:45:16 +03:00

246 lines
5.3 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
using Void::Coro::Task;
using Void::Fsm::ChildFailure;
using Void::Fsm::ChildSuccess;
using Void::Fsm::FsmError;
using Void::Fsm::State;
using Void::Fsm::StateKey;
namespace Fsm = Void::Fsm;
// startMachine<Sub, Next>(data) launches a full sub-FSM on the parent's shared
// executor/events/services (startup data routed to Sub's initial-state
// startup-entry), then enters Next. The engine dispatches the sub's typed
// outcome to Next's signature-matched handler — a member taking `ChildSuccess`
// (success) or `ChildFailure` (failure); the typed value is read with
// `.as<T>()`. No override, no fixed names.
namespace
{
// Sub-machine seeded via its startup-entry (run(int)): doubles the seed,
// fails (typed FsmError) if negative.
class Doubler : public Fsm::Machine<Doubler>
{
public:
using Success = int;
using Failure = Void::Fsm::FsmError;
struct States {
struct Run : State {
Task<StateKey> run(int seed)
{
if (seed < 0) {
co_return fail("negative");
}
co_return succeed(seed * 2);
}
};
};
};
class Calc : public Fsm::Machine<Calc>
{
public:
using Success = std::string;
int input = 0;
struct States {
struct Start : State {
Task<StateKey> run()
{
co_return startMachine<Doubler, AfterDouble>(
self().input);
}
};
struct AfterDouble : State {
Task<StateKey> ok(ChildSuccess s)
{
co_return succeed("doubled:" +
std::to_string(s.as<int>()));
}
Task<StateKey> no(ChildFailure f)
{
co_return succeed("failed:" +
f.as<FsmError>().message);
}
};
};
explicit Calc(int in) : input(in) {}
};
} // namespace
// @tag integration
Void::Testing::Test submachine_success_dispatches_on_success()
{
Calc fsm{5};
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"doubled:10"});
}
// @tag integration
Void::Testing::Test submachine_failure_dispatches_on_failure()
{
Calc fsm{-1};
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, std::string{"failed:negative"});
}
// ── void-result sub-machine ─────────────────────────────────────────────────
namespace
{
class Pinger : public Fsm::Machine<Pinger>
{
public:
struct States {
struct Ping : State {
Task<StateKey> run() { co_return succeed(); }
};
};
};
class Host : public Fsm::Machine<Host>
{
public:
using Success = int;
struct States {
struct Go : State {
Task<StateKey> run()
{
co_return startMachine<Pinger, Done>();
}
};
struct Done : State {
// void-result sub: ChildSuccess box has no value.
Task<StateKey> ok(ChildSuccess)
{
co_return succeed(42);
}
};
};
};
} // namespace
// @tag integration
Void::Testing::Test submachine_void_result_calls_success_handler()
{
Host fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 42);
}
// ── chained startMachine (a continuation state launches another) ─────────────
namespace
{
class Chain : public Fsm::Machine<Chain>
{
public:
using Success = int;
struct States {
struct A : State {
Task<StateKey> run()
{
co_return startMachine<Doubler, B>(3); // 3 -> 6
}
};
struct B : State {
Task<StateKey> ok(ChildSuccess s)
{
// 6 -> 12
co_return startMachine<Doubler, C>(s.as<int>());
}
};
struct C : State {
Task<StateKey> ok(ChildSuccess s)
{
co_return succeed(s.as<int>());
}
};
};
};
} // namespace
// @tag integration
Void::Testing::Test submachine_chains_through_continuation_states()
{
Chain fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 12);
}
// ── the sub-machine shares the parent's event mailbox ────────────────────────
namespace
{
struct Tick {
};
class WaitSub : public Fsm::Machine<WaitSub>
{
public:
using Success = int;
struct States {
struct W : State {
Task<StateKey> run()
{
co_await await_event<Tick>();
co_return succeed(7);
}
};
};
};
class Outer : public Fsm::Machine<Outer>
{
public:
using Success = int;
struct States {
struct Begin : State {
Task<StateKey> run()
{
co_return startMachine<WaitSub, Fin>();
}
};
struct Fin : State {
Task<StateKey> ok(ChildSuccess s)
{
co_return succeed(s.as<int>());
}
};
};
};
} // namespace
// @tag integration
Void::Testing::Test submachine_shares_parent_event_mailbox()
{
Outer fsm;
fsm.start();
fsm.post(Tick{}); // delivered to the sub-machine awaiting on the shared ctx
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 7);
}