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>
This commit is contained in:
1 parent
43b5a81504
commit
80293ee56f
93 files changed
+8500
-3312
No files matched your search
@@ -0,0 +1,40 @@
|
||||
#include <Void/Data/Csv.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Rec {
|
||||
std::string name = "";
|
||||
int age = 0;
|
||||
double score = 0;
|
||||
bool ok = false;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test csv_header_and_to_row()
|
||||
{
|
||||
const auto h = Void::Csv::header_row<Rec>();
|
||||
Void::Testing::expect_eq(h.size(), std::size_t{4});
|
||||
Void::Testing::expect(h[0] == "name" && h[1] == "age" && h[3] == "ok");
|
||||
|
||||
Rec r{"neo", 30, 9.5, true};
|
||||
const auto row = Void::Csv::to_row(r);
|
||||
Void::Testing::expect(row[0] == "neo" && row[1] == "30" &&
|
||||
row[3] == "true");
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test csv_from_row_round_trip()
|
||||
{
|
||||
Rec r{"trin", 28, 7.25, false};
|
||||
const auto back = Void::Csv::from_row<Rec>(Void::Csv::to_row(r));
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect_eq(back->name, std::string{"trin"});
|
||||
Void::Testing::expect_eq(back->age, 28);
|
||||
Void::Testing::expect(back->score > 7.2 && back->score < 7.3);
|
||||
Void::Testing::expect_eq(back->ok, false);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
#include <Void/Data/Json.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
using Void::Testing::Test;
|
||||
|
||||
const char* kDoc = R"({
|
||||
"name": "root",
|
||||
"nested": { "a": { "b": [10, 20, 30] } },
|
||||
"weird/key": 7,
|
||||
"flag": true
|
||||
})";
|
||||
|
||||
struct JsonQuery : Void::Testing::Suite<JsonQuery> {
|
||||
// ── JSON Pointer (at) ─────────────────────────────────────
|
||||
Test pointer_walks_objects_and_arrays()
|
||||
{
|
||||
auto doc = Void::Json::parse(kDoc);
|
||||
Void::Testing::expect(doc.has_value());
|
||||
const auto* v = doc->at("/nested/a/b/1");
|
||||
Void::Testing::expect(v != nullptr);
|
||||
Void::Testing::expect_eq(v->as_number(), 20.0);
|
||||
}
|
||||
|
||||
Test empty_pointer_is_whole_document()
|
||||
{
|
||||
auto doc = Void::Json::parse(kDoc);
|
||||
const auto* v = doc->at("");
|
||||
Void::Testing::expect(v == &*doc);
|
||||
}
|
||||
|
||||
Test missing_path_returns_null()
|
||||
{
|
||||
auto doc = Void::Json::parse(kDoc);
|
||||
Void::Testing::expect(doc->at("/nested/missing") == nullptr);
|
||||
Void::Testing::expect(doc->at("/nested/a/b/9") == nullptr);
|
||||
Void::Testing::expect(doc->at("/flag/x") == nullptr);
|
||||
}
|
||||
|
||||
Test escaped_token()
|
||||
{
|
||||
auto doc = Void::Json::parse(kDoc);
|
||||
const auto* v = doc->at("/weird~1key"); // ~1 -> '/'
|
||||
Void::Testing::expect(v != nullptr);
|
||||
Void::Testing::expect_eq(v->as_number(), 7.0);
|
||||
}
|
||||
|
||||
// ── SAX streaming parse ───────────────────────────────────
|
||||
Test sax_fires_events_in_order()
|
||||
{
|
||||
std::vector<std::string> events;
|
||||
Void::Json::SaxHandler h;
|
||||
h.on_object_begin = [&] { events.push_back("{"); };
|
||||
h.on_object_end = [&] { events.push_back("}"); };
|
||||
h.on_array_begin = [&] { events.push_back("["); };
|
||||
h.on_array_end = [&] { events.push_back("]"); };
|
||||
h.on_key = [&](std::string_view k) {
|
||||
events.push_back("key:" + std::string{k});
|
||||
};
|
||||
h.on_number = [&](double d) {
|
||||
events.push_back("num:" + std::to_string((int)d));
|
||||
};
|
||||
h.on_bool = [&](bool b) {
|
||||
events.push_back(b ? "true" : "false");
|
||||
};
|
||||
h.on_string = [&](std::string_view s) {
|
||||
events.push_back("str:" + std::string{s});
|
||||
};
|
||||
|
||||
auto err =
|
||||
Void::Json::parse_sax(R"({"a":[1,2],"b":"x","c":true})", h);
|
||||
Void::Testing::expect(!err.has_value());
|
||||
|
||||
const std::vector<std::string> expected{
|
||||
"{", "key:a", "[", "num:1", "num:2", "]",
|
||||
"key:b", "str:x", "key:c", "true", "}"};
|
||||
Void::Testing::expect(events == expected);
|
||||
}
|
||||
|
||||
Test sax_reports_malformed_input()
|
||||
{
|
||||
Void::Json::SaxHandler h;
|
||||
auto err = Void::Json::parse_sax(R"({"a": })", h);
|
||||
Void::Testing::expect(err.has_value());
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
@@ -0,0 +1,61 @@
|
||||
#include <Void/Data/Reflect.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace J = Void::Json;
|
||||
|
||||
namespace
|
||||
{
|
||||
struct Address {
|
||||
std::string city = "?";
|
||||
int zip = 0;
|
||||
};
|
||||
struct User {
|
||||
[[= J::Rename{"user_id"}]] int id = 0;
|
||||
[[= J::Skip{}]] int cache = 99;
|
||||
[[= J::Required{}]] std::string name = "";
|
||||
[[= J::Flatten{}]] Address addr{};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test json_annotations_to_json()
|
||||
{
|
||||
User u{7, 99, "neo", Address{"zion", 100}};
|
||||
const auto j = J::to_json(u);
|
||||
Void::Testing::expect(j.find("user_id") != nullptr); // Rename
|
||||
Void::Testing::expect_eq(j.find("user_id")->as_int(), std::int64_t{7});
|
||||
Void::Testing::expect(j.find("cache") == nullptr); // Skip
|
||||
Void::Testing::expect(j.find("name") != nullptr);
|
||||
// Flatten: Address keys appear at top level, "addr" does not.
|
||||
Void::Testing::expect(j.find("city") != nullptr);
|
||||
Void::Testing::expect_eq(j.find("city")->as_string(), std::string{"zion"});
|
||||
Void::Testing::expect(j.find("zip") != nullptr);
|
||||
Void::Testing::expect(j.find("addr") == nullptr);
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test json_annotations_round_trip()
|
||||
{
|
||||
User u{7, 99, "neo", Address{"zion", 100}};
|
||||
const auto j = J::to_json(u);
|
||||
const auto back = J::from_json<User>(j);
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect_eq(back->id, 7);
|
||||
Void::Testing::expect_eq(back->name, std::string{"neo"});
|
||||
Void::Testing::expect_eq(back->addr.city, std::string{"zion"});
|
||||
Void::Testing::expect_eq(back->addr.zip, 100);
|
||||
Void::Testing::expect_eq(back->cache, 99); // Skip keeps default
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test json_required_missing_is_error()
|
||||
{
|
||||
const auto j = J::parse(R"({"user_id":1,"city":"x","zip":2})");
|
||||
Void::Testing::expect(j.has_value());
|
||||
const auto r = J::from_json<User>(*j);
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect_eq(r.error().field, std::string{"name"});
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
#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
|
||||
Reference in new issue
Block a user