//! The guard itself: a wait outside a hold passes, a wait inside one panics //! naming both, and the mark leaves with the guard — a panic included. use super::*; #[test] fn a_wait_with_nothing_held_passes() { must_not_wait("an outbound HTTP call"); assert_eq!(held(), None); } #[test] fn a_wait_inside_a_hold_panics_naming_the_wait_and_the_file() { let _file = holding("the served file"); let panic = std::panic::catch_unwind(|| must_not_wait("the statements slot")) .expect_err("a wait inside a hold must panic in a debug build"); let message = panic .downcast_ref::() .cloned() .unwrap_or_default(); assert!(message.contains("the statements slot"), "{message}"); assert!(message.contains("the served file"), "{message}"); } #[test] fn the_mark_leaves_with_the_guard_and_nests() { { let _lock = holding("the write lock"); { let _file = holding("the served file"); assert_eq!(held(), Some("the served file")); } assert_eq!(held(), Some("the write lock")); } assert_eq!(held(), None); must_not_wait("a child process"); } #[test] fn a_panicking_hold_still_unmarks_the_thread() { let _ = std::panic::catch_unwind(|| { let _file = holding("the served file"); panic!("the work inside the hold failed"); }); assert_eq!(held(), None); } #[test] fn the_mark_is_this_threads_alone() { let _file = holding("the served file"); std::thread::spawn(|| must_not_wait("an outbound HTTP call")) .join() .expect("another thread holds nothing"); } #[test] fn a_tally_counts_what_ran_inside_it_and_nothing_outside() { doing("before any tally"); folded(9); let outer = tally(); doing("the MCP tool erase_photos"); doing("the MCP tool erase_photos"); folded(3); let inner = tally(); doing("a pushed batch"); folded(40); assert_eq!( inner.done(), Work { doing: vec!["a pushed batch".to_string()], facts: 40, folds: 1, over: Vec::new() } ); // The inner hold's work is the outer hold's too. assert_eq!( outer.done(), Work { doing: vec!["the MCP tool erase_photos".to_string(), "a pushed batch".to_string()], facts: 43, folds: 2, over: Vec::new(), } ); // A tally dropped without being read (a panic inside the hold) leaves // nothing behind for the next hold on this thread. { let _dropped = tally(); folded(7); } assert_eq!(tally().done(), Work::default()); } /// RULE: a hold of a file that lasts SLOW_HOLD (5 s) or more writes one line naming what ran inside it and how many facts it folded, and a shorter hold writes none — a stall is diagnosed from the log, never from a stack sample of the owner's app. #[test] fn a_slow_hold_says_who_asked_and_how_much_it_folded() { use std::time::Duration; let work = Work { doing: vec!["the MCP tool erase_photos".to_string(), "a write (bin by user:u1)".to_string()], facts: 412, folds: 1, over: Vec::new(), }; let at = std::panic::Location::caller(); assert_eq!(SLOW_HOLD, Duration::from_secs(5)); let asked = |steps| Asked { steps, ..Asked::default() }; assert_eq!(slow_hold_line("ws-1", Duration::from_millis(4_999), SLOW_HOLD, &work, &asked(9), at), None); let line = slow_hold_line("ws-1", Duration::from_secs(612), SLOW_HOLD, &work, &asked(7_000_000_000), at).expect("a line"); assert!(line.contains("ws-1"), "{line}"); assert!(line.contains("612.0 s"), "{line}"); assert!(line.contains("the MCP tool erase_photos"), "{line}"); assert!(line.contains("412 fact(s) folded in 1 fold(s), 7000000000 steps read"), "{line}"); // Where the time went: a hold that waited inside SQLite says for how long // and in which statement — few steps and a long wait is a lock or the disk. let waited = Asked { steps: 683_907, inside: Duration::from_millis(16_200), slowest: Some((Duration::from_millis(16_100), "BEGIN IMMEDIATE".to_string())) }; let line = slow_hold_line("ws-1", Duration::from_millis(16_800), SLOW_HOLD, &work, &waited, at).expect("a line"); assert!(line.contains("683907 steps read, 16.2 s inside SQLite, its slowest statement 16.1 s: BEGIN IMMEDIATE (asked at"), "{line}"); assert!(!line.contains('\n'), "one line: {line}"); // A hold that named nothing still says how long and where it was asked. let line = slow_hold_line("ws-1", SLOW_HOLD, SLOW_HOLD, &Work::default(), &asked(0), at).expect("a line"); assert!(line.contains("nothing that named itself"), "{line}"); assert!(line.contains("held_tests.rs"), "{line}"); } /// RULE: an act of a job, and an act that erases, reads at most ACT_STEPS of the file — in a debug build one that reads more panics, naming itself, so no test can pass through it; one that is only slow is said by the hold and never panicked on. /// 10 and 11 Oct 2026: two worker acts each read a whole table once per fact, and held the owner's file for more than ten minutes. #[test] fn an_act_within_its_bound_says_nothing_and_a_slow_one_is_said_by_the_hold() { use std::time::Duration; assert_eq!(act_over_line("a write (write by job:repo-prune)", 2_000, ACT_STEPS, ACT_TIME), None); let line = act_over_line("a write (write by job:repo-prune)", 2_000, ACT_STEPS + 1, Duration::from_millis(10)).expect("a line"); assert!(line.contains("a write (write by job:repo-prune)"), "{line}"); assert!(line.contains("2000 fact(s)"), "{line}"); assert!(!line.contains('\n'), "one line: {line}"); // Slow, and within its steps: the hold carries the line, nothing panics. let hold = tally(); act_done("a write (write by job:slow)", 3, 10, ACT_TIME + Duration::from_millis(1)); act_done("a write (write by job:quick)", 3, 10, Duration::from_millis(1)); let work = hold.done(); assert_eq!(work.over.len(), 1, "{:?}", work.over); assert!(work.over[0].contains("job:slow"), "{:?}", work.over); } #[test] #[cfg(debug_assertions)] #[should_panic(expected = "asked too much of the file in one act")] fn an_act_that_reads_past_its_bound_fails_the_test_that_reaches_it() { act_done("a write (write by job:repo-prune)", 2_000, ACT_STEPS + 1, std::time::Duration::from_millis(10)); }