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>
193 lines
6.1 KiB
C++
193 lines
6.1 KiB
C++
#include <Void/Data/Reflect.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Suite.hpp>
|
|
|
|
#include <array>
|
|
#include <chrono>
|
|
#include <map>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
#ifdef VOID_HAS_JSON_REFLECTION
|
|
|
|
namespace
|
|
{
|
|
using namespace std::chrono_literals;
|
|
using Void::Json::from_json;
|
|
using Void::Json::to_json;
|
|
using Void::Testing::Test;
|
|
|
|
struct Circle {
|
|
double radius = 0;
|
|
};
|
|
struct Square {
|
|
double side = 0;
|
|
};
|
|
using Shape = std::variant<Circle, Square>;
|
|
|
|
struct Sample {
|
|
std::string name;
|
|
std::chrono::milliseconds latency{0};
|
|
std::chrono::seconds ttl{0};
|
|
std::array<int, 3> rgb{0, 0, 0};
|
|
std::map<int, std::string> by_id;
|
|
std::optional<std::chrono::milliseconds> deadline;
|
|
std::chrono::system_clock::time_point created{};
|
|
};
|
|
|
|
struct ReflectChrono : Void::Testing::Suite<ReflectChrono> {
|
|
// ── duration as a unit-suffixed string ────────────────────
|
|
Test duration_emits_unit_string()
|
|
{
|
|
const Sample s{.latency = 250ms, .ttl = 30s};
|
|
const auto j = to_json(s);
|
|
Void::Testing::expect_eq(j["latency"].as_string(),
|
|
std::string{"250ms"});
|
|
Void::Testing::expect_eq(j["ttl"].as_string(),
|
|
std::string{"30s"});
|
|
}
|
|
|
|
Test duration_round_trips()
|
|
{
|
|
const Sample s{.name = "n",
|
|
.latency = 1500ms,
|
|
.ttl = 5s,
|
|
.rgb = {1, 2, 3}};
|
|
const auto back = from_json<Sample>(to_json(s));
|
|
Void::Testing::expect(back.has_value());
|
|
Void::Testing::expect(back->latency == 1500ms);
|
|
Void::Testing::expect(back->ttl == 5s);
|
|
}
|
|
|
|
Test optional_duration_round_trips()
|
|
{
|
|
Sample s;
|
|
s.deadline = 750ms;
|
|
auto back = from_json<Sample>(to_json(s));
|
|
Void::Testing::expect(back.has_value());
|
|
Void::Testing::expect(back->deadline.has_value());
|
|
Void::Testing::expect(*back->deadline == 750ms);
|
|
|
|
s.deadline.reset();
|
|
back = from_json<Sample>(to_json(s));
|
|
Void::Testing::expect(back.has_value());
|
|
Void::Testing::expect(!back->deadline.has_value());
|
|
}
|
|
|
|
Test invalid_duration_is_error()
|
|
{
|
|
auto j = Void::Json::object();
|
|
j.as_object().emplace_back(
|
|
"latency", Void::Json::Value{std::string{"nonsense"}});
|
|
const auto back = from_json<Sample>(j);
|
|
Void::Testing::expect(!back.has_value());
|
|
}
|
|
|
|
// ── time_point as ms-since-epoch string ───────────────────
|
|
Test time_point_emits_ms_string()
|
|
{
|
|
Sample s;
|
|
s.created = std::chrono::system_clock::time_point{
|
|
std::chrono::milliseconds{1750000000123}};
|
|
const auto j = to_json(s);
|
|
Void::Testing::expect_eq(j["created"].as_string(),
|
|
std::string{"1750000000123ms"});
|
|
}
|
|
|
|
Test time_point_round_trips()
|
|
{
|
|
Sample s;
|
|
s.created = std::chrono::system_clock::time_point{
|
|
std::chrono::milliseconds{1750000000123}};
|
|
const auto back = from_json<Sample>(to_json(s));
|
|
Void::Testing::expect(back.has_value());
|
|
const auto ms =
|
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
back->created.time_since_epoch())
|
|
.count();
|
|
Void::Testing::expect_eq(ms,
|
|
static_cast<long long>(1750000000123));
|
|
}
|
|
|
|
Test invalid_time_point_is_error()
|
|
{
|
|
auto j = Void::Json::object();
|
|
j.as_object().emplace_back(
|
|
"created", Void::Json::Value{std::string{"nope"}});
|
|
const auto back = from_json<Sample>(j);
|
|
Void::Testing::expect(!back.has_value());
|
|
}
|
|
|
|
// ── std::array ────────────────────────────────────────────
|
|
Test array_round_trips()
|
|
{
|
|
const Sample s{.rgb = {10, 20, 30}};
|
|
const auto j = to_json(s);
|
|
Void::Testing::expect(j["rgb"].is_array());
|
|
Void::Testing::expect_eq(j["rgb"].as_array().size(),
|
|
std::size_t{3});
|
|
const auto back = from_json<Sample>(j);
|
|
Void::Testing::expect(back.has_value());
|
|
Void::Testing::expect_eq(back->rgb[2], 30);
|
|
}
|
|
|
|
Test array_wrong_length_is_error()
|
|
{
|
|
auto arr = Void::Json::array();
|
|
arr.as_array().push_back(Void::Json::Value{1.0});
|
|
arr.as_array().push_back(Void::Json::Value{2.0});
|
|
auto j = Void::Json::object();
|
|
j.as_object().emplace_back("rgb", std::move(arr));
|
|
const auto back = from_json<Sample>(j);
|
|
Void::Testing::expect(!back.has_value());
|
|
}
|
|
|
|
// ── non-string-keyed map ──────────────────────────────────
|
|
Test int_keyed_map_round_trips()
|
|
{
|
|
Sample s;
|
|
s.by_id = {{1, "one"}, {2, "two"}};
|
|
const auto j = to_json(s);
|
|
Void::Testing::expect(j["by_id"].is_array());
|
|
const auto back = from_json<Sample>(j);
|
|
Void::Testing::expect(back.has_value());
|
|
Void::Testing::expect_eq(back->by_id.size(), std::size_t{2});
|
|
Void::Testing::expect_eq(back->by_id.at(1), std::string{"one"});
|
|
Void::Testing::expect_eq(back->by_id.at(2), std::string{"two"});
|
|
}
|
|
|
|
// ── std::variant (type-name tagged) ───────────────────────
|
|
Test variant_round_trips_each_alternative()
|
|
{
|
|
const Shape c = Circle{2.0};
|
|
const auto jc = to_json(c);
|
|
Void::Testing::expect_eq(jc["type"].as_string(),
|
|
std::string{"Circle"});
|
|
const auto bc = from_json<Shape>(jc);
|
|
Void::Testing::expect(bc.has_value());
|
|
Void::Testing::expect(std::holds_alternative<Circle>(*bc));
|
|
Void::Testing::expect_eq(std::get<Circle>(*bc).radius, 2.0);
|
|
|
|
const Shape sq = Square{3.0};
|
|
const auto bs = from_json<Shape>(to_json(sq));
|
|
Void::Testing::expect(bs.has_value());
|
|
Void::Testing::expect(std::holds_alternative<Square>(*bs));
|
|
Void::Testing::expect_eq(std::get<Square>(*bs).side, 3.0);
|
|
}
|
|
|
|
Test variant_unknown_tag_is_error()
|
|
{
|
|
auto j = Void::Json::object();
|
|
j.as_object().emplace_back(
|
|
"type", Void::Json::Value{std::string{"Triangle"}});
|
|
j.as_object().emplace_back("value", Void::Json::object());
|
|
const auto back = from_json<Shape>(j);
|
|
Void::Testing::expect(!back.has_value());
|
|
}
|
|
};
|
|
} // namespace
|
|
|
|
#endif // VOID_HAS_JSON_REFLECTION
|