From 2947821c60f4ef474194695bcd3e71c9def380cc Mon Sep 17 00:00:00 2001 From: Lucas Meurer Date: Wed, 30 Apr 2025 12:55:57 +0200 Subject: [PATCH] Load images on the ui thread for tests (#6901) https://github.com/emilk/egui/pull/5394 made it so images would load on a background thread, which is great. But this makes snapshot tests that have images via include_image!() flakey since they might or might not load by the time the snapshot is rendered. This is no perfect solution, since the underlying problem of "waiting for something async to happen" still exists and we should add some more general solution for that. --- crates/egui_extras/src/loaders/image_loader.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/egui_extras/src/loaders/image_loader.rs b/crates/egui_extras/src/loaders/image_loader.rs index bb025651..2528c2ae 100644 --- a/crates/egui_extras/src/loaders/image_loader.rs +++ b/crates/egui_extras/src/loaders/image_loader.rs @@ -8,9 +8,6 @@ use egui::{ use image::ImageFormat; use std::{mem::size_of, path::Path, sync::Arc, task::Poll}; -#[cfg(not(target_arch = "wasm32"))] -use std::thread; - type Entry = Poll, String>>; #[derive(Default)] @@ -76,7 +73,7 @@ impl ImageLoader for ImageCrateLoader { return Err(LoadError::NotSupported); } - #[cfg(not(target_arch = "wasm32"))] + #[cfg(not(any(target_arch = "wasm32", test)))] #[expect(clippy::unnecessary_wraps)] // needed here to match other return types fn load_image( ctx: &egui::Context, @@ -88,7 +85,7 @@ impl ImageLoader for ImageCrateLoader { cache.lock().insert(uri.clone(), Poll::Pending); // Do the image parsing on a bg thread - thread::Builder::new() + std::thread::Builder::new() .name(format!("egui_extras::ImageLoader::load({uri:?})")) .spawn({ let ctx = ctx.clone(); @@ -116,7 +113,8 @@ impl ImageLoader for ImageCrateLoader { Ok(ImagePoll::Pending { size: None }) } - #[cfg(target_arch = "wasm32")] + // Load images on the current thread for tests, so they are less flaky + #[cfg(any(target_arch = "wasm32", test))] fn load_image( _ctx: &egui::Context, uri: &str,