Commit 871cbf2008
Verified · cmc ci/pages: success ci/test: failure
Layout: unified · split
tests/watch.rs +34 −30
| @@ -6,6 +6,7 @@ | |||
| 6 | //! guarantee testable without waiting on a filesystem. | 6 | //! guarantee testable without waiting on a filesystem. |
| 7 | 7 | ||
| 8 | use std::sync::atomic::{AtomicU32, Ordering}; | 8 | use std::sync::atomic::{AtomicU32, Ordering}; |
| 9 | use std::sync::Arc; | ||
| 9 | use std::time::{Duration, Instant}; | 10 | use std::time::{Duration, Instant}; |
| 10 | 11 | ||
| 11 | use camino::{Utf8Path, Utf8PathBuf}; | 12 | use camino::{Utf8Path, Utf8PathBuf}; |
| @@ -132,8 +133,9 @@ fn absolute_event_paths_are_reduced_to_a_sorted_unique_set() { | |||
| 132 | // End to end | 133 | // End to end |
| 133 | // --------------------------------------------------------------------------- | 134 | // --------------------------------------------------------------------------- |
| 134 | 135 | ||
| 135 | /// Drive the real watcher against a real edit. Timing-dependent by nature, so it polls | 136 | /// Drive the real watcher against a real edit, then hold still. Timing-dependent by |
| 136 | /// for the expected result with a generous ceiling rather than sleeping a fixed amount. | 137 | /// nature, so it polls for the expected result with a generous ceiling rather than |
| 138 | /// sleeping a fixed amount. | ||
| 137 | #[test] | 139 | #[test] |
| 138 | fn watching_rebuilds_the_site_when_a_source_file_changes() { | 140 | fn watching_rebuilds_the_site_when_a_source_file_changes() { |
| 139 | let root = tmpdir("watchrun"); | 141 | let root = tmpdir("watchrun"); |
| @@ -147,9 +149,14 @@ fn watching_rebuilds_the_site_when_a_source_file_changes() { | |||
| 147 | .unwrap() | 149 | .unwrap() |
| 148 | .contains("First version.")); | 150 | .contains("First version.")); |
| 149 | 151 | ||
| 150 | let (src_t, out_t) = (src.clone(), out.clone()); | 152 | // Count rebuilds rather than reading an output file's mtime: a quiet watcher is |
| 153 | // "no more rebuilds", and that is what the counter says directly. | ||
| 154 | let rebuilds = Arc::new(AtomicU32::new(0)); | ||
| 155 | let (src_t, out_t, counter) = (src.clone(), out.clone(), Arc::clone(&rebuilds)); | ||
| 151 | let handle = std::thread::spawn(move || { | 156 | let handle = std::thread::spawn(move || { |
| 152 | let _ = orgo::watch::run(&src_t, &out_t, &BuildOptions::default()); | 157 | let _ = orgo::watch::run_with(&src_t, &out_t, &BuildOptions::default(), |_| { |
| 158 | counter.fetch_add(1, Ordering::Relaxed); | ||
| 159 | }); | ||
| 153 | }); | 160 | }); |
| 154 | 161 | ||
| 155 | // The edit is repeated rather than made once after a fixed head start. The watcher | 162 | // The edit is repeated rather than made once after a fixed head start. The watcher |
| @@ -160,43 +167,40 @@ fn watching_rebuilds_the_site_when_a_source_file_changes() { | |||
| 160 | // defect worth failing a build over. (Found by the first CI run on Linux, where a | 167 | // defect worth failing a build over. (Found by the first CI run on Linux, where a |
| 161 | // 300ms head start was not enough and macOS had never noticed.) | 168 | // 300ms head start was not enough and macOS had never noticed.) |
| 162 | let deadline = Instant::now() + Duration::from_secs(30); | 169 | let deadline = Instant::now() + Duration::from_secs(30); |
| 163 | let mut rebuilt = false; | ||
| 164 | let mut wrote_at: Option<Instant> = None; | 170 | let mut wrote_at: Option<Instant> = None; |
| 165 | while Instant::now() < deadline { | 171 | while rebuilds.load(Ordering::Relaxed) == 0 && Instant::now() < deadline { |
| 166 | if wrote_at.is_none_or(|t| t.elapsed() >= Duration::from_millis(400)) { | 172 | if wrote_at.is_none_or(|t| t.elapsed() >= Duration::from_millis(400)) { |
| 167 | std::fs::write(src.join("index.org"), "#+TITLE: Home\n\nSecond version.\n").unwrap(); | 173 | std::fs::write(src.join("index.org"), "#+TITLE: Home\n\nSecond version.\n").unwrap(); |
| 168 | wrote_at = Some(Instant::now()); | 174 | wrote_at = Some(Instant::now()); |
| 169 | } | 175 | } |
| 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 | } | ||
| 177 | std::thread::sleep(Duration::from_millis(50)); | 176 | std::thread::sleep(Duration::from_millis(50)); |
| 178 | } | 177 | } |
| 179 | assert!(rebuilt, "an edit should trigger a rebuild within 30s"); | 178 | assert!( |
| 179 | rebuilds.load(Ordering::Relaxed) > 0, | ||
| 180 | "an edit should trigger a rebuild within 30s" | ||
| 181 | ); | ||
| 182 | assert!(std::fs::read_to_string(out.join("index.html")) | ||
| 183 | .unwrap() | ||
| 184 | .contains("Second version.")); | ||
| 180 | 185 | ||
| 181 | // The output lives inside the source, so the rebuild's own writes raised events. If | 186 | // Nothing is editing the tree now, so nothing more should be built. Two of the |
| 182 | // those are not filtered out, watch spins forever. | 187 | // rebuild's own side effects would otherwise feed back in as changes: |
| 188 | // | ||
| 189 | // 1. Its writes. The output lives inside the source, so they raise events — | ||
| 190 | // [`ChangeFilter`] drops them. | ||
| 191 | // 2. Its reads. A build reads every source file, and inotify reports opens, so a | ||
| 192 | // watcher that treats an access as a change rebuilds at full speed. Reported | ||
| 193 | // against `orgo serve`, where every rebuild reloads the browser. | ||
| 183 | // | 194 | // |
| 184 | // The file to watch for that is `syntax.css`, not `index.html`. The incremental | 195 | // The first sleep lets the rebuild from the last repeated write land, or this |
| 185 | // build leaves an unchanged page alone, so `index.html` holds still even *during* a | 196 | // measures that instead of a feedback loop. |
| 186 | // runaway loop — an assertion on it passes whether or not the filter works, which is | 197 | std::thread::sleep(Duration::from_millis(1500)); |
| 187 | // exactly what it did before this comment existed. `syntax.css` is rewritten on | 198 | let settled = rebuilds.load(Ordering::Relaxed); |
| 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"); | ||
| 192 | std::thread::sleep(Duration::from_millis(1500)); | 199 | 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(); | ||
| 196 | assert_eq!( | 200 | assert_eq!( |
| 197 | first, second, | 201 | settled, |
| 198 | "the build's own writes must not feed back in as changes — watch is rebuilding \ | 202 | rebuilds.load(Ordering::Relaxed), |
| 199 | in a loop" | 203 | "a quiet tree must produce no rebuilds — watch is rebuilding in a loop" |
| 200 | ); | 204 | ); |
| 201 | 205 | ||
| 202 | drop(handle); // the watcher thread ends with the process | 206 | drop(handle); // the watcher thread ends with the process |