use super::DrainWatch;
use crate::containment::cgroup::test_support::FakeLeaf;
use crate::containment::cgroup::LeafDir;
fn armed(fake: &FakeLeaf) -> DrainWatch {
DrainWatch::arm(&LeafDir::open_for_test(&fake.leaf))
.expect("arm")
.expect("the leaf exists")
}
#[skuld::test]
fn a_siblings_removal_is_not_a_change_to_the_leaf() {
let fake = FakeLeaf::new("cosca-watched", true);
let mut watch = armed(&fake);
let sibling = fake.leaf.with_file_name("cosca-sibling");
std::fs::create_dir(&sibling).expect("make a sibling");
std::fs::remove_dir(&sibling).expect("remove the sibling");
assert!(!watch.consume().expect("consume"), "a sibling's removal is no change");
assert!(!watch.saw_removal());
}
#[skuld::test]
fn an_events_write_and_the_leafs_removal_are_changes() {
let fake = FakeLeaf::new("cosca-watched", true);
let mut watch = armed(&fake);
assert!(!watch.consume().expect("consume"), "nothing happened yet");
FakeLeaf::set_populated(&fake.events, false);
assert!(watch.consume().expect("consume"), "cgroup.events was written");
FakeLeaf::remove(&fake.leaf);
assert!(watch.consume().expect("consume"), "the leaf was removed");
assert!(watch.saw_removal());
}
#[skuld::test]
fn an_overflowed_queue_is_a_change() {
let max: usize = std::fs::read_to_string("/proc/sys/fs/inotify/max_queued_events")
.expect("read fs.inotify.max_queued_events")
.trim()
.parse()
.expect("a count");
let fake = FakeLeaf::new("cosca-watched", true);
let siblings = [
fake.leaf.with_file_name("cosca-sibling-a"),
fake.leaf.with_file_name("cosca-sibling-b"),
];
let mut watch = armed(&fake);
for i in 0..=max {
let sibling = &siblings[i % siblings.len()];
std::fs::create_dir(sibling).expect("make a sibling");
std::fs::remove_dir(sibling).expect("remove the sibling");
}
assert!(watch.consume().expect("consume"), "the queue overflowed");
assert!(!watch.saw_removal());
}