#include #include #include #include #include #include #include #include #include #include #include using Void::Configuration::Config; using Void::Configuration::ConfigWatch; namespace { Void::FileSystem::FileWatcherOptions manual_poll() { return {.poll_interval = std::chrono::milliseconds{60'000}, .debounce_polls = 2}; } int bump = 0; void write(const std::filesystem::path& p, std::string_view text) { auto r = Void::FileSystem::write_file(p, text); Void::Testing::expect(r.has_value()); const auto now = std::filesystem::file_time_type::clock::now(); std::filesystem::last_write_time( p, now + std::chrono::milliseconds{(++bump) * 10}); } } // namespace Void::Testing::Test config_watch_reloads_and_diffs() { Void::FileSystem::TempDir tmp{"void_cfgwatch"}; const auto file = tmp.path() / "app.ini"; write(file, "[log]\nlevel = info\nkeep = 1\n"); std::mutex mu; std::vector last_changed; std::string last_level; auto watch = ConfigWatch::ini( file, [&](const Config& cfg, const std::vector& changed) { std::lock_guard lock(mu); last_changed = changed; last_level = cfg.get_or("log.level"); }, manual_poll()); Void::Testing::expect(watch.current().get_or("log.level") == "info"); write(file, "[log]\nlevel = debug\nkeep = 1\nadded = x\n"); watch.poll_now(); watch.poll_now(); // debounce satisfied -> reload + callback std::lock_guard lock(mu); Void::Testing::expect_eq(last_level, std::string{"debug"}); Void::Testing::expect_eq(last_changed.size(), std::size_t{2}); Void::Testing::expect_eq(last_changed[0], std::string{"log.added"}); Void::Testing::expect_eq(last_changed[1], std::string{"log.level"}); Void::Testing::expect(watch.current().get_or("log.level") == "debug"); } Void::Testing::Test config_watch_keeps_old_config_on_parse_error() { Void::FileSystem::TempDir tmp{"void_cfgwatch"}; const auto file = tmp.path() / "app.ini"; write(file, "[log]\nlevel = info\n"); std::atomic reloads{0}; auto watch = ConfigWatch::ini( file, [&](const Config&, const std::vector&) { ++reloads; }, manual_poll()); write(file, "[broken\nlevel=zzz\n"); // malformed section watch.poll_now(); watch.poll_now(); Void::Testing::expect_eq(reloads.load(), 0); Void::Testing::expect(watch.current().get_or("log.level") == "info"); } Void::Testing::Test config_watch_deletion_keeps_last_config() { Void::FileSystem::TempDir tmp{"void_cfgwatch"}; const auto file = tmp.path() / "app.ini"; write(file, "[log]\nlevel = info\n"); std::atomic reloads{0}; auto watch = ConfigWatch::ini( file, [&](const Config&, const std::vector&) { ++reloads; }, manual_poll()); std::filesystem::remove(file); watch.poll_now(); watch.poll_now(); Void::Testing::expect_eq(reloads.load(), 0); Void::Testing::expect(watch.current().get_or("log.level") == "info"); }