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

114 lines
2.7 KiB
C++

#include <Void/Data/Reflect.hpp>
#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
using Void::Coro::Task;
using Void::Fsm::MachineOptions;
using Void::Fsm::State;
using Void::Fsm::StateKey;
namespace
{
// A machine that parks at a named state and exposes its data for
// serialize/restore.
class Pipeline : public Void::Fsm::Machine<Pipeline>
{
public:
using Success = int;
struct Data {
int processed = 0;
std::string stage = "init";
};
Data data;
struct Ingest : State {
Task<StateKey> run()
{
co_await await_event<int>(); // park here
co_return next<Process>();
}
};
struct Process : State {
Task<StateKey> run()
{
auto& m = self();
m.data.processed += 1;
co_return succeed(m.data.processed);
}
};
Pipeline()
: Void::Fsm::Machine<Pipeline>(
MachineOptions{.own_worker = false})
{
initial<Ingest>();
add<Process>();
}
protected:
void on_serialize(Void::Json::Value& out) const override
{
out = Void::Json::to_json(data);
}
void on_restore(const Void::Json::Value& in) override
{
if (auto r = Void::Json::from_json<Data>(in); r.has_value()) {
data = std::move(*r);
}
}
};
} // namespace
Void::Testing::Test fsm_serialize_captures_state_and_data()
{
Pipeline fsm;
fsm.data.processed = 5;
fsm.data.stage = "running";
fsm.start();
fsm.pump(); // enters Ingest, parks on await_event<int>
const auto json = fsm.serialize();
Void::Testing::expect_eq(json["state"].as_string(), std::string{"Ingest"});
Void::Testing::expect_eq(json["data"]["processed"].as_int(),
std::int64_t{5});
Void::Testing::expect_eq(json["data"]["stage"].as_string(),
std::string{"running"});
fsm.stop();
}
Void::Testing::Test fsm_restore_resumes_at_named_state()
{
const auto saved = Void::Json::parse(
R"({"state":"Process","data":{"processed":9,"stage":"mid"}})");
Void::Testing::expect(saved.has_value());
Pipeline fsm;
const auto r = fsm.restore(*saved);
Void::Testing::expect(r.has_value());
// Starts directly at Process (skipping Ingest's event wait), with the
// restored data → succeeds with processed+1.
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 10);
}
Void::Testing::Test fsm_restore_rejects_unknown_state_and_running()
{
const auto bogus = Void::Json::parse(R"({"state":"NoSuch","data":{}})");
Void::Testing::expect(bogus.has_value());
Pipeline fsm;
Void::Testing::expect(!fsm.restore(*bogus).has_value());
fsm.start();
Void::Testing::expect(!fsm.restore(*bogus).has_value()); // running
fsm.stop();
}