#include #include #include #include #include #include #include #include #include using Void::FileSystem::FileWatcher; namespace { // Long poll interval so only explicit poll_now() rounds matter. FileWatcher::Options manual_poll() { return {.poll_interval = std::chrono::milliseconds{60'000}, .debounce_polls = 2}; } int write_counter = 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()); // Nudge mtime forward — coarse filesystem timestamps could // otherwise hide consecutive writes from the watcher. const auto now = std::filesystem::file_time_type::clock::now(); std::filesystem::last_write_time( p, now + std::chrono::milliseconds{(++write_counter) * 10}); } } // namespace Void::Testing::Test watcher_fires_after_debounce() { Void::FileSystem::TempDir tmp{"void_watch"}; const auto file = tmp.path() / "config.txt"; write(file, "v1"); std::atomic fired{0}; FileWatcher watcher{manual_poll()}; watcher.watch(file, [&](const FileWatcher::Event& e) { Void::Testing::expect(e.exists); ++fired; }); write(file, "v2-with-different-size"); watcher.poll_now(); // change observed, stability count = 1 Void::Testing::expect_eq(fired.load(), 0); watcher.poll_now(); // stable for 2 polls -> fires Void::Testing::expect_eq(fired.load(), 1); watcher.poll_now(); // no further change -> no further events Void::Testing::expect_eq(fired.load(), 1); } Void::Testing::Test watcher_debounce_restarts_while_changing() { Void::FileSystem::TempDir tmp{"void_watch"}; const auto file = tmp.path() / "config.txt"; write(file, "v1"); std::atomic fired{0}; FileWatcher watcher{manual_poll()}; watcher.watch(file, [&](const FileWatcher::Event&) { ++fired; }); write(file, "second version"); watcher.poll_now(); write(file, "third version, longer again"); watcher.poll_now(); // still changing -> debounce restarts Void::Testing::expect_eq(fired.load(), 0); watcher.poll_now(); Void::Testing::expect_eq(fired.load(), 1); } Void::Testing::Test watcher_reports_deletion() { Void::FileSystem::TempDir tmp{"void_watch"}; const auto file = tmp.path() / "config.txt"; write(file, "v1"); std::atomic gone{0}; FileWatcher watcher{manual_poll()}; watcher.watch(file, [&](const FileWatcher::Event& e) { if (!e.exists) { ++gone; } }); std::filesystem::remove(file); watcher.poll_now(); watcher.poll_now(); Void::Testing::expect_eq(gone.load(), 1); } Void::Testing::Test watcher_unwatch_stops_events() { Void::FileSystem::TempDir tmp{"void_watch"}; const auto file = tmp.path() / "config.txt"; write(file, "v1"); std::atomic fired{0}; FileWatcher watcher{manual_poll()}; watcher.watch(file, [&](const FileWatcher::Event&) { ++fired; }); Void::Testing::expect_eq(watcher.watch_count(), std::size_t{1}); watcher.unwatch(file); Void::Testing::expect_eq(watcher.watch_count(), std::size_t{0}); write(file, "changed"); watcher.poll_now(); watcher.poll_now(); Void::Testing::expect_eq(fired.load(), 0); } // @tag integration Void::Testing::Test watcher_background_thread_fires_on_its_own() { Void::FileSystem::TempDir tmp{"void_watch"}; const auto file = tmp.path() / "config.txt"; write(file, "v1"); std::atomic fired{0}; FileWatcher watcher{ {.poll_interval = std::chrono::milliseconds{10}, .debounce_polls = 2}}; watcher.watch(file, [&](const FileWatcher::Event&) { ++fired; }); write(file, "v2 changed contents"); for (int i = 0; i < 200 && fired.load() == 0; ++i) { std::this_thread::sleep_for(std::chrono::milliseconds{5}); } Void::Testing::expect_eq(fired.load(), 1); }