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

299 lines
6.8 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <thread>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::State;
using Void::Fsm::StateKey;
namespace Fsm = Void::Fsm;
using namespace std::chrono_literals;
// ── fluent events + free jumps ──────────────────────────────────────────────
namespace
{
struct Add {
int n;
};
// Sums two Add events, posted in one fluent send() call.
class Sum : public Fsm::Machine<Sum>
{
public:
using Success = int;
int total = 0;
struct States {
struct Take : State {
Task<StateKey> run()
{
auto a = co_await await_event<Add>();
self().total += a.n;
auto b = co_await await_event<Add>();
self().total += b.n;
co_return succeed(self().total);
}
};
};
};
} // namespace
// @tag integration
Void::Testing::Test send_posts_multiple_events()
{
Sum fsm;
fsm.start();
fsm.send(Add{3}, Add{4}); // one fluent call, two events
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 7);
}
namespace
{
// A and C are not connected by any declared edge; goto_state<C>() jumps
// from A to C freely.
class Hop : public Fsm::Machine<Hop>
{
public:
using Success = int;
struct States {
struct A : State {
Task<StateKey> run() { co_return goto_state<C>(); }
};
struct B : State {
Task<StateKey> run() { co_return succeed(1); }
};
struct C : State {
Task<StateKey> run() { co_return succeed(3); }
};
};
};
} // namespace
// @tag integration
Void::Testing::Test goto_state_jumps_to_any_registered_state()
{
Hop fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 3); // went A -> C, skipping B
}
namespace
{
struct Tick {
};
// Loops in Loop on each Tick; an external jump<Done>() redirects at the
// next boundary.
class Jumper : public Fsm::Machine<Jumper>
{
public:
using Success = int;
struct States {
struct Loop : State {
Task<StateKey> run()
{
co_await await_event<Tick>();
co_return next<Loop>();
}
};
struct Done : State {
Task<StateKey> run() { co_return succeed(99); }
};
};
explicit Jumper(MachineOptions o = {}) : Fsm::Machine<Jumper>(o) {}
};
} // namespace
Void::Testing::Test jump_redirects_at_next_boundary()
{
Jumper fsm{MachineOptions{.own_worker = false}};
fsm.start();
while (fsm.pump() > 0) {
} // enter Loop, park on Tick
fsm.send(Tick{});
while (fsm.pump() > 0) {
} // Loop -> next<Loop> -> park again
fsm.jump<Jumper::States::Done>(); // request a jump
fsm.send(Tick{});
while (fsm.pump() > 0) {
} // Loop finishes -> boundary applies the jump -> Done
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 99);
}
// ── lifecycle control ───────────────────────────────────────────────────────
namespace
{
// Increments a machine-owned counter each run (data survives reset()).
class Counter : public Fsm::Machine<Counter>
{
public:
using Success = int;
int runs = 0;
struct States {
struct Once : State {
Task<StateKey> run()
{
co_return succeed(++self().runs);
}
};
};
};
} // namespace
// @tag integration
Void::Testing::Test restart_reruns_the_machine()
{
Counter fsm;
const auto first = fsm.run_blocking();
Void::Testing::expect_eq(*first, 1);
fsm.restart();
const auto second = fsm.wait();
Void::Testing::expect_eq(*second, 2);
}
namespace
{
class Pick : public Fsm::Machine<Pick>
{
public:
using Success = int;
struct States {
struct A : State {
Task<StateKey> run() { co_return succeed(1); }
};
struct B : State {
Task<StateKey> run() { co_return succeed(2); }
};
}; // initial = A
};
} // namespace
// @tag integration
Void::Testing::Test restart_at_changes_initial_state()
{
Pick fsm;
const auto first = fsm.run_blocking();
Void::Testing::expect_eq(*first, 1); // A
fsm.restart_at<Pick::States::B>();
const auto second = fsm.wait();
Void::Testing::expect_eq(*second, 2); // B
}
// ── runtime mutation (Idle only) ────────────────────────────────────────────
namespace
{
class Mut : public Fsm::Machine<Mut>
{
public:
using Success = int;
struct States {
struct A : State {
Task<StateKey> run() { co_return succeed(1); }
};
struct B : State {
Task<StateKey> run() { co_return succeed(2); }
};
struct C : State {
Task<StateKey> run() { co_return succeed(3); }
};
};
// Not in States — added via replace<>.
struct Extra : State {
Task<StateKey> run() { co_return succeed(7); }
};
};
} // namespace
Void::Testing::Test remove_and_set_initial_before_start()
{
Mut fsm;
Void::Testing::expect(fsm.has_state<Mut::States::C>());
fsm.remove<Mut::States::C>();
Void::Testing::expect(!fsm.has_state<Mut::States::C>());
Void::Testing::expect_eq(fsm.describe().size(), std::size_t{2});
fsm.set_initial<Mut::States::B>();
const auto outcome = fsm.run_blocking();
Void::Testing::expect_eq(*outcome, 2);
}
Void::Testing::Test replace_swaps_a_state_and_keeps_initial()
{
Mut fsm;
// A was initial -> Extra becomes initial.
fsm.replace<Mut::States::A, Mut::Extra>();
Void::Testing::expect(!fsm.has_state<Mut::States::A>());
Void::Testing::expect(fsm.has_state<Mut::Extra>());
const auto outcome = fsm.run_blocking();
Void::Testing::expect_eq(*outcome, 7);
}
// ── pause / resume ──────────────────────────────────────────────────────────
namespace
{
struct Go {
};
class Sleeper : public Fsm::Machine<Sleeper>
{
public:
using Success = int;
struct States {
struct W : State {
Task<StateKey> run()
{
co_await await_event<Go>();
co_return succeed(5);
}
};
};
};
} // namespace
// @tag integration
Void::Testing::Test pause_holds_events_until_resume()
{
Sleeper fsm;
fsm.start();
fsm.pause();
fsm.post(Go{}); // buffered while paused, not delivered
std::this_thread::sleep_for(20ms);
Void::Testing::expect(!fsm.finished()); // still parked, event held
fsm.resume();
const auto outcome = fsm.wait();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 5);
}