| @@ -6,7 +6,6 @@ |
| 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; |
| |
| 10 | use std::time::{Duration, Instant}; |
9 | use std::time::{Duration, Instant}; |
| 11 | |
10 | |
| 12 | use camino::{Utf8Path, Utf8PathBuf}; |
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 | // End to end |
132 | // End to end |
| 172 | // --------------------------------------------------------------------------- |
133 | // --------------------------------------------------------------------------- |
| 173 | |
134 | |
| 174 | /// Drive the real watcher against a real edit, then hold still. Timing-dependent by |
135 | /// Drive the real watcher against a real edit. Timing-dependent by nature, so it polls |
| 175 | /// nature, so it polls for the expected result with a generous ceiling rather than |
136 | /// for the expected result with a generous ceiling rather than sleeping a fixed amount. |
| 176 | /// sleeping a fixed amount. |
| |
| 177 | #[test] |
137 | #[test] |
| 178 | fn watching_rebuilds_the_site_when_a_source_file_changes() { |
138 | fn watching_rebuilds_the_site_when_a_source_file_changes() { |
| 179 | let root = tmpdir("watchrun"); |
139 | let root = tmpdir("watchrun"); |
| @@ -187,14 +147,9 @@ fn watching_rebuilds_the_site_when_a_source_file_changes() { |
| 187 | .unwrap() |
147 | .unwrap() |
| 188 | .contains("First version.")); |
148 | .contains("First version.")); |
| 189 | |
149 | |
| 190 | // Count rebuilds rather than reading an output file's mtime: a quiet watcher is |
150 | let (src_t, out_t) = (src.clone(), out.clone()); |
| 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)); |
| |
| 194 | let handle = std::thread::spawn(move || { |
151 | let handle = std::thread::spawn(move || { |
| 195 | let _ = orgo::watch::run_with(&src_t, &out_t, &BuildOptions::default(), |_| { |
152 | let _ = orgo::watch::run(&src_t, &out_t, &BuildOptions::default()); |
| 196 | counter.fetch_add(1, Ordering::Relaxed); |
| |
| 197 | }); |
| |
| 198 | }); |
153 | }); |
| 199 | |
154 | |
| 200 | // The edit is repeated rather than made once after a fixed head start. The watcher |
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 | // defect worth failing a build over. (Found by the first CI run on Linux, where a |
160 | // defect worth failing a build over. (Found by the first CI run on Linux, where a |
| 206 | // 300ms head start was not enough and macOS had never noticed.) |
161 | // 300ms head start was not enough and macOS had never noticed.) |
| 207 | let deadline = Instant::now() + Duration::from_secs(30); |
162 | let deadline = Instant::now() + Duration::from_secs(30); |
| |
163 | let mut rebuilt = false; |
| 208 | let mut wrote_at: Option<Instant> = None; |
164 | let mut wrote_at: Option<Instant> = None; |
| 209 | while rebuilds.load(Ordering::Relaxed) == 0 && Instant::now() < deadline { |
165 | while Instant::now() < deadline { |
| 210 | if wrote_at.is_none_or(|t| t.elapsed() >= Duration::from_millis(400)) { |
166 | if wrote_at.is_none_or(|t| t.elapsed() >= Duration::from_millis(400)) { |
| 211 | std::fs::write(src.join("index.org"), "#+TITLE: Home\n\nSecond version.\n").unwrap(); |
167 | std::fs::write(src.join("index.org"), "#+TITLE: Home\n\nSecond version.\n").unwrap(); |
| 212 | wrote_at = Some(Instant::now()); |
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 | std::thread::sleep(Duration::from_millis(50)); |
177 | std::thread::sleep(Duration::from_millis(50)); |
| 215 | } |
178 | } |
| 216 | assert!( |
179 | assert!(rebuilt, "an edit should trigger a rebuild within 30s"); |
| 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.")); |
| |
| 223 | |
180 | |
| 224 | // Nothing is editing the tree now, so nothing more should be built. Two of the |
181 | // The output lives inside the source, so the rebuild's own writes raised events. If |
| 225 | // rebuild's own side effects would otherwise feed back in as changes: |
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 — |
184 | // The file to watch for that is `syntax.css`, not `index.html`. The incremental |
| 228 | // [`ChangeFilter`] drops them. |
185 | // build leaves an unchanged page alone, so `index.html` holds still even *during* a |
| 229 | // 2. Its reads. A build reads every source file, and inotify reports opens, so a |
186 | // runaway loop — an assertion on it passes whether or not the filter works, which is |
| 230 | // watcher that treats an access as a change rebuilds at full speed. Reported |
187 | // exactly what it did before this comment existed. `syntax.css` is rewritten on |
| 231 | // against `orgo serve`, where every rebuild reloads the browser. |
188 | // every build, so its mtime is a direct record of how many builds have run. |
| 232 | // |
189 | // Long enough for the rebuild from the last repeated write to have landed before the |
| 233 | // The first sleep lets the rebuild from the last repeated write land, or this |
190 | // first reading, or this measures that instead of a feedback loop. |
| 234 | // measures that instead of a feedback loop. |
191 | let stylesheet = out.join("syntax.css"); |
| 235 | std::thread::sleep(Duration::from_millis(1500)); |
| |
| 236 | let settled = rebuilds.load(Ordering::Relaxed); |
| |
| 237 | std::thread::sleep(Duration::from_millis(1500)); |
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 | assert_eq!( |
196 | assert_eq!( |
| 239 | settled, |
197 | first, second, |
| 240 | rebuilds.load(Ordering::Relaxed), |
198 | "the build's own writes must not feed back in as changes — watch is rebuilding \ |
| 241 | "a quiet tree must produce no rebuilds — watch is rebuilding in a loop" |
199 | in a loop" |
| 242 | ); |
200 | ); |
| 243 | |
201 | |
| 244 | drop(handle); // the watcher thread ends with the process |
202 | drop(handle); // the watcher thread ends with the process |