From 0a9d0ad95091b228f4d30d55077db4aceec932fd Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Tue, 1 Dec 2026 02:36:05 -0800 Subject: [PATCH] Tests: hold the font scan in a lister so the menu list test runs on the new craft-libs The craft-libs revision this branch moves to skips any font file that is not a regular file (craft-libs adb8c1c2, font-catalog `Catalog::rescan`), so the scan never opened the named pipe that `the_menu_family_list_now_does_not_wait_for_the_scan` used to hold it. The test's release thread then blocked opening the pipe for writing with no reader, and gate shard 5/6 ran the test for over two hours before the job was canceled. The test now holds the scan in a file lister added by a test-only constructor, `FontDb::with_font_dirs_and_scan_hook`. The lister reports that the scan started and waits until the lookups under test return. A lookup that waits for the scan makes the lister give up after 120 s and the test fails on that record instead of hanging. The test no longer needs `mkfifo`, so it runs on every platform. Its assertions are unchanged. --- crates/text/src/fontdb/db.rs | 14 ++++++++++++ crates/text/src/tests/sysfonts.rs | 38 ++++++++++++++++++------------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/crates/text/src/fontdb/db.rs b/crates/text/src/fontdb/db.rs index f506de34c5..dfc57e6bf8 100644 --- a/crates/text/src/fontdb/db.rs +++ b/crates/text/src/fontdb/db.rs @@ -34,6 +34,20 @@ impl FontDb { Self::new(catalog) } + /// [`FontDb::with_font_dirs`] whose every scan first calls `hook`, for tests that hold a scan + /// part way. + #[cfg(test)] + pub(crate) fn with_font_dirs_and_scan_hook(font_dirs: Vec, hook: impl Fn() + Send + Sync + 'static) -> Self { + let catalog = Catalog::new(FontEnv { os: Os::None, ..FontEnv::default() }).with_font_service(false); + let (folders, files): (Vec, Vec) = font_dirs.into_iter().partition(|p| p.is_dir()); + let catalog = catalog.with_lister(Arc::new(move || { + hook(); + files.clone() + })); + catalog.set_app_dirs(folders); + Self::new(catalog) + } + /// Same scan paths as [`FontDb::global`] (platform lister and font folders), for tests. #[cfg(test)] pub(crate) fn for_system_scan_test() -> Self { diff --git a/crates/text/src/tests/sysfonts.rs b/crates/text/src/tests/sysfonts.rs index 261cc1c54c..3d3776c7f6 100644 --- a/crates/text/src/tests/sysfonts.rs +++ b/crates/text/src/tests/sysfonts.rs @@ -524,30 +524,36 @@ fn a_background_scan_serves_the_first_lookup() { /// for the scan of the installed fonts: a large library would hold the first frame past the /// first-frame watchdog, and the app would never show its window (#5483). Once the scan ends, the /// list has the installed fonts. -#[cfg(unix)] #[test] fn the_menu_family_list_now_does_not_wait_for_the_scan() { let dir = font_dir("held-scan"); - // A named pipe with a font's name: the scan opens it and waits there until something writes. - let pipe = dir.join("Held.ttf"); - assert!(std::process::Command::new("mkfifo").arg(&pipe).status().unwrap().success()); - let db: &'static FontDb = Box::leak(Box::new(FontDb::with_font_dirs(vec![dir]))); + // The scan reports that it has started, then waits in its file lister until the calls below + // have returned and the sender is dropped. Every later scan proceeds with the sender disconnected. + // The gate CI runs has no test timeout, so a list that waited for the scan would hang its shard: + // after 120 s the lister lets the scan go on and records it, and the test fails on that record. + // A passing run never reaches the bound. + const BOUND: std::time::Duration = std::time::Duration::from_secs(120); + let (started, scanning) = std::sync::mpsc::channel::<()>(); + let (returned, waiting) = std::sync::mpsc::channel::<()>(); + let held = std::sync::Mutex::new((started, waiting)); + let gave_up = Arc::new(std::sync::atomic::AtomicBool::new(false)); + let gave_up_in_scan = gave_up.clone(); + let hold = move || { + if let Ok(held) = held.lock() { + let _ = held.0.send(()); + if let Err(std::sync::mpsc::RecvTimeoutError::Timeout) = held.1.recv_timeout(BOUND) { + gave_up_in_scan.store(true, std::sync::atomic::Ordering::SeqCst); + } + } + }; + let db: &'static FontDb = Box::leak(Box::new(FontDb::with_font_dirs_and_scan_hook(vec![dir], hold))); let generation = db.generation(); db.scan_in_background(); - // The scan can't end until the pipe is opened for writing, so a list that waited for it would - // hold the installed fonts. This thread lets the scan go on once the calls return, or after 10 s - // if they are still waiting, so that such a test fails instead of hanging. - let (returned, waiting) = std::sync::mpsc::channel::<()>(); - let release = pipe.clone(); - let guard = std::thread::spawn(move || { - let _ = waiting.recv_timeout(std::time::Duration::from_secs(10)); - // The pipe is opened and closed with nothing written. - drop(std::fs::OpenOptions::new().write(true).open(&release).unwrap()); - }); + scanning.recv_timeout(BOUND).expect("the background scan started"); let ready = db.installed_fonts_ready(); let listed = db.menu_family_list_now(); drop(returned); - guard.join().unwrap(); + assert!(!gave_up.load(std::sync::atomic::Ordering::SeqCst), "the list waited for the held scan"); assert!(!ready && !has(&listed, FAMILY), "before the scan ends, only the fonts already loaded"); // Waits for the scan. assert!(has(&db.menu_family_list(), FAMILY));