mirror of
https://github.com/lemonade-sdk/lemonade.git
synced 2026-11-23 08:49:28 -10:00
329 lines
10 KiB
C++
329 lines
10 KiB
C++
// Standalone test for lemon::DirectoryWatcher.
|
|
// Compile with:
|
|
// g++ -std=c++17 -pthread -I src/cpp/include test/cpp/test_directory_watcher.cpp -o directory_watcher_test
|
|
// cl /std:c++17 /EHsc /I src/cpp/include test/cpp/test_directory_watcher.cpp /link kernel32.lib
|
|
|
|
#include "lemon/directory_watcher.h"
|
|
|
|
#include <atomic>
|
|
#include <cassert>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
using lemon::DirectoryWatcher;
|
|
namespace fs = std::filesystem;
|
|
|
|
struct TestResult {
|
|
int passed = 0;
|
|
int failed = 0;
|
|
|
|
void ok(const std::string& name) {
|
|
printf("[PASS] %s\n", name.c_str());
|
|
++passed;
|
|
}
|
|
|
|
void fail(const std::string& name) {
|
|
printf("[FAIL] %s\n", name.c_str());
|
|
++failed;
|
|
}
|
|
};
|
|
|
|
// Create a unique temp directory for this test run
|
|
static std::string make_temp_dir() {
|
|
fs::path tmp = fs::temp_directory_path() / "directory_watcher_test_";
|
|
tmp += std::to_string(std::hash<std::string>{}(std::to_string(std::time(nullptr))));
|
|
fs::create_directories(tmp);
|
|
return tmp.string();
|
|
}
|
|
|
|
// Test 1: Basic file-change detection via callback
|
|
static void test_basic_detection(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
auto cb = fs::path(dir); // capture for lambda
|
|
std::atomic<int> call_count{0};
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([&call_count]() { ++call_count; });
|
|
watcher.start();
|
|
|
|
// Write a file — triggers inotify kqueue/polling
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
std::ofstream{fs::path(dir) / "test.txt"} << "hello";
|
|
|
|
// Give debounce + poll time
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
}
|
|
|
|
int count = call_count.load();
|
|
if (count > 0) {
|
|
r.ok("basic file-change detection (calls=" + std::to_string(count) + ")");
|
|
} else {
|
|
r.fail("basic file-change detection (callback never fired)");
|
|
}
|
|
}
|
|
|
|
// Test 2: File deletion detection
|
|
static void test_file_deletion(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
std::atomic<int> call_count{0};
|
|
|
|
// Pre-create a file
|
|
std::ofstream{fs::path(dir) / "to_delete.txt"} << "bye";
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([&call_count]() { ++call_count; });
|
|
watcher.start();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
fs::remove(fs::path(dir) / "to_delete.txt");
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
}
|
|
|
|
int count = call_count.load();
|
|
if (count > 0) {
|
|
r.ok("file deletion detection (calls=" + std::to_string(count) + ")");
|
|
} else {
|
|
r.fail("file deletion detection (callback never fired)");
|
|
}
|
|
}
|
|
|
|
// Test 3: Stop cleanly
|
|
static void test_stop(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
std::atomic<int> call_count{0};
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([&call_count]() { ++call_count; });
|
|
watcher.start();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
watcher.stop();
|
|
}
|
|
|
|
r.ok("stop cleanly");
|
|
}
|
|
|
|
// Test 4: Non-existent directory — watcher polls until dir appears
|
|
static void test_nonexistent_dir(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
// Remove it so it doesn't exist
|
|
fs::remove_all(dir);
|
|
|
|
std::atomic<int> call_count{0};
|
|
int writes = 0;
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([&call_count]() { ++call_count; });
|
|
watcher.start();
|
|
|
|
// Wait a bit for the watcher to start polling
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
|
|
|
// Now create the directory (simulates late creation)
|
|
fs::create_directories(dir);
|
|
|
|
// Wait a bit for the watcher to detect the new dir and set up inotify
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(600));
|
|
|
|
// On a loaded runner the watch can arm after a write, and that write
|
|
// is never reported, so retry with a fresh file.
|
|
// Each write gets a quiet wait: writing again sooner restarts the macOS debounce,
|
|
// and the watcher must stay alive or a pending change is dropped on stop.
|
|
while (call_count.load() == 0 && writes < 5) {
|
|
std::ofstream{dir / ("file" + std::to_string(writes++) + ".txt")} << "data";
|
|
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(3);
|
|
while (call_count.load() == 0 && std::chrono::steady_clock::now() < deadline) {
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
}
|
|
}
|
|
}
|
|
|
|
int count = call_count.load();
|
|
std::string detail = "calls=" + std::to_string(count) + ", writes=" + std::to_string(writes);
|
|
if (count > 0) {
|
|
r.ok("non-existent dir recovery (" + detail + ")");
|
|
} else {
|
|
r.fail("non-existent dir recovery (callback never fired, " + detail + ")");
|
|
}
|
|
}
|
|
|
|
// Test 5: Rapid changes — debounce coalesces events
|
|
static void test_debounce(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
std::atomic<int> call_count{0};
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([&call_count]() { ++call_count; });
|
|
watcher.start();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
|
// Create many files in quick succession
|
|
for (int i = 0; i < 10; ++i) {
|
|
std::ofstream{fs::path(dir) / ("rapid_" + std::to_string(i) + ".txt")} << i;
|
|
}
|
|
|
|
// Wait for debounce window
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(300));
|
|
|
|
// After debounce, another batch
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
|
for (int i = 10; i < 20; ++i) {
|
|
std::ofstream{fs::path(dir) / ("rapid_" + std::to_string(i) + ".txt")} << i;
|
|
}
|
|
|
|
// Wait for second debounce
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
}
|
|
|
|
int count = call_count.load();
|
|
// Should have ~2 callback groups (two batches with gap between them)
|
|
if (count >= 2) {
|
|
r.ok("debounce coalescing (calls=" + std::to_string(count) + ", expected>=2)");
|
|
} else {
|
|
r.fail("debounce coalescing (calls=" + std::to_string(count) + ", expected>=2)");
|
|
}
|
|
}
|
|
|
|
// Test 6: Directory removal — watcher stops gracefully
|
|
static void test_directory_removal(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([]() {});
|
|
watcher.start();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
|
|
// Remove the watched directory
|
|
fs::remove_all(dir);
|
|
|
|
// Wait for watcher to detect removal and stop
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
}
|
|
|
|
r.ok("directory removal graceful stop");
|
|
}
|
|
|
|
// Test 7: A file written in a pre-existing nested folder is detected
|
|
static void test_nested_change(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
fs::path nested = fs::path(dir) / "nested_existing" / "refs";
|
|
fs::create_directories(nested);
|
|
std::atomic<int> call_count{0};
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([&call_count]() { ++call_count; });
|
|
watcher.start();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
std::ofstream{nested / "main"} << "commit-b";
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
}
|
|
|
|
fs::remove_all(fs::path(dir) / "nested_existing");
|
|
if (call_count.load() > 0) {
|
|
r.ok("nested folder change detection");
|
|
} else {
|
|
r.fail("nested folder change detection (callback never fired)");
|
|
}
|
|
}
|
|
|
|
// Test 8: A folder created after start() is watched too
|
|
static void test_new_folder_is_watched(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
fs::path created = fs::path(dir) / "nested_created";
|
|
std::atomic<int> call_count{0};
|
|
int after_create = 0;
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([&call_count]() { ++call_count; });
|
|
watcher.start();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
fs::create_directories(created);
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
after_create = call_count.load();
|
|
|
|
std::ofstream{created / "model.gguf"} << "GGUF";
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
}
|
|
|
|
fs::remove_all(created);
|
|
if (call_count.load() > after_create) {
|
|
r.ok("new folder is watched");
|
|
} else {
|
|
r.fail("new folder is watched (write inside it was missed)");
|
|
}
|
|
}
|
|
|
|
#ifdef __linux__
|
|
// Test 9: A file still being written is not announced until it is closed
|
|
static void test_open_file_waits_for_close(TestResult& r) {
|
|
fs::path dir = make_temp_dir();
|
|
fs::path nested = fs::path(dir) / "nested_download";
|
|
fs::create_directories(nested);
|
|
std::atomic<int> call_count{0};
|
|
int while_open = 0;
|
|
|
|
{
|
|
DirectoryWatcher watcher(dir.string());
|
|
watcher.set_callback([&call_count]() { ++call_count; });
|
|
watcher.start();
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
|
{
|
|
std::ofstream file(nested / "model.gguf");
|
|
for (int i = 0; i < 6; ++i) {
|
|
file << "GGUF" << std::flush;
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
}
|
|
while_open = call_count.load();
|
|
}
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(800));
|
|
}
|
|
|
|
fs::remove_all(nested);
|
|
if (while_open == 0 && call_count.load() > 0) {
|
|
r.ok("open file waits for close");
|
|
} else {
|
|
r.fail("open file waits for close (calls while open=" + std::to_string(while_open) +
|
|
", total=" + std::to_string(call_count.load()) + ")");
|
|
}
|
|
}
|
|
#endif
|
|
|
|
int main() {
|
|
TestResult r;
|
|
|
|
printf("=== DirectoryWatcher Unit Tests ===\n\n");
|
|
|
|
test_basic_detection(r);
|
|
test_file_deletion(r);
|
|
test_stop(r);
|
|
test_nonexistent_dir(r);
|
|
test_debounce(r);
|
|
test_directory_removal(r);
|
|
test_nested_change(r);
|
|
test_new_folder_is_watched(r);
|
|
#ifdef __linux__
|
|
test_open_file_waits_for_close(r);
|
|
#endif
|
|
|
|
printf("\n%d/%d tests passed\n", r.passed, r.passed + r.failed);
|
|
return r.failed == 0 ? 0 : 1;
|
|
}
|