oxedyne/fe2o3/fe2o3_file/tests/quiet.rs
9.8 KiB, 3 runs
created by r1870400018:63097, 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 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 2 | //! Anthropic Claude |
| 3 | |
| 4 | use oxedyne_fe2o3_file::quiet::{ |
| 5 | Quiet, |
| 6 | Stillness, |
| 7 | }; |
| 8 | |
| 9 | use oxedyne_fe2o3_core::{ |
| 10 | prelude::*, |
| 11 | test::test_it, |
| 12 | }; |
| 13 | |
| 14 | use std::time::{ |
| 15 | Duration, |
| 16 | Instant, |
| 17 | }; |
| 18 | |
| 19 | |
| 20 | pub fn test_quiet(filter: &'static str) -> Outcome<()> { |
| 21 | |
| 22 | res!(test_it(filter, &["The first poll is always a move 000", "all", "quiet"], || { |
| 23 | let mut q: Quiet<u64> = Quiet::new(Duration::from_secs(15)); |
| 24 | let t0 = Instant::now(); |
| 25 | assert_eq!(q.poll(1, t0, Duration::from_secs(5)), Stillness::Moved); |
| 26 | assert_eq!(q.reading(), Some(&1)); |
| 27 | Ok(()) |
| 28 | })); |
| 29 | |
| 30 | res!(test_it(filter, &["An unchanged reading settles then reports still 000", "all", "quiet"], || { |
| 31 | let quiet = Duration::from_secs(15); |
| 32 | let poll = Duration::from_secs(5); |
| 33 | let mut q: Quiet<u64> = Quiet::new(quiet); |
| 34 | let t0 = Instant::now(); |
| 35 | assert_eq!(q.poll(7, t0, poll), Stillness::Moved); |
| 36 | // Two more polls, unchanged, still short of the 15s window. |
| 37 | match q.poll(7, t0 + Duration::from_secs(5), poll) { |
| 38 | Stillness::Settling(left) => assert_eq!(left, Duration::from_secs(10)), |
| 39 | other => return Err(err!("Expected Settling(10s), got {:?}.", other; Test)), |
| 40 | } |
| 41 | match q.poll(7, t0 + Duration::from_secs(10), poll) { |
| 42 | Stillness::Settling(left) => assert_eq!(left, Duration::from_secs(5)), |
| 43 | other => return Err(err!("Expected Settling(5s), got {:?}.", other; Test)), |
| 44 | } |
| 45 | // Exactly at the window: still. |
| 46 | assert_eq!(q.poll(7, t0 + Duration::from_secs(15), poll), Stillness::Still); |
| 47 | // And it stays still while nothing changes, polled at the ordinary cadence. |
| 48 | assert_eq!(q.poll(7, t0 + Duration::from_secs(20), poll), Stillness::Still); |
| 49 | Ok(()) |
| 50 | })); |
| 51 | |
| 52 | res!(test_it(filter, &["A change resets the window 000", "all", "quiet"], || { |
| 53 | let quiet = Duration::from_secs(15); |
| 54 | let poll = Duration::from_secs(5); |
| 55 | let mut q: Quiet<u64> = Quiet::new(quiet); |
| 56 | let t0 = Instant::now(); |
| 57 | assert_eq!(q.poll(1, t0, poll), Stillness::Moved); |
| 58 | assert_eq!(q.poll(1, t0 + Duration::from_secs(15), poll), Stillness::Still); |
| 59 | // The reading changes: back to Moved, and the window starts over. |
| 60 | assert_eq!(q.poll(2, t0 + Duration::from_secs(16), poll), Stillness::Moved); |
| 61 | assert_eq!(q.poll(2, t0 + Duration::from_secs(20), poll), Stillness::Settling(Duration::from_secs(11))); |
| 62 | assert_eq!(q.poll(2, t0 + Duration::from_secs(31), poll), Stillness::Still); |
| 63 | Ok(()) |
| 64 | })); |
| 65 | |
| 66 | // The control for the bug `ore-quiet-mark`'s bash loop had: "last seen" was stamped on only |
| 67 | // some code paths, so ten of the loop's own regular polls could look like a stalled gap and |
| 68 | // restart the window. `Quiet::poll` stamps it unconditionally, on every call. |
| 69 | res!(test_it(filter, &["Ten regular polls never trip the gap guard 000", "all", "quiet"], || { |
| 70 | let quiet = Duration::from_secs(15); |
| 71 | let poll = Duration::from_secs(5); |
| 72 | let mut q: Quiet<u64> = Quiet::new(quiet); // default gap_factor = 3, so the guard is 15s. |
| 73 | let t0 = Instant::now(); |
| 74 | assert_eq!(q.poll(9, t0, poll), Stillness::Moved); |
| 75 | for i in 1..=10u64 { |
| 76 | let now = t0 + poll * (i as u32); |
| 77 | let got = q.poll(9, now, poll); |
| 78 | assert!( |
| 79 | !matches!(got, Stillness::Restarted), |
| 80 | "poll {} restarted on an ordinary 5s cadence: {:?}", i, got, |
| 81 | ); |
| 82 | } |
| 83 | // 10 polls at 5s = 50s, well past the 15s window: it must have gone, and stayed, still. |
| 84 | assert_eq!(q.poll(9, t0 + poll * 11, poll), Stillness::Still); |
| 85 | Ok(()) |
| 86 | })); |
| 87 | |
| 88 | res!(test_it(filter, &["A gap past the guard restarts the window 000", "all", "quiet"], || { |
| 89 | let quiet = Duration::from_secs(15); |
| 90 | let poll = Duration::from_secs(5); |
| 91 | let mut q: Quiet<u64> = Quiet::new(quiet); // guard = 3 * 5s = 15s. |
| 92 | let t0 = Instant::now(); |
| 93 | assert_eq!(q.poll(3, t0, poll), Stillness::Moved); |
| 94 | // A gap of exactly the guard is not "more than" it: no restart, reading unchanged. |
| 95 | match q.poll(3, t0 + Duration::from_secs(15), poll) { |
| 96 | Stillness::Still => {}, |
| 97 | other => return Err(err!("A gap equal to the guard must not restart, got {:?}.", other; Test)), |
| 98 | } |
| 99 | // Now a stall: the process is stopped for a minute between two polls. Even though the |
| 100 | // reading never changed, nothing was watching, and waking is not a minute of stillness. |
| 101 | assert_eq!( |
| 102 | q.poll(3, t0 + Duration::from_secs(15) + Duration::from_secs(61), poll), |
| 103 | Stillness::Restarted, |
| 104 | ); |
| 105 | // Immediately after the restart the window has begun again, not finished. |
| 106 | match q.poll(3, t0 + Duration::from_secs(15) + Duration::from_secs(62), poll) { |
| 107 | Stillness::Settling(_) => {}, |
| 108 | other => return Err(err!("Expected Settling just after a restart, got {:?}.", other; Test)), |
| 109 | } |
| 110 | Ok(()) |
| 111 | })); |
| 112 | |
| 113 | // A real stall, from outside: a child shell stops this whole process with SIGSTOP for 600 ms |
| 114 | // and starts it again, and the guard must see the gap on `Instant`'s own clock. A suspend is |
| 115 | // not this. `Instant` does not run while the machine sleeps, so nothing here can show one. |
| 116 | #[cfg(target_os = "linux")] |
| 117 | res!(test_it(filter, &["A real SIGSTOP between two polls restarts the window 000", "all", "quiet"], || { |
| 118 | let stall = Duration::from_millis(600); |
| 119 | let poll = Duration::from_millis(20); |
| 120 | let mut q: Quiet<u64> = Quiet::new(Duration::from_secs(60)); // guard = 3 * 20ms = 60ms. |
| 121 | let mut prev = Instant::now(); |
| 122 | assert_eq!(q.poll(4, prev, poll), Stillness::Moved); |
| 123 | let mut child = res!(std::process::Command::new("sh") |
| 124 | .args(["-c", "kill -STOP $PPID; sleep 0.6; kill -CONT $PPID"]) |
| 125 | .spawn()); |
| 126 | let began = Instant::now(); |
| 127 | let mut seen = false; |
| 128 | while !seen && began.elapsed() < Duration::from_secs(10) { |
| 129 | std::thread::sleep(poll); |
| 130 | let now = Instant::now(); |
| 131 | let got = q.poll(4, now, poll); |
| 132 | // A scheduling hiccup on a loaded machine can restart the window by itself. Only a |
| 133 | // restart after a gap as long as the stall is the stop being seen. |
| 134 | if got == Stillness::Restarted && now.duration_since(prev) >= stall - Duration::from_millis(100) { |
| 135 | seen = true; |
| 136 | } |
| 137 | prev = now; |
| 138 | } |
| 139 | let status = res!(child.wait()); |
| 140 | assert!(status.success(), "the stopping child failed: {:?}", status); |
| 141 | assert!(seen, "the guard never saw a gap of the stall's length after a real SIGSTOP"); |
| 142 | // And the window really did start again at the restart, not finish. |
| 143 | match q.poll(4, Instant::now(), poll) { |
| 144 | Stillness::Settling(_) => {}, |
| 145 | other => return Err(err!("Expected Settling just after the stall, got {:?}.", other; Test)), |
| 146 | } |
| 147 | Ok(()) |
| 148 | })); |
| 149 | |
| 150 | res!(test_it(filter, &["The gap factor is configurable 000", "all", "quiet"], || { |
| 151 | let mut q: Quiet<u64> = Quiet::with_gap_factor(Duration::from_secs(15), 10); |
| 152 | let poll = Duration::from_secs(1); |
| 153 | let t0 = Instant::now(); |
| 154 | assert_eq!(q.poll(1, t0, poll), Stillness::Moved); |
| 155 | // 9s gap: under a 10x guard on a 1s poll, so no restart. |
| 156 | match q.poll(1, t0 + Duration::from_secs(9), poll) { |
| 157 | Stillness::Restarted => return Err(err!("A 9s gap under a 10s guard restarted."; Test)), |
| 158 | _ => {}, |
| 159 | } |
| 160 | // 11s gap: over the guard. |
| 161 | assert_eq!(q.poll(1, t0 + Duration::from_secs(20), poll), Stillness::Restarted); |
| 162 | Ok(()) |
| 163 | })); |
| 164 | |
| 165 | // What a caller does on noticing the machine slept: it has no reading it can trust, and the |
| 166 | // time spent still is not time anybody watched. |
| 167 | res!(test_it(filter, &["A restart drops the reading and starts the window over 000", "all", "quiet"], || { |
| 168 | let quiet = Duration::from_secs(15); |
| 169 | let poll = Duration::from_secs(5); |
| 170 | let mut q: Quiet<u64> = Quiet::new(quiet); |
| 171 | let t0 = Instant::now(); |
| 172 | assert_eq!(q.poll(5, t0, poll), Stillness::Moved); |
| 173 | assert_eq!(q.poll(5, t0 + quiet, poll), Stillness::Still); |
| 174 | q.restart(); |
| 175 | assert_eq!(q.reading(), None); |
| 176 | // The same reading is no longer still: it begins its window afresh, as a first poll does. |
| 177 | assert_eq!(q.poll(5, t0 + quiet + poll, poll), Stillness::Moved); |
| 178 | assert_eq!(q.reading(), Some(&5)); |
| 179 | assert_eq!(q.poll(5, t0 + quiet + poll * 2, poll), Stillness::Settling(quiet - poll)); |
| 180 | assert_eq!(q.poll(5, t0 + quiet + poll + quiet, poll), Stillness::Still); |
| 181 | Ok(()) |
| 182 | })); |
| 183 | |
| 184 | res!(test_it(filter, &["A restart is not a gap and keeps the configuration 000", "all", "quiet"], || { |
| 185 | let mut q: Quiet<u64> = Quiet::with_gap_factor(Duration::from_secs(15), 10); |
| 186 | let poll = Duration::from_secs(1); |
| 187 | let t0 = Instant::now(); |
| 188 | assert_eq!(q.poll(1, t0, poll), Stillness::Moved); |
| 189 | q.restart(); |
| 190 | // An hour later, by the guard's own arithmetic a very long gap, but the restart took the |
| 191 | // memory of the last poll with it: this is a first poll, not a restart reported twice. |
| 192 | let t1 = t0 + Duration::from_secs(3_600); |
| 193 | assert_eq!(q.poll(1, t1, poll), Stillness::Moved); |
| 194 | // The window and the gap factor are those given at construction. |
| 195 | assert_eq!(q.poll(1, t1 + Duration::from_secs(9), poll), Stillness::Settling(Duration::from_secs(6))); |
| 196 | assert_eq!(q.poll(1, t1 + Duration::from_secs(15), poll), Stillness::Still); |
| 197 | assert_eq!(q.poll(1, t1 + Duration::from_secs(40), poll), Stillness::Restarted); |
| 198 | Ok(()) |
| 199 | })); |
| 200 | |
| 201 | Ok(()) |
| 202 | } |