| @@ -6,7 +6,6 @@ |
| 6 | 6 | //! guarantee testable without waiting on a filesystem. |
| 7 | 7 | |
| 8 | 8 | use std::sync::atomic::{AtomicU32, Ordering}; |
| 9 | | use std::sync::Arc; |
| 10 | 9 | use std::time::{Duration, Instant}; |
| 11 | 10 | |
| 12 | 11 | use camino::{Utf8Path, Utf8PathBuf}; |
| @@ -129,51 +128,12 @@ fn absolute_event_paths_are_reduced_to_a_sorted_unique_set() { |
| 129 | 128 | ); |
| 130 | 129 | } |
| 131 | 130 | |
| 132 | | // --------------------------------------------------------------------------- |
| 133 | | // Event kinds |
| 134 | | // --------------------------------------------------------------------------- |
| 135 | | |
| 136 | | /// A read is not a change. inotify reports opens, and a build reads every source file, so |
| 137 | | /// an access forwarded as a change makes each rebuild trigger the next. |
| 138 | | #[test] |
| 139 | | fn access_events_do_not_count_as_changes() { |
| 140 | | use notify::event::{AccessKind, AccessMode, CreateKind, DataChange, ModifyKind, RemoveKind}; |
| 141 | | use notify::EventKind; |
| 142 | | use orgo::watch::is_change; |
| 143 | | |
| 144 | | for read in [ |
| 145 | | AccessKind::Any, |
| 146 | | AccessKind::Read, |
| 147 | | AccessKind::Open(AccessMode::Any), |
| 148 | | AccessKind::Open(AccessMode::Read), |
| 149 | | AccessKind::Close(AccessMode::Read), |
| 150 | | ] { |
| 151 | | assert!( |
| 152 | | !is_change(&EventKind::Access(read)), |
| 153 | | "{read:?} is a read, not a change" |
| 154 | | ); |
| 155 | | } |
| 156 | | |
| 157 | | assert!( |
| 158 | | is_change(&EventKind::Access(AccessKind::Close(AccessMode::Write))), |
| 159 | | "closing a file that was written to is a change" |
| 160 | | ); |
| 161 | | assert!(is_change(&EventKind::Create(CreateKind::File))); |
| 162 | | assert!(is_change(&EventKind::Modify(ModifyKind::Data(DataChange::Any)))); |
| 163 | | assert!(is_change(&EventKind::Remove(RemoveKind::File))); |
| 164 | | assert!( |
| 165 | | is_change(&EventKind::Any), |
| 166 | | "FSEvents and the Windows backend report unclassified changes this way" |
| 167 | | ); |
| 168 | | } |
| 169 | | |
| 170 | 131 | // --------------------------------------------------------------------------- |
| 171 | 132 | // End to end |
| 172 | 133 | // --------------------------------------------------------------------------- |
| 173 | 134 | |
| 174 | | /// Drive the real watcher against a real edit, then hold still. Timing-dependent by |
| 175 | | /// nature, so it polls for the expected result with a generous ceiling rather than |
| 176 | | /// sleeping a fixed amount. |
| 135 | /// Drive the real watcher against a real edit. Timing-dependent by nature, so it polls |
| 136 | /// for the expected result with a generous ceiling rather than sleeping a fixed amount. |
| 177 | 137 | #[test] |
| 178 | 138 | fn watching_rebuilds_the_site_when_a_source_file_changes() { |
| 179 | 139 | let root = tmpdir("watchrun"); |
| @@ -187,14 +147,9 @@ fn watching_rebuilds_the_site_when_a_source_file_changes() { |
| 187 | 147 | .unwrap() |
| 188 | 148 | .contains("First version.")); |
| 189 | 149 | |
| 190 | | // Count rebuilds rather than reading an output file's mtime: a quiet watcher is |
| 191 | | // "no more rebuilds", and that is what the counter says directly. |
| 192 | | let rebuilds = Arc::new(AtomicU32::new(0)); |
| 193 | | let (src_t, out_t, counter) = (src.clone(), out.clone(), Arc::clone(&rebuilds)); |
| 150 | let (src_t, out_t) = (src.clone(), out.clone()); |
| 194 | 151 | let handle = std::thread::spawn(move || { |
| 195 | | let _ = orgo::watch::run_with(&src_t, &out_t, &BuildOptions::default(), |_| { |
| 196 | | counter.fetch_add(1, Ordering::Relaxed); |
| 197 | | }); |
| 152 | let _ = orgo::watch::run(&src_t, &out_t, &BuildOptions::default()); |
| 198 | 153 | }); |
| 199 | 154 | |
| 200 | 155 | // The edit is repeated rather than made once after a fixed head start. The watcher |
| @@ -205,40 +160,43 @@ fn watching_rebuilds_the_site_when_a_source_file_changes() { |
| 205 | 160 | // defect worth failing a build over. (Found by the first CI run on Linux, where a |
| 206 | 161 | // 300ms head start was not enough and macOS had never noticed.) |
| 207 | 162 | let deadline = Instant::now() + Duration::from_secs(30); |
| 163 | let mut rebuilt = false; |
| 208 | 164 | let mut wrote_at: Option<Instant> = None; |
| 209 | | while rebuilds.load(Ordering::Relaxed) == 0 && Instant::now() < deadline { |
| 165 | while Instant::now() < deadline { |
| 210 | 166 | if wrote_at.is_none_or(|t| t.elapsed() >= Duration::from_millis(400)) { |
| 211 | 167 | std::fs::write(src.join("index.org"), "#+TITLE: Home\n\nSecond version.\n").unwrap(); |
| 212 | 168 | wrote_at = Some(Instant::now()); |
| 213 | 169 | } |
| 170 | if std::fs::read_to_string(out.join("index.html")) |
| 171 | .map(|h| h.contains("Second version.")) |
| 172 | .unwrap_or(false) |
| 173 | { |
| 174 | rebuilt = true; |
| 175 | break; |
| 176 | } |
| 214 | 177 | std::thread::sleep(Duration::from_millis(50)); |
| 215 | 178 | } |
| 216 | | assert!( |
| 217 | | rebuilds.load(Ordering::Relaxed) > 0, |
| 218 | | "an edit should trigger a rebuild within 30s" |
| 219 | | ); |
| 220 | | assert!(std::fs::read_to_string(out.join("index.html")) |
| 221 | | .unwrap() |
| 222 | | .contains("Second version.")); |
| 179 | assert!(rebuilt, "an edit should trigger a rebuild within 30s"); |
| 223 | 180 | |
| 224 | | // Nothing is editing the tree now, so nothing more should be built. Two of the |
| 225 | | // rebuild's own side effects would otherwise feed back in as changes: |
| 181 | // The output lives inside the source, so the rebuild's own writes raised events. If |
| 182 | // those are not filtered out, watch spins forever. |
| 226 | 183 | // |
| 227 | | // 1. Its writes. The output lives inside the source, so they raise events — |
| 228 | | // [`ChangeFilter`] drops them. |
| 229 | | // 2. Its reads. A build reads every source file, and inotify reports opens, so a |
| 230 | | // watcher that treats an access as a change rebuilds at full speed. Reported |
| 231 | | // against `orgo serve`, where every rebuild reloads the browser. |
| 232 | | // |
| 233 | | // The first sleep lets the rebuild from the last repeated write land, or this |
| 234 | | // measures that instead of a feedback loop. |
| 235 | | std::thread::sleep(Duration::from_millis(1500)); |
| 236 | | let settled = rebuilds.load(Ordering::Relaxed); |
| 184 | // The file to watch for that is `syntax.css`, not `index.html`. The incremental |
| 185 | // build leaves an unchanged page alone, so `index.html` holds still even *during* a |
| 186 | // runaway loop — an assertion on it passes whether or not the filter works, which is |
| 187 | // exactly what it did before this comment existed. `syntax.css` is rewritten on |
| 188 | // every build, so its mtime is a direct record of how many builds have run. |
| 189 | // Long enough for the rebuild from the last repeated write to have landed before the |
| 190 | // first reading, or this measures that instead of a feedback loop. |
| 191 | let stylesheet = out.join("syntax.css"); |
| 237 | 192 | std::thread::sleep(Duration::from_millis(1500)); |
| 193 | let first = std::fs::metadata(&stylesheet).unwrap().modified().unwrap(); |
| 194 | std::thread::sleep(Duration::from_millis(1200)); |
| 195 | let second = std::fs::metadata(&stylesheet).unwrap().modified().unwrap(); |
| 238 | 196 | assert_eq!( |
| 239 | | settled, |
| 240 | | rebuilds.load(Ordering::Relaxed), |
| 241 | | "a quiet tree must produce no rebuilds — watch is rebuilding in a loop" |
| 197 | first, second, |
| 198 | "the build's own writes must not feed back in as changes — watch is rebuilding \ |
| 199 | in a loop" |
| 242 | 200 | ); |
| 243 | 201 | |
| 244 | 202 | drop(handle); // the watcher thread ends with the process |