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());
}
fn drained_by_now(watch: &mut DrainWatch) -> bool {
watch.drained_by(std::time::Instant::now())
}
#[skuld::test]
fn a_drained_leaf_is_drained_by_the_deadline() {
let fake = FakeLeaf::new("cosca-watched", false);
let mut watch = armed(&fake);
assert!(drained_by_now(&mut watch));
}
#[skuld::test]
fn a_leaf_still_populated_at_the_deadline_is_not_drained_by_it() {
let fake = FakeLeaf::new("cosca-watched", true);
let mut watch = armed(&fake);
assert!(!drained_by_now(&mut watch));
}
#[skuld::test]
fn a_leaf_removed_during_the_last_round_is_drained() {
let fake = FakeLeaf::new("cosca-watched", true);
let mut watch = armed(&fake);
FakeLeaf::remove(&fake.leaf);
assert!(drained_by_now(&mut watch));
assert!(watch.saw_removal());
}
#[skuld::test]
fn a_leaf_whose_state_cannot_be_read_is_left_to_the_wait() {
let fake = FakeLeaf::new("cosca-watched", true);
let mut watch = armed(&fake);
std::fs::write(&fake.events, "frozen 0\n").expect("write a cgroup.events with no populated field");
assert!(drained_by_now(&mut watch));
assert!(watch.wait().is_err(), "the wait finds the failure the look left it");
}