Files
Library/tests/standalone/Fsm/Entry.cpp
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

137 lines
3.5 KiB
C++

#include <Void/Fsm/Fsm.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
using Void::Coro::Task;
using Void::Fsm::ChildSuccess;
using Void::Fsm::State;
using Void::Fsm::StateKey;
namespace Fsm = Void::Fsm;
// A state's entry is inferred by signature (a 0-arg member returning StateKey /
// Task<StateKey>). When several members match, one annotated
// `[[= Void::Fsm::entry]]` is preferred; otherwise the first-declared wins. A
// startup-data overload run(Cfg) is selected when the machine is launched with
// data, and the nullary entry otherwise.
namespace
{
// Two nullary entry candidates; the annotated one is chosen even though it
// is not declared first.
class Annotated : public Fsm::Machine<Annotated>
{
public:
using Success = int;
struct States {
struct Pick : State {
Task<StateKey> first() // declared first
{
co_return succeed(1);
}
[[= Fsm::entry]] Task<StateKey> chosen()
{
co_return succeed(2);
}
};
};
};
// Two nullary entry candidates, none annotated → first-declared wins.
class FirstWins : public Fsm::Machine<FirstWins>
{
public:
using Success = int;
struct States {
struct Pick : State {
Task<StateKey> first() { co_return succeed(1); }
Task<StateKey> second() { co_return succeed(2); }
};
};
};
} // namespace
// @tag integration
Void::Testing::Test annotated_entry_beats_first_declared()
{
Annotated fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 2); // the [[= Fsm::entry]] member ran
}
// @tag integration
Void::Testing::Test first_declared_entry_wins_without_annotation()
{
FirstWins fsm;
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 1); // first-declared ran
}
// ── startup-data overload selection ─────────────────────────────────────────
namespace
{
// Begin has both a nullary entry and a startup-data overload. The right one
// is picked by whether the machine was launched with data.
class Seeded : public Fsm::Machine<Seeded>
{
public:
using Success = int;
struct States {
struct Begin : State {
Task<StateKey> run() // no startup data
{
co_return succeed(0);
}
Task<StateKey> run(int seed) // with startup data
{
co_return succeed(seed);
}
};
};
};
class Launcher : public Fsm::Machine<Launcher>
{
public:
using Success = int;
struct States {
struct Go : State {
Task<StateKey> run()
{
co_return startMachine<Seeded, After>(7);
}
};
struct After : State {
Task<StateKey> ok(ChildSuccess s)
{
co_return succeed(s.as<int>());
}
};
};
};
} // namespace
// @tag integration
Void::Testing::Test startup_entry_used_when_launched_with_data()
{
Launcher fsm; // Seeded launched via startMachine<Seeded, After>(7)
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 7); // run(int) selected
}
// @tag integration
Void::Testing::Test nullary_entry_used_without_startup_data()
{
Seeded fsm; // started normally, no startup data
const auto outcome = fsm.run_blocking();
Void::Testing::expect(outcome.has_value());
Void::Testing::expect_eq(*outcome, 0); // run() selected
}