#![cfg(feature = "hot-reload")]
#![allow(deprecated)]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use config_lib::hot_reload::{ConfigChangeEvent, HotReloadConfig};
use std::fs::File;
use std::io::Write;
use std::time::Duration;
use tempfile::TempDir;
fn write_conf(path: &std::path::Path, body: &str) {
let mut f = File::create(path).unwrap();
f.write_all(body.as_bytes()).unwrap();
f.flush().unwrap();
f.sync_all().unwrap();
}
#[test]
fn modified_file_emits_reloaded_event() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("app.conf");
write_conf(&path, "key=value1\n");
let (hot, rx) = HotReloadConfig::from_file(&path)
.unwrap()
.with_debounce(Duration::from_millis(25))
.with_change_notifications();
let handle = hot.start_watching();
std::thread::sleep(Duration::from_millis(150));
write_conf(&path, "key=value2\n");
let deadline = std::time::Instant::now() + Duration::from_secs(2);
let mut saw_reloaded = false;
while std::time::Instant::now() < deadline {
if let Ok(event) = rx.recv_timeout(Duration::from_millis(100)) {
if matches!(event, ConfigChangeEvent::Reloaded { .. }) {
saw_reloaded = true;
break;
}
}
}
assert!(saw_reloaded, "no Reloaded event arrived within 2 seconds");
handle.stop().unwrap();
}