use std::time::{Duration, Instant};
use windows_sys::Win32::Foundation::{ERROR_ACCESS_DENIED, ERROR_NOT_SUPPORTED};
use super::{
Monitor, Opened, Request, Subscription, awaits_open_answer, lock, open_file_target,
should_report_established,
};
use crate::directory::{DirectoryHandle, FaultDetail, OpenFailure, VolumeIdentity};
use crate::queue::{Notification, Receiver, WatchId};
use crate::retry::FaultOperation;
use crate::session::Session;
use crate::testing::TempDir;
use crate::watch::{RetryMode, VolumeChangeDecision, VolumeChangePolicy, Watch, WatchOptions};
const TEARDOWN_BUDGET: Duration = Duration::from_secs(5);
fn watching(dir: &std::path::Path) -> (Monitor, Session, Receiver, Watch) {
let monitor = Monitor::new().expect("create the monitor");
let (session, receiver) = monitor.session();
let watch = session
.subscribe(dir, WatchOptions::new())
.expect("register the subscription");
monitor.quiesce();
(monitor, session, receiver, watch)
}
#[test]
fn a_new_monitor_is_running_and_owns_nothing() {
let monitor = Monitor::new().expect("create the monitor");
assert!(monitor.is_running());
assert_eq!(monitor.watcher_count(), 0);
}
#[test]
fn shutdown_stops_the_servicing_path() {
let monitor = Monitor::new().expect("create the monitor");
monitor.shut_down();
assert!(!monitor.is_running());
}
#[test]
fn shutdown_is_idempotent_and_drop_after_it_is_safe() {
let monitor = Monitor::new().expect("create the monitor");
monitor.shut_down();
monitor.shut_down();
monitor.shut_down();
assert!(!monitor.is_running());
drop(monitor);
}
#[test]
fn dropping_an_idle_monitor_is_prompt() {
let monitor = Monitor::new().expect("create the monitor");
let started = Instant::now();
drop(monitor);
assert!(started.elapsed() < TEARDOWN_BUDGET);
}
#[test]
fn a_subscription_becomes_a_watcher() {
let dir = TempDir::new("monitor-subscribe");
let (monitor, _session, _receiver, watch) = watching(dir.path());
assert_eq!(monitor.watcher_count(), 1);
assert!(monitor.is_watching(watch.id()));
drop(watch);
drop(monitor);
dir.cleanup();
}
#[test]
fn several_subscriptions_become_several_watchers() {
let dir = TempDir::new("monitor-several");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let watches: Vec<_> = (0..8)
.map(|_| {
session
.subscribe(dir.path(), WatchOptions::new())
.expect("register")
})
.collect();
monitor.quiesce();
assert_eq!(monitor.watcher_count(), 8);
let mut ids: Vec<u64> = watches.iter().map(|watch| watch.id().get()).collect();
ids.sort_unstable();
ids.dedup();
assert_eq!(ids.len(), 8);
drop(watches);
drop(monitor);
dir.cleanup();
}
#[test]
fn teardown_with_watchers_outstanding_converges_promptly() {
let dir = TempDir::new("monitor-outstanding");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let _watches: Vec<_> = (0..8)
.map(|_| {
session
.subscribe(dir.path(), WatchOptions::new())
.expect("register")
})
.collect();
monitor.quiesce();
let started = Instant::now();
monitor.shut_down();
let elapsed = started.elapsed();
assert!(
elapsed < TEARDOWN_BUDGET,
"teardown took {elapsed:?}, which means it waited rather than cancelled"
);
assert_eq!(monitor.watcher_count(), 0);
dir.cleanup();
}
#[test]
fn teardown_releases_every_watcher_so_the_receiver_disconnects() {
let dir = TempDir::new("monitor-release");
let (monitor, session, receiver, watch) = watching(dir.path());
drop(watch);
drop(session);
monitor.shut_down();
assert!(
receiver.is_disconnected(),
"teardown must release the watcher's sender, not just forget the table entry"
);
dir.cleanup();
}
#[test]
fn dropping_a_monitor_tears_down_its_watchers() {
let dir = TempDir::new("monitor-drop");
let (monitor, session, receiver, watch) = watching(dir.path());
drop(watch);
drop(session);
drop(monitor);
assert!(
receiver.is_disconnected(),
"`Drop` must run the same teardown as an explicit shutdown"
);
dir.cleanup();
}
#[test]
fn teardown_from_a_thread_other_than_the_creator_is_safe() {
let dir = TempDir::new("monitor-thread");
let (monitor, session, receiver, watch) = watching(dir.path());
drop(watch);
drop(session);
std::thread::spawn(move || drop(monitor))
.join()
.expect("teardown thread");
assert!(receiver.is_disconnected());
dir.cleanup();
}
#[test]
fn many_monitors_tear_down_concurrently_without_wedging() {
let dir = TempDir::new("monitor-concurrent");
let root = dir.path().to_path_buf();
let workers: Vec<_> = (0..8)
.map(|_| {
let root = root.clone();
std::thread::spawn(move || {
let (monitor, session, receiver, watch) = watching(&root);
drop(watch);
drop(session);
drop(monitor);
assert!(receiver.is_disconnected());
})
})
.collect();
let started = Instant::now();
for worker in workers {
worker.join().expect("worker");
}
assert!(
started.elapsed() < Duration::from_secs(30),
"concurrent teardown wedged"
);
dir.cleanup();
}
#[test]
fn debug_reports_the_monitors_state() {
let dir = TempDir::new("monitor-debug");
let (monitor, _session, _receiver, watch) = watching(dir.path());
let rendered = format!("{monitor:?}");
assert!(rendered.contains("running: true"), "{rendered}");
assert!(rendered.contains("watchers: 1"), "{rendered}");
drop(watch);
drop(monitor);
dir.cleanup();
}
#[test]
fn two_subscriptions_on_the_same_directory_share_one_watcher() {
let dir = TempDir::new("coalesce-same");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let a = session
.subscribe(dir.path(), WatchOptions::new())
.expect("register");
let b = session
.subscribe(dir.path(), WatchOptions::new())
.expect("register");
monitor.quiesce();
assert_eq!(monitor.watcher_count(), 2, "two subscriptions");
assert_eq!(
monitor.directory_count(),
1,
"one directory, one coalesced watcher"
);
drop((a, b));
drop(monitor);
dir.cleanup();
}
#[test]
fn subscriptions_on_different_directories_get_different_watchers() {
let first = TempDir::new("coalesce-a");
let second = TempDir::new("coalesce-b");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let a = session
.subscribe(first.path(), WatchOptions::new())
.expect("register");
let b = session
.subscribe(second.path(), WatchOptions::new())
.expect("register");
monitor.quiesce();
assert_eq!(monitor.directory_count(), 2);
drop((a, b));
drop(monitor);
first.cleanup();
second.cleanup();
}
#[test]
fn the_same_directory_reached_through_a_different_spelling_still_coalesces() {
let dir = TempDir::new("coalesce-spelling");
let mut with_separator = dir.path().to_path_buf();
with_separator.push("");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let a = session
.subscribe(dir.path(), WatchOptions::new())
.expect("register");
let b = session
.subscribe(&with_separator, WatchOptions::new())
.expect("register");
monitor.quiesce();
assert_eq!(monitor.directory_count(), 1);
drop((a, b));
drop(monitor);
dir.cleanup();
}
#[test]
fn cancelling_one_of_two_coalesced_subscriptions_keeps_the_watcher_alive() {
let dir = TempDir::new("coalesce-cancel-one");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let a = session
.subscribe(dir.path(), WatchOptions::new())
.expect("register");
let b = session
.subscribe(dir.path(), WatchOptions::new())
.expect("register");
monitor.quiesce();
a.cancel();
monitor.quiesce();
assert_eq!(monitor.watcher_count(), 1, "only b remains");
assert_eq!(
monitor.directory_count(),
1,
"the watcher survives while a subscriber remains"
);
assert!(monitor.is_watching(b.id()));
drop(b);
drop(monitor);
dir.cleanup();
}
#[test]
fn cancelling_the_last_coalesced_subscription_tears_down_the_watcher() {
let dir = TempDir::new("coalesce-cancel-last");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let a = session
.subscribe(dir.path(), WatchOptions::new())
.expect("register");
let b = session
.subscribe(dir.path(), WatchOptions::new())
.expect("register");
monitor.quiesce();
let a_id = a.id();
let b_id = b.id();
a.cancel();
b.cancel();
monitor.quiesce();
assert!(!monitor.is_registered(a_id));
assert!(!monitor.is_registered(b_id));
assert_eq!(monitor.watcher_count(), 0);
assert_eq!(monitor.directory_count(), 0, "the watcher is torn down");
drop(monitor);
dir.cleanup();
}
#[test]
fn many_subscriptions_on_one_directory_all_coalesce() {
let dir = TempDir::new("coalesce-many");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let watches: Vec<Watch> = (0..16)
.map(|_| {
session
.subscribe(dir.path(), WatchOptions::new())
.expect("register")
})
.collect();
monitor.quiesce();
assert_eq!(monitor.watcher_count(), 16);
assert_eq!(monitor.directory_count(), 1);
drop(watches);
drop(monitor);
dir.cleanup();
}
#[test]
fn a_target_that_does_not_exist_yet_establishes_once_it_appears() {
let dir = TempDir::new("monitor-retry-appears");
let target = dir.path().join("not-yet");
let monitor = Monitor::new().expect("create the monitor");
let (session, receiver) = monitor.session();
let watch = session
.subscribe(&target, WatchOptions::new())
.expect("register the subscription");
monitor.quiesce();
assert_eq!(monitor.is_faulted(watch.id()), Some(true));
assert!(!monitor.is_watching(watch.id()));
std::fs::create_dir(&target).expect("create the target directory");
let deadline = Instant::now() + Duration::from_secs(10);
while !monitor.is_watching(watch.id()) {
assert!(
Instant::now() < deadline,
"the subscription never established once its target appeared"
);
std::thread::sleep(Duration::from_millis(10));
}
assert_eq!(monitor.is_faulted(watch.id()), Some(false));
drop(receiver);
drop(watch);
drop(monitor);
dir.cleanup();
}
#[test]
fn an_interactive_subscription_is_asked_and_its_answer_speeds_up_establishment() {
let dir = TempDir::new("monitor-retry-interactive");
let target = dir.path().join("not-yet");
let monitor = Monitor::new().expect("create the monitor");
let (session, receiver) = monitor.session();
let watch = session
.subscribe(
&target,
WatchOptions::new().retry(crate::watch::RetryMode::Interactive),
)
.expect("register the subscription");
monitor.quiesce();
let deadline = Instant::now() + Duration::from_secs(10);
let mut asked = false;
while !asked {
assert!(
Instant::now() < deadline,
"the interactive subscription was never asked"
);
if let Some(notification) = receiver.try_recv()
&& matches!(notification, Notification::RetryQuestion { watch: id, .. } if id == watch.id())
{
asked = true;
}
std::thread::sleep(Duration::from_millis(5));
}
std::fs::create_dir(&target).expect("create the target directory");
session.answer(watch.id(), Some(Duration::from_millis(1)));
let deadline = Instant::now() + Duration::from_secs(10);
while !monitor.is_watching(watch.id()) {
assert!(
Instant::now() < deadline,
"the subscription never established after answering"
);
std::thread::sleep(Duration::from_millis(5));
}
drop(receiver);
drop(watch);
drop(monitor);
dir.cleanup();
}
#[test]
fn a_path_based_reopen_that_lands_on_a_new_directory_rekeys_so_a_later_subscription_still_coalesces()
{
let dir = TempDir::new("coalesce-rekey");
let monitor = Monitor::new().expect("create the monitor");
let (session, receiver) = monitor.session();
let watch = session
.subscribe(dir.path(), WatchOptions::new().report_liveness(true))
.expect("register");
monitor.quiesce();
assert_eq!(monitor.directory_count(), 1);
std::fs::remove_dir_all(dir.path()).expect("delete the watched directory");
let deadline = Instant::now() + Duration::from_secs(10);
loop {
assert!(Instant::now() < deadline, "the outage was never observed");
if let Some(notification) = receiver.try_recv()
&& matches!(notification, Notification::Suspended { watch: tag } if tag == watch.id())
{
break;
}
std::thread::sleep(Duration::from_millis(5));
}
std::fs::create_dir_all(dir.path()).expect("recreate the watched directory");
let deadline = Instant::now() + Duration::from_secs(10);
loop {
assert!(
Instant::now() < deadline,
"the watcher never reestablished against the recreated directory"
);
if let Some(notification) = receiver.try_recv()
&& matches!(notification, Notification::Resumed { watch: tag } if tag == watch.id())
{
break;
}
std::thread::sleep(Duration::from_millis(5));
}
let second = session
.subscribe(dir.path(), WatchOptions::new())
.expect("register a second subscription to the same, recreated path");
monitor.quiesce();
assert_eq!(
monitor.directory_count(),
1,
"the recreated directory's watcher must be re-keyed, not duplicated"
);
watch.cancel();
second.cancel();
monitor.quiesce();
assert_eq!(
monitor.directory_count(),
0,
"cancelling re-keyed routes must retire their shared watcher"
);
drop(monitor);
dir.cleanup();
}
#[test]
fn a_path_based_reopen_that_collides_with_another_watched_directory_migrates_routes_instead_of_dropping_them()
{
let dir_a = TempDir::new("collide-rekey-a");
let dir_b = TempDir::new("collide-rekey-b");
let monitor = Monitor::new().expect("create the monitor");
let (session, receiver) = monitor.session();
let watch_a = session
.subscribe(dir_a.path(), WatchOptions::new().report_liveness(true))
.expect("register the first subscription");
let watch_b = session
.subscribe(dir_b.path(), WatchOptions::new().report_liveness(true))
.expect("register the second subscription");
monitor.quiesce();
assert_eq!(monitor.directory_count(), 2);
std::fs::remove_dir_all(dir_b.path()).expect("delete dir_b");
let status = std::process::Command::new("cmd")
.arg("/C")
.arg("mklink")
.arg("/J")
.arg(dir_b.path())
.arg(dir_a.path())
.status()
.expect("run mklink");
assert!(status.success(), "mklink /J failed to create the junction");
let deadline = Instant::now() + Duration::from_secs(10);
loop {
assert!(
Instant::now() < deadline,
"watch_b never reestablished against the junctioned path"
);
if let Some(notification) = receiver.try_recv()
&& matches!(notification, Notification::Resumed { watch: tag } if tag == watch_b.id())
{
break;
}
std::thread::sleep(Duration::from_millis(5));
}
assert_eq!(
monitor.directory_count(),
1,
"the two watchers must have coalesced onto one, not left as two \
(which would mean the collision was never detected) and not zero \
(which would mean one was dropped without migrating its route)"
);
std::fs::write(dir_a.path().join("after-collision.txt"), b"x").expect("create a file");
let deadline = Instant::now() + Duration::from_secs(10);
loop {
assert!(
Instant::now() < deadline,
"watch_b's route was not migrated onto the surviving watcher"
);
if let Some(Notification::Batch { watch: tag, .. }) = receiver.try_recv()
&& tag == watch_b.id()
{
break;
}
std::thread::sleep(Duration::from_millis(5));
}
drop((watch_a, watch_b));
drop(monitor);
dir_a.cleanup();
dir_b.cleanup();
}
fn with_only_directory_watcher<T>(
monitor: &Monitor,
inspect: impl FnOnce(&crate::watcher::DirectoryWatcher) -> T,
) -> T {
let resident = lock(&monitor.resident);
assert_eq!(resident.directories.len(), 1);
inspect(
resident
.directories
.values()
.next()
.expect("the directory watcher exists"),
)
}
#[test]
fn request_debug_reports_the_variant_and_fields() {
let request = Request::Retry {
watch: WatchId::from_raw(17),
};
assert_eq!(format!("{request:?}"), "Retry { watch: WatchId(17) }");
}
#[test]
fn only_interactive_routes_with_a_standing_slot_await_an_open_answer() {
let cases = [
(RetryMode::Defaults, false, false),
(RetryMode::Defaults, true, false),
(RetryMode::Interactive, false, false),
(RetryMode::Interactive, true, true),
];
for (retry, has_fault_slot, expected) in cases {
assert_eq!(
awaits_open_answer(retry, has_fault_slot),
expected,
"retry={retry:?}, has_fault_slot={has_fault_slot}"
);
}
}
#[test]
fn established_is_reported_only_for_a_live_opted_in_route() {
let cases = [
(false, false, false),
(false, true, false),
(true, false, true),
(true, true, false),
];
for (report_liveness, is_faulted, expected) in cases {
assert_eq!(
should_report_established(report_liveness, is_faulted),
expected,
"report_liveness={report_liveness}, is_faulted={is_faulted}"
);
}
}
#[test]
fn file_target_parent_failures_retain_their_retry_classification() {
let dir = TempDir::new("monitor-file-parent-failures");
let missing_parent = dir.path().join("missing-parent").join("leaf.txt");
match open_file_target(&missing_parent) {
Opened::Pending(detail) => assert!(detail.failure.is_retryable()),
Opened::Handle { .. } | Opened::Failed(_) => {
panic!("a missing parent must remain retryable")
}
}
let file_parent = dir.path().join("not-a-directory");
std::fs::write(&file_parent, b"x").expect("create the non-directory parent");
match open_file_target(&file_parent.join("leaf.txt")) {
Opened::Failed(detail) => assert_eq!(detail.failure, OpenFailure::NotADirectory),
Opened::Handle { .. } | Opened::Pending(_) => {
panic!("a file used as the parent must fail permanently")
}
}
dir.cleanup();
}
#[test]
fn monitor_projects_a_routed_watchers_fault_and_permanent_stop() {
let dir = TempDir::new("monitor-project-watcher-state");
let monitor = Monitor::new().expect("create the monitor");
let (session, _receiver) = monitor.session();
let watch = session
.subscribe(
dir.path(),
WatchOptions::new().retry(RetryMode::Interactive),
)
.expect("register the subscription");
monitor.quiesce();
let detail = FaultDetail::synthetic(OpenFailure::Unsupported, ERROR_NOT_SUPPORTED);
with_only_directory_watcher(&monitor, |watcher| {
watcher.enter_fault_for_test(detail, FaultOperation::Arm);
});
assert_eq!(monitor.fault_detail(watch.id()), Some(detail));
assert!(monitor.stop_reason(watch.id()).is_none());
with_only_directory_watcher(&monitor, |watcher| {
watcher.record_stop_for_test(std::io::Error::from_raw_os_error(
i32::try_from(ERROR_ACCESS_DENIED).expect("Win32 errors fit in i32"),
));
});
assert_eq!(
monitor
.stop_reason(watch.id())
.and_then(|error| error.raw_os_error()),
Some(i32::try_from(ERROR_ACCESS_DENIED).expect("Win32 errors fit in i32"))
);
assert_eq!(monitor.fault_detail(watch.id()), None);
drop(watch);
drop(monitor);
dir.cleanup();
}
#[test]
fn a_volume_confirmation_slot_does_not_make_open_retries_interactive() {
let dir = TempDir::new("monitor-volume-slot-open-retry");
let target = dir.path().join("not-yet");
let monitor = Monitor::new().expect("create the monitor");
let (session, receiver) = monitor.session();
let watch = session
.subscribe(
&target,
WatchOptions::new().on_volume_change(VolumeChangePolicy::Confirm),
)
.expect("register the subscription");
monitor.quiesce();
let resident = lock(&monitor.resident);
let Some(Subscription::Pending {
awaiting_answer, ..
}) = resident.subscriptions.get(&watch.id())
else {
panic!("the missing target must be pending")
};
assert!(!awaiting_answer);
drop(resident);
while let Some(notification) = receiver.try_recv() {
assert!(
!matches!(notification, Notification::RetryQuestion { watch: id, .. } if id == watch.id()),
"a volume-only standing slot must not receive an open-fault question"
);
}
drop(watch);
drop(monitor);
dir.cleanup();
}
#[test]
fn coalescing_onto_a_faulted_watcher_suppresses_established() {
let dir = TempDir::new("monitor-faulted-coalesce");
let monitor = Monitor::new().expect("create the monitor");
let (session, receiver) = monitor.session();
let first = session
.subscribe(
dir.path(),
WatchOptions::new().retry(RetryMode::Interactive),
)
.expect("register the first subscription");
monitor.quiesce();
let detail = FaultDetail::synthetic(OpenFailure::Unsupported, ERROR_NOT_SUPPORTED);
with_only_directory_watcher(&monitor, |watcher| {
watcher.enter_fault_for_test(detail, FaultOperation::Arm);
});
let second = session
.subscribe(dir.path(), WatchOptions::new().report_liveness(true))
.expect("coalesce the second subscription");
monitor.quiesce();
while let Some(notification) = receiver.try_recv() {
assert!(
!matches!(notification, Notification::Established { watch, .. } if watch == second.id()),
"a faulted watcher has no settled tier to report"
);
}
drop((first, second));
drop(monitor);
dir.cleanup();
}
#[test]
fn stopping_the_only_route_after_a_volume_change_removes_its_resident_state() {
let dir = TempDir::new("monitor-volume-stop-cleanup");
let monitor = Monitor::new().expect("create the monitor");
let (session, receiver) = monitor.session();
let watch = session
.subscribe(
dir.path(),
WatchOptions::new().on_volume_change(VolumeChangePolicy::Confirm),
)
.expect("register the subscription");
monitor.quiesce();
let current = DirectoryHandle::open(dir.path())
.expect("open the watched directory")
.volume_identity()
.expect("read its volume identity");
let previous = [0, u32::MAX]
.into_iter()
.map(|serial| VolumeIdentity::synthetic(serial, "TEST-FS", "TEST-VOLUME"))
.find(|candidate| *candidate != current)
.expect("one synthetic serial differs from the real volume");
with_only_directory_watcher(&monitor, |watcher| {
watcher.simulate_volume_change_for_test(previous);
});
let mut asked = false;
while let Some(notification) = receiver.try_recv() {
if matches!(notification, Notification::VolumeChanged { watch: id, .. } if id == watch.id())
{
asked = true;
}
}
assert!(
asked,
"the confirming route must receive the volume question"
);
session.answer_volume_change(watch.id(), VolumeChangeDecision::Stop);
monitor.quiesce();
assert!(!monitor.is_registered(watch.id()));
assert_eq!(monitor.directory_count(), 0);
drop(watch);
drop(monitor);
dir.cleanup();
}