From 23b10d78fb2d86a9648951c75d498cb911372510 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Fri, 13 Nov 2026 06:05:11 -0800 Subject: [PATCH] craft-ui: unit tests that render take turns with the wgpu renderer Each egui_kittest wgpu renderer creates its own Vulkan instance and device. When one test uses its device while a test on another thread creates or destroys one, the Vulkan loader now and then crashes the test binary with SIGSEGV. The previous commit serialized the four rendering tests in tests/visual_regression.rs; the craft-ui unit test binary has the same pattern in two tests: combo::tests::the_button_and_menu_look_like_eguis and tabs::dialog::tests::selected_tab_erases_a_baseline_centered_on_its_bottom_edge. Both now take crate::gpu_test_lock() before building their harness and hold it until the harness is dropped. The comment on the lock in visual_regression.rs now names the loader race instead of concurrent rendering alone. --- crates/ui/src/combo/tests.rs | 1 + crates/ui/src/lib.rs | 11 +++++++++++ crates/ui/src/tabs/dialog.rs | 1 + crates/ui/tests/visual_regression.rs | 6 +++--- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/ui/src/combo/tests.rs b/crates/ui/src/combo/tests.rs index 90e1afb15..fbef2413a 100644 --- a/crates/ui/src/combo/tests.rs +++ b/crates/ui/src/combo/tests.rs @@ -170,6 +170,7 @@ fn scene(ui: &mut egui::Ui, ours: bool) { } fn render(ours: bool, hover: Option<&str>, open: Option<&str>) -> image::RgbaImage { + let _gpu = crate::gpu_test_lock(); // Two of the combo boxes have no label: the scene compares looks, not names. let mut h = Harness::builder().with_size(egui::vec2(480.0, 700.0)).with_accessibility_check(false).build_ui(move |ui| scene(ui, ours)); h.run(); diff --git a/crates/ui/src/lib.rs b/crates/ui/src/lib.rs index e5a2d06c2..bbdd912ac 100644 --- a/crates/ui/src/lib.rs +++ b/crates/ui/src/lib.rs @@ -27,3 +27,14 @@ pub mod text; pub mod theme; pub use combo::ComboBox; + +/// Lets one unit test at a time hold a wgpu renderer. Each renderer creates its own Vulkan instance +/// and device. When one test uses its device while a test on another thread creates or destroys +/// one, the Vulkan loader now and then crashes the test binary with SIGSEGV. Take the lock before +/// building the harness (`let _gpu = crate::gpu_test_lock();`), so it is released after the harness +/// is dropped. A test that failed while holding it does not block the others. +#[cfg(test)] +pub(crate) fn gpu_test_lock() -> std::sync::MutexGuard<'static, ()> { + static LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); + LOCK.lock().unwrap_or_else(std::sync::PoisonError::into_inner) +} diff --git a/crates/ui/src/tabs/dialog.rs b/crates/ui/src/tabs/dialog.rs index 292cbb4cf..e4820b042 100644 --- a/crates/ui/src/tabs/dialog.rs +++ b/crates/ui/src/tabs/dialog.rs @@ -123,6 +123,7 @@ mod tests { #[test] fn selected_tab_erases_a_baseline_centered_on_its_bottom_edge() { + let _gpu = crate::gpu_test_lock(); let mut h = egui_kittest::Harness::builder().with_size(Vec2::new(120.0, 80.0)).wgpu().build_ui(|ui| { ui.painter().rect_filled(ui.max_rect(), 0.0, Color32::WHITE); let rect = Rect::from_min_size(egui::pos2(10.0, 10.0), Vec2::new(100.0, 26.0)); diff --git a/crates/ui/tests/visual_regression.rs b/crates/ui/tests/visual_regression.rs index d9902965b..87edb8b4f 100644 --- a/crates/ui/tests/visual_regression.rs +++ b/crates/ui/tests/visual_regression.rs @@ -29,9 +29,9 @@ struct Capture { meshes: Option>, } -/// Lets one test at a time hold a wgpu renderer. With Mesa's software Vulkan driver, a test -/// binary that renders on two devices at once from parallel test threads now and then crashes -/// with SIGSEGV. +/// Lets one test at a time hold a wgpu renderer. Each renderer creates its own Vulkan instance and +/// device. When one test uses its device while a test on another thread creates or destroys one, +/// the Vulkan loader now and then crashes the test binary with SIGSEGV. static GPU_LOCK: Mutex<()> = Mutex::new(()); /// Take this before creating a renderer (`let _gpu = gpu_lock();`), so it is released after the