feat(configuration): bounded schemas, provenance/redaction, and async transactional ReloadController
This commit is contained in:
1 parent
a24e086e47
commit
1dc51d5c43
14 files changed
+3351
No files matched your search
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(VoidConfigurationAsyncOnlyConsumer LANGUAGES CXX)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS ConfigurationAsync)
|
||||
|
||||
add_executable(void_configuration_async_only_consumer main.cpp)
|
||||
target_link_libraries(void_configuration_async_only_consumer PRIVATE Void::ConfigurationAsync)
|
||||
target_compile_features(void_configuration_async_only_consumer PRIVATE cxx_std_26)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME ConfigurationAsyncOnlyConsumer COMMAND void_configuration_async_only_consumer)
|
||||
@@ -0,0 +1,20 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Configuration/ReloadController.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
struct AsyncCfg {
|
||||
std::string mode{"ok"};
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Void::Async::ManualExecutor exec;
|
||||
Void::Configuration::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"mode", "fast"}});
|
||||
auto schema = Void::Configuration::Schema<AsyncCfg>::from_reflection();
|
||||
auto ctrl = Void::Configuration::ReloadController<AsyncCfg>::create(
|
||||
schema, std::move(ss), {.executor = &exec});
|
||||
if (!ctrl) return 1;
|
||||
return (*ctrl)->current().values->mode == "fast" ? 0 : 2;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
cmake_minimum_required(VERSION 4.0)
|
||||
project(VoidConfigurationOnlyConsumer LANGUAGES CXX)
|
||||
|
||||
find_package(Void CONFIG REQUIRED COMPONENTS Configuration)
|
||||
|
||||
add_executable(void_configuration_only_consumer main.cpp)
|
||||
target_link_libraries(void_configuration_only_consumer PRIVATE Void::Configuration)
|
||||
target_compile_features(void_configuration_only_consumer PRIVATE cxx_std_26)
|
||||
|
||||
enable_testing()
|
||||
add_test(NAME ConfigurationOnlyConsumer COMMAND void_configuration_only_consumer)
|
||||
@@ -0,0 +1,21 @@
|
||||
#include <Void/Configuration/Bind.hpp>
|
||||
#include <Void/Configuration/Config.hpp>
|
||||
#include <Void/Configuration/Schema.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
struct MinimalCfg {
|
||||
[[= Void::Configuration::Required{}]] std::string name;
|
||||
[[= Void::Configuration::IntRange{1, 100}]] int workers{4};
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Void::Configuration::SourceSet ss;
|
||||
ss.add_defaults_map("def", {{"name", "svc"}, {"workers", "8"}});
|
||||
auto schema = Void::Configuration::Schema<MinimalCfg>::from_reflection();
|
||||
auto merged = Void::Configuration::load_sources(ss, schema.limits());
|
||||
if (!merged) return 1;
|
||||
auto val = Void::Configuration::validate<MinimalCfg>(*merged, schema);
|
||||
return val && val->value.name == "svc" && val->value.workers == 8 ? 0 : 2;
|
||||
}
|
||||
@@ -262,6 +262,26 @@ if(_has_plugin)
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||
endif()
|
||||
|
||||
has_modules(_has_configuration Configuration Core Functional System Data Time LogState)
|
||||
if(_has_configuration)
|
||||
run_consumer(ConfigurationOnly "${install_prefix}" "ConfigurationOnly"
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_Boost=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_LibArchive=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_OpenSSL=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||
endif()
|
||||
|
||||
has_modules(_has_configuration_async ConfigurationAsync Configuration SystemAsync Async Time Functional LogState)
|
||||
if(_has_configuration_async)
|
||||
run_consumer(ConfigurationAsyncOnly "${install_prefix}" "ConfigurationAsyncOnly"
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_Boost=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_LibArchive=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_OpenSSL=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_PkgConfig=TRUE
|
||||
-DCMAKE_DISABLE_FIND_PACKAGE_SQLite3=TRUE)
|
||||
endif()
|
||||
|
||||
# Foundation consumer with Core-formatting bridge and zero external packages.
|
||||
has_modules(_has_foundation Math Text Time Functional Binary FunctionalCoreFormatting Memory Core)
|
||||
if(_has_foundation)
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
#include <Void/Configuration/Bind.hpp>
|
||||
#include <Void/Configuration/Config.hpp>
|
||||
#include <Void/Configuration/Schema.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
namespace C = Void::Configuration;
|
||||
|
||||
struct StrictServerConfig {
|
||||
[[= C::Required{}]] std::string host{};
|
||||
[[= C::IntRange{1, 65535}]] int port{8080};
|
||||
[[= C::Secret{}]] std::string api_token{};
|
||||
[[= C::RestartRequired{}]] std::string bind_interface{"0.0.0.0"};
|
||||
[[= C::Deprecated{"use tls_min_version"}]] bool legacy_tls{false};
|
||||
std::vector<int> shard_ids{};
|
||||
std::optional<std::string> note{};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_schema_precedence_provenance_and_redaction()
|
||||
{
|
||||
C::SourceSet ss;
|
||||
// Passed out of order on purpose: CLI must win over Env, which wins over
|
||||
// File, which wins over Defaults.
|
||||
ss.add_cli_args("cli", {"prog", "--host=cli.internal", "--port", "9090"});
|
||||
ss.add_env_map("env",
|
||||
{{"host", "env.internal"},
|
||||
{"port", "7070"},
|
||||
{"api_token", "super-secret-value-123"}});
|
||||
ss.add_json_text(
|
||||
"file.json",
|
||||
R"({"host":"json.internal","port":6060,"shard_ids":[10,20]})");
|
||||
ss.add_defaults_map("defaults",
|
||||
{{"host", "def.internal"}, {"port", "8080"}});
|
||||
|
||||
auto schema = C::Schema<StrictServerConfig>::from_reflection();
|
||||
auto merged = C::load_sources(ss, schema.limits(), schema.secret_keys());
|
||||
Void::Testing::require(merged.has_value());
|
||||
|
||||
auto res = C::validate<StrictServerConfig>(*merged, schema);
|
||||
Void::Testing::require(res.has_value());
|
||||
Void::Testing::expect_eq(res->value.host, std::string{"cli.internal"});
|
||||
Void::Testing::expect_eq(res->value.port, 9090);
|
||||
Void::Testing::expect_eq(res->value.api_token,
|
||||
std::string{"super-secret-value-123"});
|
||||
Void::Testing::expect_eq(res->value.shard_ids.size(), std::size_t{2});
|
||||
Void::Testing::expect_eq(res->value.shard_ids[0], 10);
|
||||
Void::Testing::expect_eq(res->value.shard_ids[1], 20);
|
||||
|
||||
// Provenance tracks winning origin and shadowed origins with redaction flag.
|
||||
const auto* host_prov = res->provenance->find("host");
|
||||
Void::Testing::require(host_prov != nullptr);
|
||||
Void::Testing::expect_eq(host_prov->winner.source, std::string{"cli"});
|
||||
Void::Testing::expect_eq(host_prov->shadowed.size(), std::size_t{3});
|
||||
|
||||
const auto* tok_prov = res->provenance->find("api_token");
|
||||
Void::Testing::require(tok_prov != nullptr);
|
||||
Void::Testing::expect(tok_prov->redacted);
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_schema_secret_redaction_and_bounds()
|
||||
{
|
||||
auto schema = C::Schema<StrictServerConfig>::from_reflection();
|
||||
|
||||
// Bad secret value (overflow / out-of-range) must redact secret value.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"}});
|
||||
// Force a scalar limit violation with secret key.
|
||||
C::ConfigLimits tiny;
|
||||
tiny.max_scalar_bytes = 8;
|
||||
C::SourceSet big_sec;
|
||||
big_sec.add_defaults_map(
|
||||
"d", {{"host", "h"}, {"api_token", "VERY_SECRET_XYZ_TOKEN"}});
|
||||
auto m = C::load_sources(big_sec, tiny, schema.secret_keys());
|
||||
Void::Testing::expect(!m.has_value());
|
||||
Void::Testing::expect(m.error().message().find("VERY_SECRET_XYZ") ==
|
||||
std::string::npos);
|
||||
Void::Testing::expect(m.error().message().find("[REDACTED]") !=
|
||||
std::string::npos);
|
||||
}
|
||||
|
||||
// Out-of-range port (0 or 70000) fails cleanly with range_error.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"}, {"port", "70000"}});
|
||||
auto m = C::load_sources(ss, schema.limits(), schema.secret_keys());
|
||||
Void::Testing::require(m.has_value());
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::range_error));
|
||||
}
|
||||
|
||||
// Exceeding max_source_bytes / max_nesting_depth rejected.
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_cfg_limits"};
|
||||
const auto file = tmp.path() / "big.json";
|
||||
std::string junk(256, 'a');
|
||||
Void::Testing::require(
|
||||
Void::FileSystem::write_file(file, "{\"host\":\"" + junk + "\"}")
|
||||
.has_value());
|
||||
C::ConfigLimits small_src;
|
||||
small_src.max_source_bytes = 64;
|
||||
C::SourceSet ss;
|
||||
ss.add_json_file("f", file);
|
||||
auto m = C::load_sources(ss, small_src);
|
||||
Void::Testing::expect(!m.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
m.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::limit_exceeded));
|
||||
}
|
||||
|
||||
{
|
||||
std::string nested;
|
||||
for (int i = 0; i < 40; ++i) nested += "{\"a\":";
|
||||
nested += "1";
|
||||
for (int i = 0; i < 40; ++i) nested += "}";
|
||||
C::SourceSet ss;
|
||||
ss.add_json_text("deep", std::move(nested));
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::expect(!m.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
m.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::limit_exceeded));
|
||||
}
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_schema_collisions_gaps_null_empty_and_constraints()
|
||||
{
|
||||
auto schema = C::Schema<StrictServerConfig>::from_reflection();
|
||||
schema.add_constraint("no_root_interface_in_prod",
|
||||
[](const StrictServerConfig& cfg)
|
||||
-> Void::Result::Result<void> {
|
||||
if (cfg.host == "prod" && cfg.port < 1024) {
|
||||
return Void::Result::fail(
|
||||
C::make_config_error(
|
||||
C::ConfigErrorCode::constraint_failed,
|
||||
"prod requires non-privileged port"));
|
||||
}
|
||||
return {};
|
||||
});
|
||||
|
||||
// Dotted key vs nested path collision rejected.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_json_text("coll",
|
||||
R"({"a":{"b":"nested"},"a.b":"literal-dot"})");
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::expect(!m.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
m.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::key_collision));
|
||||
}
|
||||
|
||||
// Unknown key rejected by default.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"}, {"typo_key", "1"}});
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::require(m.has_value());
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::unknown_key));
|
||||
}
|
||||
|
||||
// Vector index gap rejected.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"},
|
||||
{"shard_ids.0", "1"},
|
||||
{"shard_ids.2", "3"}});
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::require(m.has_value());
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::vector_gap));
|
||||
}
|
||||
|
||||
// Null vs Empty vs Missing vs Removed distinction + no fallback on bad port.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_json_text("j", R"({"host":"","port":null,"note":null})");
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::require(m.has_value());
|
||||
Void::Testing::expect(m->state_of("host") == C::ValueState::Empty);
|
||||
Void::Testing::expect(m->state_of("port") == C::ValueState::Null);
|
||||
Void::Testing::expect(m->state_of("missing_x") == C::ValueState::Missing);
|
||||
// null on non-optional int fails instead of falling back to 8080.
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
}
|
||||
|
||||
// Removed required field fails required validation.
|
||||
{
|
||||
C::SourceSet ss;
|
||||
ss.add_defaults_map("d", {{"host", "h"}});
|
||||
ss.add_removals("r", C::SourceKind::Environment, {"host"});
|
||||
auto m = C::load_sources(ss);
|
||||
Void::Testing::require(m.has_value());
|
||||
Void::Testing::expect(m->state_of("host") == C::ValueState::Removed);
|
||||
auto v = C::validate<StrictServerConfig>(*m, schema);
|
||||
Void::Testing::expect(!v.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::missing_required));
|
||||
}
|
||||
|
||||
// Cross-field constraint + deprecated warning + restart_required diff.
|
||||
{
|
||||
C::SourceSet s1;
|
||||
s1.add_defaults_map(
|
||||
"d", {{"host", "prod"}, {"port", "80"}});
|
||||
auto m1 = C::load_sources(s1);
|
||||
auto v1 = C::validate<StrictServerConfig>(*m1, schema);
|
||||
Void::Testing::expect(!v1.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
v1.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::constraint_failed));
|
||||
|
||||
C::SourceSet s2;
|
||||
s2.add_defaults_map("d", {{"host", "prod"},
|
||||
{"port", "8443"},
|
||||
{"legacy_tls", "true"},
|
||||
{"bind_interface", "127.0.0.1"}});
|
||||
C::SourceSet s3;
|
||||
s3.add_defaults_map("d", {{"host", "prod"},
|
||||
{"port", "8443"},
|
||||
{"bind_interface", "10.0.0.1"}});
|
||||
auto m2 = C::load_sources(s2);
|
||||
auto m3 = C::load_sources(s3);
|
||||
auto v2 = C::validate<StrictServerConfig>(*m2, schema);
|
||||
Void::Testing::require(v2.has_value());
|
||||
Void::Testing::expect(!v2->warnings.empty());
|
||||
auto rr = C::detect_restart_required_changes(*m2, *m3, schema);
|
||||
Void::Testing::expect_eq(rr.size(), std::size_t{1});
|
||||
Void::Testing::expect_eq(rr[0], std::string{"bind_interface"});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,340 @@
|
||||
#include <Void/Async/Executor.hpp>
|
||||
#include <Void/Async/Task.hpp>
|
||||
#include <Void/Configuration/ReloadController.hpp>
|
||||
#include <Void/System/AsyncWatcher.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <stdexcept>
|
||||
#include <stop_token>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace void_cfg_async_test
|
||||
{
|
||||
namespace C = Void::Configuration;
|
||||
|
||||
struct AppSettings {
|
||||
[[= C::Required{}]] std::string host{"localhost"};
|
||||
[[= C::IntRange{1, 65535}]] int port{8080};
|
||||
[[= C::RestartRequired{}]] std::string bind_addr{"0.0.0.0"};
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
Void::Coro::Task<void> assign_task_result(Void::Coro::Task<T> task,
|
||||
std::optional<T>* out)
|
||||
{
|
||||
*out = co_await task;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T drive_task(Void::Async::ManualExecutor& exec, Void::Coro::Task<T> task)
|
||||
{
|
||||
std::optional<T> out;
|
||||
Void::Async::spawn(exec, assign_task_result(std::move(task), &out));
|
||||
(void)exec.drain();
|
||||
return std::move(*out);
|
||||
}
|
||||
|
||||
Void::Coro::Task<Void::Result::Result<C::PreparedChange>> make_resource_ok(
|
||||
std::string label, std::atomic<int>* disposes)
|
||||
{
|
||||
auto view = std::make_shared<std::string>(std::move(label));
|
||||
C::PreparedChange pc;
|
||||
pc.view = view;
|
||||
pc.declared_budget_bytes = 64;
|
||||
pc.dispose_async =
|
||||
[disposes]() -> Void::Coro::Task<Void::Result::Result<void>> {
|
||||
if (disposes != nullptr) {
|
||||
disposes->fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
co_return Void::Result::Result<void>{};
|
||||
};
|
||||
co_return pc;
|
||||
}
|
||||
|
||||
Void::Coro::Task<Void::Result::Result<C::PreparedChange>> make_resource_fail()
|
||||
{
|
||||
co_return Void::Result::fail(C::make_config_error(
|
||||
C::ConfigErrorCode::prepare_failed, "participant 2 prepare failed"));
|
||||
}
|
||||
|
||||
Void::Coro::Task<Void::Result::Result<std::pair<
|
||||
Void::Result::Result<C::ReloadReport>,
|
||||
Void::Result::Result<C::ReloadReport>>>>
|
||||
run_two_reloads_concurrent(
|
||||
std::shared_ptr<C::ReloadController<AppSettings>> ctrl,
|
||||
C::SourceSet s1, C::SourceSet s2)
|
||||
{
|
||||
auto t1 = ctrl->reload(std::move(s1));
|
||||
auto t2 = ctrl->reload(std::move(s2));
|
||||
auto r1 = co_await t1;
|
||||
auto r2 = co_await t2;
|
||||
co_return Void::Result::Result<
|
||||
std::pair<Void::Result::Result<C::ReloadReport>,
|
||||
Void::Result::Result<C::ReloadReport>>>{
|
||||
std::pair{std::move(r1), std::move(r2)}};
|
||||
}
|
||||
} // namespace void_cfg_async_test
|
||||
|
||||
using namespace void_cfg_async_test;
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_async_fallible_factory_and_participant_rollback()
|
||||
{
|
||||
auto schema = C::Schema<AppSettings>::from_reflection();
|
||||
Void::Async::ManualExecutor exec;
|
||||
|
||||
// Invalid initial config fails controller creation cleanly.
|
||||
{
|
||||
C::SourceSet bad;
|
||||
bad.add_defaults_map("d", {{"host", "h"}, {"port", "99999"}});
|
||||
auto failed = C::ReloadController<AppSettings>::create(
|
||||
schema, std::move(bad), {.executor = &exec});
|
||||
Void::Testing::expect(!failed.has_value());
|
||||
}
|
||||
|
||||
C::SourceSet init;
|
||||
init.add_defaults_map("d", {{"host", "v1"}, {"port", "8080"}});
|
||||
auto ctrl_res = C::ReloadController<AppSettings>::create(
|
||||
schema, std::move(init), {.executor = &exec});
|
||||
Void::Testing::require(ctrl_res.has_value());
|
||||
auto ctrl = std::move(*ctrl_res);
|
||||
|
||||
std::atomic<int> disposals{0};
|
||||
Void::Testing::require(
|
||||
ctrl->register_participant(
|
||||
"p1", 64,
|
||||
[&](const C::Snapshot<AppSettings>&,
|
||||
const std::shared_ptr<const AppSettings>& cand,
|
||||
Void::Async::OperationOptions) {
|
||||
return make_resource_ok(cand->host + ":" +
|
||||
std::to_string(cand->port),
|
||||
&disposals);
|
||||
})
|
||||
.has_value());
|
||||
Void::Testing::require(
|
||||
ctrl->register_participant(
|
||||
"p2", 64,
|
||||
[](const C::Snapshot<AppSettings>&,
|
||||
const std::shared_ptr<const AppSettings>&,
|
||||
Void::Async::OperationOptions) {
|
||||
return make_resource_fail();
|
||||
})
|
||||
.has_value());
|
||||
|
||||
// Reload fails in p2 -> must dispose p1 and keep generation 1 intact.
|
||||
{
|
||||
C::SourceSet s2;
|
||||
s2.add_defaults_map("d", {{"host", "v2"}, {"port", "9090"}});
|
||||
auto r = drive_task(exec, ctrl->reload(std::move(s2)));
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect_eq(disposals.load(), 1);
|
||||
auto snap = ctrl->current();
|
||||
Void::Testing::expect_eq(snap.generation, std::uint64_t{1});
|
||||
Void::Testing::expect_eq(snap.values->host, std::string{"v1"});
|
||||
}
|
||||
|
||||
// Unregister p2 and commit v2 with notification throw isolation +
|
||||
// post-commit stop observation + restart-required check.
|
||||
ctrl->unregister_participant("p2");
|
||||
ctrl->on_committed(
|
||||
[](const C::Snapshot<AppSettings>&, const C::ReloadReport&) {
|
||||
throw std::runtime_error("observer failure");
|
||||
});
|
||||
|
||||
std::stop_source ss;
|
||||
Void::Testing::require(
|
||||
ctrl->register_participant(
|
||||
"p_stop", 64,
|
||||
[&](const C::Snapshot<AppSettings>&,
|
||||
const std::shared_ptr<const AppSettings>&,
|
||||
Void::Async::OperationOptions)
|
||||
-> Void::Coro::Task<Void::Result::Result<C::PreparedChange>> {
|
||||
// Request stop only inside the second reload if flagged.
|
||||
co_return co_await make_resource_ok("ok", nullptr);
|
||||
})
|
||||
.has_value());
|
||||
|
||||
{
|
||||
C::SourceSet s2;
|
||||
s2.add_defaults_map("d", {{"host", "v2"}, {"port", "9090"}});
|
||||
auto r = drive_task(exec, ctrl->reload(std::move(s2)));
|
||||
Void::Testing::require(r.has_value());
|
||||
Void::Testing::expect(r->committed);
|
||||
Void::Testing::expect_eq(r->generation, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(r->notification_errors.size(), std::size_t{1});
|
||||
auto snap = ctrl->current();
|
||||
auto view = snap.resource<std::string>("p1");
|
||||
Void::Testing::require(view != nullptr);
|
||||
Void::Testing::expect_eq(*view, std::string{"v2:9090"});
|
||||
}
|
||||
|
||||
// Restart-required key change preserves generation 2 and reports pending.
|
||||
{
|
||||
C::SourceSet s_rr;
|
||||
s_rr.add_defaults_map(
|
||||
"d", {{"host", "v3"}, {"port", "9091"}, {"bind_addr", "127.0.0.1"}});
|
||||
auto r = drive_task(exec, ctrl->reload(std::move(s_rr)));
|
||||
Void::Testing::require(r.has_value());
|
||||
Void::Testing::expect(!r->committed);
|
||||
Void::Testing::expect(r->restart_required);
|
||||
Void::Testing::expect_eq(r->generation, std::uint64_t{2});
|
||||
Void::Testing::expect_eq(ctrl->current().values->host,
|
||||
std::string{"v2"});
|
||||
}
|
||||
|
||||
ctrl->close_and_join();
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_async_retention_precommit_cancel_and_concurrent_order()
|
||||
{
|
||||
auto schema = C::Schema<AppSettings>::from_reflection();
|
||||
Void::Async::ManualExecutor exec;
|
||||
|
||||
C::SourceSet init;
|
||||
init.add_defaults_map("d", {{"host", "g1"}, {"port", "8001"}});
|
||||
auto ctrl =
|
||||
C::ReloadController<AppSettings>::create(schema, std::move(init),
|
||||
{.executor = &exec})
|
||||
.value();
|
||||
|
||||
std::vector<C::Snapshot<AppSettings>> pinned;
|
||||
pinned.push_back(ctrl->current()); // gen 1
|
||||
for (int i = 2; i <= 4; ++i) {
|
||||
C::SourceSet s;
|
||||
s.add_defaults_map("d", {{"host", "g" + std::to_string(i)},
|
||||
{"port", std::to_string(8000 + i)}});
|
||||
auto r = drive_task(exec, ctrl->reload(std::move(s)));
|
||||
Void::Testing::require(r.has_value());
|
||||
pinned.push_back(ctrl->current());
|
||||
}
|
||||
Void::Testing::expect_eq(ctrl->retained_generation_count(), std::size_t{4});
|
||||
|
||||
// 5th generation rejected while 4 snapshots remain pinned.
|
||||
{
|
||||
C::SourceSet s5;
|
||||
s5.add_defaults_map("d", {{"host", "g5"}, {"port", "8005"}});
|
||||
auto r5 = drive_task(exec, ctrl->reload(std::move(s5)));
|
||||
Void::Testing::expect(!r5.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
r5.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::retention_limit));
|
||||
}
|
||||
|
||||
pinned.erase(pinned.begin()); // Drop gen 1 -> 3 live
|
||||
|
||||
// Pre-commit cancellation disposes candidate and preserves gen 4.
|
||||
std::stop_source ss;
|
||||
ss.request_stop();
|
||||
{
|
||||
C::SourceSet sc;
|
||||
sc.add_defaults_map("d", {{"host", "cancel"}, {"port", "8006"}});
|
||||
Void::Async::OperationOptions opts;
|
||||
opts.stop = ss.get_token();
|
||||
auto rc = drive_task(exec, ctrl->reload(std::move(sc), opts));
|
||||
Void::Testing::expect(!rc.has_value());
|
||||
Void::Testing::expect_eq(
|
||||
rc.error().code().value,
|
||||
static_cast<std::int32_t>(C::ConfigErrorCode::cancelled));
|
||||
Void::Testing::expect_eq(ctrl->current().values->host,
|
||||
std::string{"g4"});
|
||||
}
|
||||
|
||||
// Concurrent reloads serialize cleanly within the retention budget.
|
||||
pinned.clear();
|
||||
{
|
||||
C::SourceSet a, b;
|
||||
a.add_defaults_map("d", {{"host", "c1"}, {"port", "8011"}});
|
||||
b.add_defaults_map("d", {{"host", "c2"}, {"port", "8012"}});
|
||||
auto both = drive_task(
|
||||
exec, run_two_reloads_concurrent(ctrl, std::move(a), std::move(b)));
|
||||
Void::Testing::require(both.has_value());
|
||||
Void::Testing::require(both->first.has_value());
|
||||
Void::Testing::require(both->second.has_value());
|
||||
Void::Testing::expect(both->second->generation >
|
||||
both->first->generation);
|
||||
}
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test config_async_watcher_coalescing_and_prepare_external()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp{"void_cfg_async_watch"};
|
||||
const auto cfg_path = tmp.path() / "app.json";
|
||||
Void::Testing::require(
|
||||
Void::FileSystem::write_file(cfg_path, R"({"host":"init","port":8080})")
|
||||
.has_value());
|
||||
|
||||
auto schema = C::Schema<AppSettings>::from_reflection();
|
||||
Void::Async::ManualExecutor exec;
|
||||
Void::Time::ManualClock clock;
|
||||
Void::SystemAsync::WatcherService watcher{
|
||||
exec,
|
||||
Void::SystemAsync::WatcherLimits{
|
||||
.poll_interval = std::chrono::milliseconds{10},
|
||||
.debounce_polls = 1},
|
||||
clock};
|
||||
|
||||
auto build_sources = [&]() {
|
||||
C::SourceSet ss;
|
||||
ss.add_json_file("app", cfg_path);
|
||||
return ss;
|
||||
};
|
||||
auto ctrl =
|
||||
C::ReloadController<AppSettings>::create(schema, build_sources(),
|
||||
{.clock = &clock,
|
||||
.executor = &exec})
|
||||
.value();
|
||||
|
||||
std::atomic<int> commits{0};
|
||||
ctrl->on_committed([&](const C::Snapshot<AppSettings>&,
|
||||
const C::ReloadReport&) {
|
||||
commits.fetch_add(1, std::memory_order_relaxed);
|
||||
});
|
||||
Void::Testing::require(
|
||||
ctrl->attach_watcher(watcher, {cfg_path}, build_sources, exec)
|
||||
.has_value());
|
||||
|
||||
// Overwrite with invalid JSON first (must keep old), then valid edit.
|
||||
Void::Testing::require(
|
||||
Void::FileSystem::write_file(cfg_path, "{broken").has_value());
|
||||
watcher.poll_once();
|
||||
(void)exec.drain();
|
||||
Void::Testing::expect_eq(ctrl->current().values->host,
|
||||
std::string{"init"});
|
||||
|
||||
Void::Testing::require(
|
||||
Void::FileSystem::write_file(cfg_path,
|
||||
R"({"host":"watched","port":8099})")
|
||||
.has_value());
|
||||
watcher.poll_once();
|
||||
(void)exec.drain();
|
||||
Void::Testing::expect_eq(ctrl->current().values->host,
|
||||
std::string{"watched"});
|
||||
Void::Testing::expect_eq(ctrl->current().values->port, 8099);
|
||||
Void::Testing::expect(commits.load() >= 1);
|
||||
|
||||
// Prepare-only external coordinator port leaves controller unchanged.
|
||||
{
|
||||
C::SourceSet ext;
|
||||
ext.add_defaults_map("d", {{"host", "staged_ext"}, {"port", "8111"}});
|
||||
auto prep = drive_task(exec, ctrl->prepare_external(std::move(ext)));
|
||||
Void::Testing::require(prep.has_value());
|
||||
Void::Testing::expect_eq(prep->values->host,
|
||||
std::string{"staged_ext"});
|
||||
Void::Testing::expect_eq(ctrl->current().values->host,
|
||||
std::string{"watched"});
|
||||
(void)drive_task(exec, prep->dispose_all());
|
||||
}
|
||||
|
||||
ctrl->close_and_join();
|
||||
Void::Testing::expect(watcher.shutdown().has_value());
|
||||
}
|
||||
Reference in new issue
Block a user