diff --git a/crates/text/src/fontdb/db.rs b/crates/text/src/fontdb/db.rs index f506de34c5..474198cad6 100644 --- a/crates/text/src/fontdb/db.rs +++ b/crates/text/src/fontdb/db.rs @@ -24,14 +24,14 @@ impl FontDb { /// A database holding the bundled fonts, whose system font scan reads `font_dirs` (folders, /// read with their subfolders, and font files) and nothing else. pub fn with_font_dirs(font_dirs: Vec) -> Self { - // No platform folders, font store or font service. - let catalog = Catalog::new(FontEnv { os: Os::None, ..FontEnv::default() }).with_font_service(false); - // A path that isn't a folder is a font file named by itself, read whatever its name (font - // services keep fonts in files without an extension); one that becomes a folder is read as one. - let (folders, files): (Vec, Vec) = font_dirs.into_iter().partition(|p| p.is_dir()); - let catalog = catalog.with_lister(Arc::new(move || files.clone())); - catalog.set_app_dirs(folders); - Self::new(catalog) + Self::new(font_dirs_catalog(font_dirs)) + } + + /// [`FontDb::with_font_dirs`] whose every scan also calls `lister` before it reads a folder, + /// for tests that hold a scan until they let it go on. + #[cfg(test)] + pub(crate) fn with_font_dirs_and_lister(font_dirs: Vec, lister: craft::font_catalog::Lister) -> Self { + Self::new(font_dirs_catalog(font_dirs).with_lister(lister)) } /// Same scan paths as [`FontDb::global`] (platform lister and font folders), for tests. @@ -418,3 +418,15 @@ impl FontDb { false } } + +/// [`FontDb::with_font_dirs`]'s catalog: it reads `font_dirs` (folders with their subfolders, and +/// font files) and no platform folders, font store or font service. +fn font_dirs_catalog(font_dirs: Vec) -> Catalog { + let catalog = Catalog::new(FontEnv { os: Os::None, ..FontEnv::default() }).with_font_service(false); + // A path that isn't a folder is a font file named by itself, read whatever its name (font + // services keep fonts in files without an extension); one that becomes a folder is read as one. + let (folders, files): (Vec, Vec) = font_dirs.into_iter().partition(|p| p.is_dir()); + let catalog = catalog.with_lister(Arc::new(move || files.clone())); + catalog.set_app_dirs(folders); + catalog +} diff --git a/crates/text/src/tests/sysfonts.rs b/crates/text/src/tests/sysfonts.rs index cba8f9301b..d82ca9ae38 100644 --- a/crates/text/src/tests/sysfonts.rs +++ b/crates/text/src/tests/sysfonts.rs @@ -524,40 +524,28 @@ 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 calls this lister before it reads the folder, and the lister waits there until the + // test drops `release`, once the calls below have returned. A list that waited for the scan + // would then hold the installed fonts. The lister gives up after 10 s, so such a list fails + // the test instead of hanging it. Later scans find `release` dropped and don't wait. + let (release, held) = std::sync::mpsc::channel::<()>(); + let held = std::sync::Mutex::new(held); + let hold: craft::font_catalog::Lister = Arc::new(move || { + let _ = held.lock().unwrap().recv_timeout(std::time::Duration::from_secs(10)); + Vec::new() + }); + let db: &'static FontDb = Box::leak(Box::new(FontDb::with_font_dirs_and_lister(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. It opens the pipe - // for reading and writing, which doesn't wait for a reader, and repeats that until the scan has - // ended: a single open for writing waits for any reader and lets only the readers already - // waiting go, so the test hung when the scan reached the pipe later, or ran on this thread - // because its own thread didn't start. - 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)); - while !db.installed_fonts_ready() { - // The pipe is opened and closed with nothing written. - drop(std::fs::OpenOptions::new().read(true).write(true).open(&release).unwrap()); - std::thread::yield_now(); - } - }); let ready = db.installed_fonts_ready(); let listed = db.menu_family_list_now(); - drop(returned); + drop(release); 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)); - guard.join().unwrap(); assert!(db.installed_fonts_ready()); assert_ne!(db.generation(), generation, "a menu built from the short list is built again"); assert!(has(&db.menu_family_list_now(), FAMILY));