oxedyne/fe2o3/fe2o3_sys/tests/inotify.rs
22.8 KiB, 1 run
created by r1870400018:63105, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Tests for `fe2o3_sys::inotify` against the real kernel: real directories, real `mv` |
| 2 | //! and `rm`, a real queue overflow and a real descriptor limit. The oracle for what is |
| 3 | //! watched is `/proc/self/fdinfo/<fd>`, the kernel's own list of an instance's watches, |
| 4 | //! compared with the inodes `stat` reports for the directories that should be watched. |
| 5 | //! Only the per-user limits are never reached for real, since Syncthing and the desktop |
| 6 | //! share them; the budget stands in for the watch count and `prlimit` for the descriptors. |
| 7 | |
| 8 | #![cfg(all(target_os = "linux", feature = "inotify"))] |
| 9 | |
| 10 | use oxedyne_fe2o3_core::prelude::*; |
| 11 | use oxedyne_fe2o3_sys::inotify::{ |
| 12 | Limit, |
| 13 | Opened, |
| 14 | Renamed, |
| 15 | Wake, |
| 16 | Watch, |
| 17 | }; |
| 18 | |
| 19 | use std::{ |
| 20 | collections::BTreeSet, |
| 21 | fs::{ |
| 22 | self, |
| 23 | File, |
| 24 | }, |
| 25 | os::fd::AsRawFd, |
| 26 | path::{ |
| 27 | Path, |
| 28 | PathBuf, |
| 29 | }, |
| 30 | process::Command, |
| 31 | sync::{ |
| 32 | Mutex, |
| 33 | MutexGuard, |
| 34 | }, |
| 35 | time::{ |
| 36 | Duration, |
| 37 | Instant, |
| 38 | }, |
| 39 | }; |
| 40 | |
| 41 | const CHILD: &str = "FE2O3_SYS_INO_CHILD"; |
| 42 | const LONG: Duration = Duration::from_secs(5); |
| 43 | const QUIET: Duration = Duration::from_millis(300); |
| 44 | |
| 45 | static LOCK: Mutex<()> = Mutex::new(()); |
| 46 | |
| 47 | // Every test counts watches in this process, so they take turns. |
| 48 | fn serial() -> MutexGuard<'static, ()> { |
| 49 | match LOCK.lock() { |
| 50 | Ok(g) => g, |
| 51 | Err(p) => p.into_inner(), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fn io<T>(r: std::io::Result<T>, what: &str) -> Outcome<T> { |
| 56 | match r { |
| 57 | Ok(v) => Ok(v), |
| 58 | Err(e) => Err(err!("{}: {}.", what, e; IO, File)), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | struct Scratch(PathBuf); |
| 63 | |
| 64 | impl Scratch { |
| 65 | fn new(tag: &str) -> Outcome<Self> { |
| 66 | let dir = std::env::temp_dir().join(format!("fe2o3-sys-ino-t-{}-{}", tag, std::process::id())); |
| 67 | let _ = fs::remove_dir_all(&dir); |
| 68 | res!(io(fs::create_dir_all(&dir), "make the scratch directory")); |
| 69 | Ok(Self(dir)) |
| 70 | } |
| 71 | |
| 72 | fn root(&self) -> PathBuf { |
| 73 | self.0.join("root") |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | impl Drop for Scratch { |
| 78 | fn drop(&mut self) { |
| 79 | // A test may leave a directory it closed to itself. |
| 80 | let _ = Command::new("chmod").arg("-R").arg("u+rwx").arg(&self.0).status(); |
| 81 | let _ = fs::remove_dir_all(&self.0); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // The directories of a test tree, made under the scratch directory. |
| 86 | fn mkdirs(base: &Path, dirs: &[&str]) -> Outcome<()> { |
| 87 | for d in dirs { |
| 88 | res!(io(fs::create_dir_all(base.join(d)), d)); |
| 89 | } |
| 90 | Ok(()) |
| 91 | } |
| 92 | |
| 93 | fn touch(path: &Path) -> Outcome<()> { |
| 94 | res!(io(File::create(path), "create a file")); |
| 95 | Ok(()) |
| 96 | } |
| 97 | |
| 98 | fn sh(dir: &Path, script: &str) -> Outcome<()> { |
| 99 | let out = res!(io(Command::new("sh").arg("-c").arg(script).current_dir(dir).output(), script)); |
| 100 | if !out.status.success() { |
| 101 | return Err(err!( |
| 102 | "`{}` failed: {}", script, String::from_utf8_lossy(&out.stderr); Test)); |
| 103 | } |
| 104 | Ok(()) |
| 105 | } |
| 106 | |
| 107 | // Directories a tree's owner would not read, by any name along the path. |
| 108 | fn admit(path: &[u8]) -> bool { |
| 109 | !path.split(|b| *b == b'/').any(|c| |
| 110 | matches!(c, b".git" | b".ore" | b"target" | b"skip")) |
| 111 | } |
| 112 | |
| 113 | fn all(_: &[u8]) -> bool { |
| 114 | true |
| 115 | } |
| 116 | |
| 117 | fn open<F>(root: &Path, budget: usize, admit: F) -> Outcome<Watch> |
| 118 | where F: FnMut(&[u8]) -> bool, |
| 119 | { |
| 120 | match res!(Watch::open(root, budget, admit)) { |
| 121 | Opened::Watching(w) => Ok(w), |
| 122 | Opened::Exhausted(l) => Err(err!("Expected a watch, but the tree is exhausted: {:?}.", l; Test)), |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | fn fdinfo(fd: i32) -> Outcome<String> { |
| 127 | io(fs::read_to_string(format!("/proc/self/fdinfo/{}", fd)), "read fdinfo") |
| 128 | } |
| 129 | |
| 130 | // The inodes the kernel says an instance watches. |
| 131 | fn watched(w: &Watch) -> Outcome<BTreeSet<u64>> { |
| 132 | let body = res!(fdinfo(w.as_raw_fd())); |
| 133 | let mut set = BTreeSet::new(); |
| 134 | for line in body.lines().filter(|l| l.starts_with("inotify wd:")) { |
| 135 | let tok = res!(line.split_whitespace().find(|t| t.starts_with("ino:")) |
| 136 | .ok_or_else(|| err!("No ino in fdinfo line {:?}.", line; Test))); |
| 137 | set.insert(res!(io(u64::from_str_radix(&tok[4..], 16).map_err(|e| |
| 138 | std::io::Error::new(std::io::ErrorKind::InvalidData, e)), "parse an inode"))); |
| 139 | } |
| 140 | Ok(set) |
| 141 | } |
| 142 | |
| 143 | // The inodes `stat` reports for the given paths. |
| 144 | fn inodes(paths: &[PathBuf]) -> Outcome<BTreeSet<u64>> { |
| 145 | let out = res!(io(Command::new("stat").arg("-c").arg("%i").args(paths).output(), "stat")); |
| 146 | if !out.status.success() { |
| 147 | return Err(err!("stat failed: {}", String::from_utf8_lossy(&out.stderr); Test)); |
| 148 | } |
| 149 | let mut set = BTreeSet::new(); |
| 150 | for line in String::from_utf8_lossy(&out.stdout).lines() { |
| 151 | set.insert(res!(io(line.trim().parse::<u64>().map_err(|e| |
| 152 | std::io::Error::new(std::io::ErrorKind::InvalidData, e)), "parse a stat inode"))); |
| 153 | } |
| 154 | Ok(set) |
| 155 | } |
| 156 | |
| 157 | // Exactly the root and the named directories hold watches, as the kernel lists them. |
| 158 | fn assert_placed(w: &Watch, root: &Path, rel: &[&str]) -> Outcome<()> { |
| 159 | let mut paths = vec![root.to_path_buf()]; |
| 160 | for r in rel { |
| 161 | paths.push(root.join(r)); |
| 162 | } |
| 163 | let want = res!(inodes(&paths)); |
| 164 | let got = res!(watched(w)); |
| 165 | assert_eq!(got, want, "kernel's watch list against the admitted directories"); |
| 166 | assert_eq!(w.watches(), want.len(), "the watcher's own count"); |
| 167 | Ok(()) |
| 168 | } |
| 169 | |
| 170 | // Every watch held by every inotify instance of this process. |
| 171 | fn process_watches() -> Outcome<usize> { |
| 172 | let mut n = 0; |
| 173 | for ent in res!(io(fs::read_dir("/proc/self/fd"), "list descriptors")) { |
| 174 | let ent = res!(io(ent, "read a descriptor entry")); |
| 175 | match fs::read_link(ent.path()) { |
| 176 | Ok(t) if t == Path::new("anon_inode:inotify") => { |
| 177 | let body = res!(io(fs::read_to_string( |
| 178 | format!("/proc/self/fdinfo/{}", ent.file_name().to_string_lossy())), "read fdinfo")); |
| 179 | n += body.lines().filter(|l| l.starts_with("inotify wd:")).count(); |
| 180 | }, |
| 181 | _ => (), |
| 182 | } |
| 183 | } |
| 184 | Ok(n) |
| 185 | } |
| 186 | |
| 187 | fn ren(from: &str, to: &str, dir: bool) -> Renamed { |
| 188 | Renamed { from: from.as_bytes().to_vec(), to: to.as_bytes().to_vec(), dir } |
| 189 | } |
| 190 | |
| 191 | fn moved(pairs: Vec<Renamed>) -> Wake { |
| 192 | Wake::Moved(pairs) |
| 193 | } |
| 194 | |
| 195 | // 30 directories to be watched, and the ones that are not. |
| 196 | fn big_tree(root: &Path) -> Outcome<Vec<String>> { |
| 197 | let mut admitted = Vec::new(); |
| 198 | for i in 0..5 { |
| 199 | admitted.push(format!("d{}", i)); |
| 200 | for j in 0..5 { |
| 201 | admitted.push(format!("d{}/s{}", i, j)); |
| 202 | } |
| 203 | } |
| 204 | for a in &admitted { |
| 205 | res!(io(fs::create_dir_all(root.join(a)), a)); |
| 206 | } |
| 207 | res!(mkdirs(root, &[".git/objects/pack", ".ore/store", "target/debug/deps", "d0/target/inner"])); |
| 208 | res!(mkdirs(root.parent().unwrap_or(root), &["outside/sub"])); |
| 209 | res!(sh(root, "ln -s d1 link_in && ln -s ../outside link_out && ln -s ../outside/sub deep_link")); |
| 210 | Ok(admitted) |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn placement_is_exactly_the_admitted_directories() -> Outcome<()> { |
| 215 | let _g = serial(); |
| 216 | let s = res!(Scratch::new("place")); |
| 217 | let root = s.root(); |
| 218 | res!(mkdirs(&root, &[""])); |
| 219 | let admitted = res!(big_tree(&root)); |
| 220 | assert_eq!(admitted.len(), 30); |
| 221 | let mut asked: Vec<Vec<u8>> = Vec::new(); |
| 222 | let w = res!(open(&root, 8_192, |p| { asked.push(p.to_vec()); admit(p) })); |
| 223 | let rel: Vec<&str> = admitted.iter().map(|a| a.as_str()).collect(); |
| 224 | res!(assert_placed(&w, &root, &rel)); |
| 225 | // The predicate is never asked about the root, nor about anything inside a directory |
| 226 | // it refused, nor about a link, whatever the link leads to. |
| 227 | assert!(asked.iter().all(|p| !p.is_empty())); |
| 228 | assert!(asked.iter().all(|p| p.split(|b| *b == b'/').skip(1).all(|c| c != b"inner"))); |
| 229 | assert!(asked.iter().all(|p| !p.starts_with(b".git/") && !p.starts_with(b"target/"))); |
| 230 | assert!(asked.iter().all(|p| p != b"link_in" && p != b"link_out" && p != b"deep_link")); |
| 231 | assert!(asked.contains(&b"target".to_vec()) && asked.contains(&b"d0/target".to_vec())); |
| 232 | // Nothing the links lead to is watched, so writing through them is silent. |
| 233 | let mut w = w; |
| 234 | res!(sh(&root, "touch link_out/a link_out/sub/b deep_link/c link_in/d")); |
| 235 | assert_eq!(res!(w.wait(QUIET, admit)), moved(Vec::new()), "link_in/d is inside the tree"); |
| 236 | res!(sh(&root, "touch link_out/e deep_link/f")); |
| 237 | assert_eq!(res!(w.wait(QUIET, admit)), Wake::Timeout); |
| 238 | res!(sh(&root, "touch .git/x .ore/y target/z d0/target/w")); |
| 239 | assert_eq!(res!(w.wait(QUIET, admit)), Wake::Timeout); |
| 240 | res!(assert_placed(&w, &root, &rel)); |
| 241 | Ok(()) |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | fn a_directory_closed_to_the_process_is_skipped() -> Outcome<()> { |
| 246 | let _g = serial(); |
| 247 | let s = res!(Scratch::new("locked")); |
| 248 | let root = s.root(); |
| 249 | res!(mkdirs(&root, &["open/in", "locked/in"])); |
| 250 | res!(sh(&root, "chmod 000 locked")); |
| 251 | let w = res!(open(&root, 8_192, all)); |
| 252 | res!(assert_placed(&w, &root, &["open", "open/in"])); |
| 253 | Ok(()) |
| 254 | } |
| 255 | |
| 256 | #[test] |
| 257 | fn a_tree_made_in_a_flash_is_placed_whole() -> Outcome<()> { |
| 258 | let _g = serial(); |
| 259 | let s = res!(Scratch::new("race")); |
| 260 | let root = s.root(); |
| 261 | res!(mkdirs(&root, &[""])); |
| 262 | let mut w = res!(open(&root, 8_192, all)); |
| 263 | res!(sh(&root, "mkdir -p a/b/c && touch a/b/c/f")); |
| 264 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 265 | res!(assert_placed(&w, &root, &["a", "a/b", "a/b/c"])); |
| 266 | // The tree built in that flash is as live as any other. |
| 267 | res!(sh(&root, "touch a/b/c/g")); |
| 268 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 269 | Ok(()) |
| 270 | } |
| 271 | |
| 272 | #[test] |
| 273 | fn a_file_renamed_in_place_is_paired() -> Outcome<()> { |
| 274 | let _g = serial(); |
| 275 | let s = res!(Scratch::new("mvfile")); |
| 276 | let root = s.root(); |
| 277 | res!(mkdirs(&root, &["d"])); |
| 278 | res!(touch(&root.join("x"))); |
| 279 | let mut w = res!(open(&root, 8_192, all)); |
| 280 | res!(sh(&root, "mv x y")); |
| 281 | assert_eq!(res!(w.wait(LONG, all)), moved(vec![ren("x", "y", false)])); |
| 282 | Ok(()) |
| 283 | } |
| 284 | |
| 285 | #[test] |
| 286 | fn a_file_moved_into_a_subdirectory_is_paired() -> Outcome<()> { |
| 287 | let _g = serial(); |
| 288 | let s = res!(Scratch::new("mvinto")); |
| 289 | let root = s.root(); |
| 290 | res!(mkdirs(&root, &["d"])); |
| 291 | res!(touch(&root.join("x"))); |
| 292 | let mut w = res!(open(&root, 8_192, all)); |
| 293 | res!(sh(&root, "mv x d/y")); |
| 294 | assert_eq!(res!(w.wait(LONG, all)), moved(vec![ren("x", "d/y", false)])); |
| 295 | Ok(()) |
| 296 | } |
| 297 | |
| 298 | #[test] |
| 299 | fn a_directory_rename_moves_the_prefix_of_what_follows() -> Outcome<()> { |
| 300 | let _g = serial(); |
| 301 | let s = res!(Scratch::new("mvdir")); |
| 302 | let root = s.root(); |
| 303 | res!(mkdirs(&root, &["d/sub"])); |
| 304 | res!(touch(&root.join("d/z"))); |
| 305 | let mut w = res!(open(&root, 8_192, all)); |
| 306 | res!(sh(&root, "mv d e")); |
| 307 | assert_eq!(res!(w.wait(LONG, all)), moved(vec![ren("d", "e", true)])); |
| 308 | res!(assert_placed(&w, &root, &["e", "e/sub"])); |
| 309 | // The watch on the moved directory reports under its new name. |
| 310 | res!(sh(&root, "mv e/z e/w")); |
| 311 | assert_eq!(res!(w.wait(LONG, all)), moved(vec![ren("e/z", "e/w", false)])); |
| 312 | res!(sh(&root, "mv e/sub/ e/sub2 && touch e/sub2/q")); |
| 313 | assert_eq!(res!(w.wait(LONG, all)), moved(vec![ren("e/sub", "e/sub2", true)])); |
| 314 | res!(assert_placed(&w, &root, &["e", "e/sub2"])); |
| 315 | Ok(()) |
| 316 | } |
| 317 | |
| 318 | #[test] |
| 319 | fn renames_in_one_read_each_name_the_paths_of_their_moment() -> Outcome<()> { |
| 320 | let _g = serial(); |
| 321 | let s = res!(Scratch::new("seq")); |
| 322 | let root = s.root(); |
| 323 | res!(mkdirs(&root, &["d", "a", "b"])); |
| 324 | res!(touch(&root.join("d/z"))); |
| 325 | res!(touch(&root.join("b/f"))); |
| 326 | let mut w = res!(open(&root, 8_192, all)); |
| 327 | // Both moves are queued before the first read. |
| 328 | res!(sh(&root, "mv d e && mv e/z e/w")); |
| 329 | assert_eq!(res!(w.wait(LONG, all)), |
| 330 | moved(vec![ren("d", "e", true), ren("e/z", "e/w", false)])); |
| 331 | // A swap through a third name, then a file renamed inside what is now `a`. |
| 332 | res!(sh(&root, "mv a t && mv b a && mv t b && mv a/f a/g")); |
| 333 | assert_eq!(res!(w.wait(LONG, all)), moved(vec![ |
| 334 | ren("a", "t", true), |
| 335 | ren("b", "a", true), |
| 336 | ren("t", "b", true), |
| 337 | ren("a/f", "a/g", false), |
| 338 | ])); |
| 339 | res!(assert_placed(&w, &root, &["e", "a", "b"])); |
| 340 | Ok(()) |
| 341 | } |
| 342 | |
| 343 | #[test] |
| 344 | fn a_directory_moved_out_loses_its_watches() -> Outcome<()> { |
| 345 | let _g = serial(); |
| 346 | let s = res!(Scratch::new("out")); |
| 347 | let root = s.root(); |
| 348 | res!(mkdirs(&root, &["d/sub", "keep"])); |
| 349 | let mut w = res!(open(&root, 8_192, all)); |
| 350 | res!(assert_placed(&w, &root, &["d", "d/sub", "keep"])); |
| 351 | res!(sh(&s.0, "mv root/d out")); |
| 352 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 353 | res!(assert_placed(&w, &root, &["keep"])); |
| 354 | // Writes where it went are not the tree's business. |
| 355 | res!(sh(&s.0, "touch out/f out/sub/g")); |
| 356 | assert_eq!(res!(w.wait(QUIET, all)), Wake::Timeout); |
| 357 | res!(assert_placed(&w, &root, &["keep"])); |
| 358 | Ok(()) |
| 359 | } |
| 360 | |
| 361 | #[test] |
| 362 | fn a_directory_moved_in_is_watched_whole() -> Outcome<()> { |
| 363 | let _g = serial(); |
| 364 | let s = res!(Scratch::new("in")); |
| 365 | let root = s.root(); |
| 366 | res!(mkdirs(&root, &[""])); |
| 367 | res!(mkdirs(&s.0, &["in/p/q", "in/r"])); |
| 368 | let mut w = res!(open(&root, 8_192, all)); |
| 369 | res!(sh(&s.0, "mv in root/d2")); |
| 370 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 371 | res!(assert_placed(&w, &root, &["d2", "d2/p", "d2/p/q", "d2/r"])); |
| 372 | res!(sh(&root, "touch d2/p/q/f")); |
| 373 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 374 | Ok(()) |
| 375 | } |
| 376 | |
| 377 | #[test] |
| 378 | fn a_directory_renamed_to_an_excluded_name_is_released() -> Outcome<()> { |
| 379 | let _g = serial(); |
| 380 | let s = res!(Scratch::new("toskip")); |
| 381 | let root = s.root(); |
| 382 | res!(mkdirs(&root, &["d/sub", "skip"])); |
| 383 | let mut w = res!(open(&root, 8_192, admit)); |
| 384 | res!(assert_placed(&w, &root, &["d", "d/sub"])); |
| 385 | // Both ends of this rename are watched, so the kernel pairs it, but the new name is |
| 386 | // one the tree's owner does not read. |
| 387 | res!(sh(&root, "mv d target")); |
| 388 | assert_eq!(res!(w.wait(LONG, admit)), moved(vec![ren("d", "target", true)])); |
| 389 | res!(assert_placed(&w, &root, &[])); |
| 390 | res!(sh(&root, "touch target/f target/sub/g")); |
| 391 | assert_eq!(res!(w.wait(QUIET, admit)), Wake::Timeout); |
| 392 | // And back to a name it reads, which watches the directory afresh. |
| 393 | res!(sh(&root, "mv target d")); |
| 394 | assert_eq!(res!(w.wait(LONG, admit)), moved(vec![ren("target", "d", true)])); |
| 395 | res!(assert_placed(&w, &root, &["d", "d/sub"])); |
| 396 | // Into an unwatched directory only the departure is seen, and nothing pairs. |
| 397 | res!(sh(&root, "mv d skip/d")); |
| 398 | assert_eq!(res!(w.wait(LONG, admit)), moved(Vec::new())); |
| 399 | res!(assert_placed(&w, &root, &[])); |
| 400 | res!(sh(&root, "touch skip/d/f skip/d/sub/g")); |
| 401 | assert_eq!(res!(w.wait(QUIET, admit)), Wake::Timeout); |
| 402 | res!(sh(&root, "mv skip/d d")); |
| 403 | assert_eq!(res!(w.wait(LONG, admit)), moved(Vec::new())); |
| 404 | res!(assert_placed(&w, &root, &["d", "d/sub"])); |
| 405 | Ok(()) |
| 406 | } |
| 407 | |
| 408 | #[test] |
| 409 | fn a_directory_deleted_and_made_again_is_watched_afresh() -> Outcome<()> { |
| 410 | let _g = serial(); |
| 411 | let s = res!(Scratch::new("again")); |
| 412 | let root = s.root(); |
| 413 | res!(mkdirs(&root, &["d/old", "e/deep/er"])); |
| 414 | let mut w = res!(open(&root, 8_192, all)); |
| 415 | res!(sh(&root, "rmdir d/old d && mkdir d d/n")); |
| 416 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 417 | res!(assert_placed(&w, &root, &["d", "d/n", "e", "e/deep", "e/deep/er"])); |
| 418 | res!(sh(&root, "rm -r e")); |
| 419 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 420 | res!(assert_placed(&w, &root, &["d", "d/n"])); |
| 421 | Ok(()) |
| 422 | } |
| 423 | |
| 424 | #[test] |
| 425 | fn a_flooded_queue_reads_as_look_now_and_the_tree_is_placed_again() -> Outcome<()> { |
| 426 | let _g = serial(); |
| 427 | let s = res!(Scratch::new("flood")); |
| 428 | let root = s.root(); |
| 429 | res!(mkdirs(&root, &["pre"])); |
| 430 | let mut w = res!(open(&root, 8_192, all)); |
| 431 | let max: usize = res!(io(fs::read_to_string("/proc/sys/fs/inotify/max_queued_events"), "read the queue size")) |
| 432 | .trim().parse().unwrap_or(16_384); |
| 433 | // Each file is two events, a create and a close, so this overruns the queue twice. |
| 434 | for i in 0..(max + 1_000) { |
| 435 | res!(touch(&root.join(format!("f{}", i)))); |
| 436 | } |
| 437 | // Made once the queue is full, so the kernel drops what announces them. |
| 438 | res!(sh(&root, "mkdir -p late/sub && touch late/sub/f")); |
| 439 | assert_eq!(res!(w.wait(LONG, all)), Wake::LookNow); |
| 440 | res!(assert_placed(&w, &root, &["pre", "late", "late/sub"])); |
| 441 | assert_eq!(res!(w.wait(QUIET, all)), Wake::Timeout); |
| 442 | res!(sh(&root, "touch late/sub/g")); |
| 443 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 444 | Ok(()) |
| 445 | } |
| 446 | |
| 447 | #[test] |
| 448 | fn a_budget_too_small_for_the_tree_releases_it_at_open() -> Outcome<()> { |
| 449 | let _g = serial(); |
| 450 | let s = res!(Scratch::new("budget-open")); |
| 451 | let root = s.root(); |
| 452 | res!(mkdirs(&root, &["a/b", "c/d", "e/f", "g"])); |
| 453 | assert_eq!(res!(process_watches()), 0); |
| 454 | // Root and seven directories, against a budget of five. |
| 455 | match res!(Watch::open(&root, 5, all)) { |
| 456 | Opened::Exhausted(Limit::Budget) => (), |
| 457 | other => return Err(err!("Expected Exhausted(Budget), got {:?}.", other; Test)), |
| 458 | } |
| 459 | assert_eq!(res!(process_watches()), 0, "no instance of this process keeps a watch"); |
| 460 | // The same tree fits a budget of eight, to the watch. |
| 461 | let w = res!(open(&root, 8, all)); |
| 462 | assert_eq!(w.watches(), 8); |
| 463 | assert_eq!(res!(process_watches()), 8); |
| 464 | Ok(()) |
| 465 | } |
| 466 | |
| 467 | #[test] |
| 468 | fn a_tree_that_outgrows_its_budget_is_released_and_stays_released() -> Outcome<()> { |
| 469 | let _g = serial(); |
| 470 | let s = res!(Scratch::new("budget-wait")); |
| 471 | let root = s.root(); |
| 472 | res!(mkdirs(&root, &["a", "b"])); |
| 473 | let mut w = res!(open(&root, 5, all)); |
| 474 | assert_eq!(res!(watched(&w)).len(), 3); |
| 475 | res!(mkdirs(&root, &["c", "d", "e/f"])); |
| 476 | assert_eq!(res!(w.wait(LONG, all)), Wake::Exhausted(Limit::Budget)); |
| 477 | assert_eq!(res!(watched(&w)).len(), 0, "the kernel lists no watch"); |
| 478 | assert_eq!(w.watches(), 0); |
| 479 | // It is a tree's last word; nothing is placed behind the caller's back. |
| 480 | res!(sh(&root, "touch c/x")); |
| 481 | assert_eq!(res!(w.wait(QUIET, all)), Wake::Exhausted(Limit::Budget)); |
| 482 | assert_eq!(res!(watched(&w)).len(), 0); |
| 483 | Ok(()) |
| 484 | } |
| 485 | |
| 486 | // Re-run by the test below under a descriptor limit, in one of two modes. |
| 487 | fn exhausted_child(mode: &str) -> Outcome<()> { |
| 488 | let s = res!(Scratch::new(&format!("emfile-{}", mode))); |
| 489 | let root = s.root(); |
| 490 | res!(mkdirs(&root, &["a/b", "c"])); |
| 491 | let mut held = Vec::new(); |
| 492 | while let Ok(f) = File::open("/dev/null") { |
| 493 | held.push(f); |
| 494 | } |
| 495 | if mode == "list" { |
| 496 | // One descriptor left: enough for the instance, not for listing a directory. |
| 497 | held.pop(); |
| 498 | } |
| 499 | let opened = res!(Watch::open(&root, 8_192, all)); |
| 500 | drop(held); |
| 501 | match opened { |
| 502 | Opened::Exhausted(Limit::Instances) => Ok(()), |
| 503 | other => Err(err!("Expected Exhausted(Instances), got {:?}.", other; Test)), |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | #[test] |
| 508 | fn running_out_of_descriptors_reads_as_instances() -> Outcome<()> { |
| 509 | if let Ok(mode) = std::env::var(CHILD) { |
| 510 | return exhausted_child(&mode); |
| 511 | } |
| 512 | let _g = serial(); |
| 513 | let exe = res!(io(std::env::current_exe(), "find this test binary")); |
| 514 | for mode in ["init", "list"] { |
| 515 | // A real EMFILE from the kernel, under a real descriptor limit. |
| 516 | let out = res!(io(Command::new("prlimit") |
| 517 | .arg("--nofile=64:64") |
| 518 | .arg(&exe) |
| 519 | .args(["--exact", "running_out_of_descriptors_reads_as_instances", |
| 520 | "--test-threads=1", "--nocapture"]) |
| 521 | .env(CHILD, mode) |
| 522 | .output(), "run prlimit")); |
| 523 | assert!(out.status.success(), |
| 524 | "child in mode {} failed:\n{}\n{}", mode, |
| 525 | String::from_utf8_lossy(&out.stdout), String::from_utf8_lossy(&out.stderr)); |
| 526 | assert!(String::from_utf8_lossy(&out.stdout).contains("1 passed"), |
| 527 | "child in mode {} ran no test:\n{}", mode, String::from_utf8_lossy(&out.stdout)); |
| 528 | } |
| 529 | Ok(()) |
| 530 | } |
| 531 | |
| 532 | #[test] |
| 533 | fn a_still_tree_times_out_on_time() -> Outcome<()> { |
| 534 | let _g = serial(); |
| 535 | let s = res!(Scratch::new("still")); |
| 536 | let root = s.root(); |
| 537 | res!(mkdirs(&root, &["a/b"])); |
| 538 | let mut w = res!(open(&root, 8_192, all)); |
| 539 | let t = Instant::now(); |
| 540 | assert_eq!(res!(w.wait(Duration::from_millis(300), all)), Wake::Timeout); |
| 541 | let took = t.elapsed(); |
| 542 | println!("a wait of 300ms on a still tree took {:?}", took); |
| 543 | assert!(took >= Duration::from_millis(300) && took < Duration::from_millis(500), |
| 544 | "waited {:?}", took); |
| 545 | // And a zero wait looks and returns. |
| 546 | let t = Instant::now(); |
| 547 | assert_eq!(res!(w.wait(Duration::ZERO, all)), Wake::Timeout); |
| 548 | assert!(t.elapsed() < Duration::from_millis(100)); |
| 549 | Ok(()) |
| 550 | } |
| 551 | |
| 552 | #[test] |
| 553 | fn writes_inside_an_excluded_directory_are_silent() -> Outcome<()> { |
| 554 | let _g = serial(); |
| 555 | let s = res!(Scratch::new("excluded")); |
| 556 | let root = s.root(); |
| 557 | res!(mkdirs(&root, &["skip/deep", "keep"])); |
| 558 | let mut w = res!(open(&root, 8_192, admit)); |
| 559 | res!(assert_placed(&w, &root, &["keep"])); |
| 560 | res!(sh(&root, "touch skip/a skip/deep/b && mv skip/a skip/deep/c && rm skip/deep/b")); |
| 561 | assert_eq!(res!(w.wait(QUIET, admit)), Wake::Timeout); |
| 562 | // A write in a watched directory does wake it, so the silence above was earned. |
| 563 | res!(sh(&root, "touch keep/a")); |
| 564 | assert_eq!(res!(w.wait(LONG, admit)), moved(Vec::new())); |
| 565 | // A new directory the owner reads is watched; one the owner does not is seen and left. |
| 566 | res!(sh(&root, "mkdir skip2 && mkdir target")); |
| 567 | assert_eq!(res!(w.wait(LONG, admit)), moved(Vec::new())); |
| 568 | res!(assert_placed(&w, &root, &["keep", "skip2"])); |
| 569 | Ok(()) |
| 570 | } |
| 571 | |
| 572 | #[test] |
| 573 | fn deleting_the_root_reads_as_root_gone() -> Outcome<()> { |
| 574 | let _g = serial(); |
| 575 | let s = res!(Scratch::new("rootgone")); |
| 576 | let root = s.root(); |
| 577 | res!(mkdirs(&root, &["a/b", "c"])); |
| 578 | let mut w = res!(open(&root, 8_192, all)); |
| 579 | res!(sh(&s.0, "rm -r root")); |
| 580 | assert_eq!(res!(w.wait(LONG, all)), Wake::RootGone); |
| 581 | assert_eq!(res!(watched(&w)).len(), 0); |
| 582 | assert_eq!(w.watches(), 0); |
| 583 | assert_eq!(res!(w.wait(QUIET, all)), Wake::RootGone); |
| 584 | Ok(()) |
| 585 | } |
| 586 | |
| 587 | #[test] |
| 588 | fn moving_the_root_away_reads_as_root_gone() -> Outcome<()> { |
| 589 | let _g = serial(); |
| 590 | let s = res!(Scratch::new("rootmoved")); |
| 591 | let root = s.root(); |
| 592 | res!(mkdirs(&root, &["a"])); |
| 593 | let mut w = res!(open(&root, 8_192, all)); |
| 594 | res!(sh(&s.0, "mv root elsewhere")); |
| 595 | assert_eq!(res!(w.wait(LONG, all)), Wake::RootGone); |
| 596 | assert_eq!(res!(watched(&w)).len(), 0); |
| 597 | // A new directory at the old path is for a new watch to place. |
| 598 | res!(mkdirs(&root, &["a"])); |
| 599 | assert_eq!(res!(w.wait(QUIET, all)), Wake::RootGone); |
| 600 | let w2 = res!(open(&root, 8_192, all)); |
| 601 | res!(assert_placed(&w2, &root, &["a"])); |
| 602 | Ok(()) |
| 603 | } |
| 604 | |
| 605 | #[test] |
| 606 | fn a_root_named_by_a_link_is_watched_through_it() -> Outcome<()> { |
| 607 | let _g = serial(); |
| 608 | let s = res!(Scratch::new("rootlink")); |
| 609 | let root = s.root(); |
| 610 | res!(mkdirs(&root, &["a"])); |
| 611 | res!(sh(&s.0, "ln -s root rootlink")); |
| 612 | let mut w = res!(open(&s.0.join("rootlink"), 8_192, all)); |
| 613 | res!(assert_placed(&w, &root, &["a"])); |
| 614 | res!(sh(&root, "touch a/f")); |
| 615 | assert_eq!(res!(w.wait(LONG, all)), moved(Vec::new())); |
| 616 | Ok(()) |
| 617 | } |
| 618 | |
| 619 | #[test] |
| 620 | fn a_root_that_is_not_a_directory_is_refused() -> Outcome<()> { |
| 621 | let _g = serial(); |
| 622 | let s = res!(Scratch::new("badroot")); |
| 623 | res!(touch(&s.0.join("file"))); |
| 624 | assert!(Watch::open(&s.0.join("file"), 8_192, all).is_err()); |
| 625 | assert!(Watch::open(&s.0.join("missing"), 8_192, all).is_err()); |
| 626 | assert_eq!(res!(process_watches()), 0); |
| 627 | Ok(()) |
| 628 | } |