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 532c9dfc7..87edb8b4f 100644 --- a/crates/ui/tests/visual_regression.rs +++ b/crates/ui/tests/visual_regression.rs @@ -9,6 +9,7 @@ mod fixtures; use std::path::PathBuf; +use std::sync::{Mutex, MutexGuard, PoisonError}; use egui::{Color32, Context, Event, FullOutput, Modifiers, PointerButton, Pos2, RawInput, Rect, Response, TextureId, Ui}; use egui_kittest::{TestRenderer, wgpu::WgpuTestRenderer}; @@ -28,6 +29,17 @@ struct Capture { meshes: Option>, } +/// 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 +/// renderer is dropped. A test that failed while holding it does not block the others. +fn gpu_lock() -> MutexGuard<'static, ()> { + GPU_LOCK.lock().unwrap_or_else(PoisonError::into_inner) +} + fn context(theme: Theme, scale: f32) -> Context { let ctx = Context::default(); ctx.set_theme(theme.egui()); @@ -184,6 +196,7 @@ impl Artifacts { #[test] fn rendered_controls_match_independent_reference_pixels_and_meshes() { + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("controls"); let mut count = 0; @@ -213,6 +226,7 @@ fn rendered_controls_match_independent_reference_pixels_and_meshes() { #[test] fn visual_comparisons_detect_color_spacing_and_stroke_mutations() { + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("negative-controls"); let mut count = 0; @@ -241,6 +255,7 @@ fn toolbar_fitting_matches_literal_group_and_overflow_positions() { use craft_ui::layout::fit_toolbar; use egui::{Align, Layout, Stroke, StrokeKind, UiBuilder, pos2, vec2}; + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("toolbar"); let mut count = 0; @@ -295,6 +310,7 @@ fn widget_palette_renders_all_five_states_with_existing_borders_and_expansion() use craft_ui::theme::WidgetPalette; use egui::{CornerRadius, Stroke, StrokeKind, pos2, vec2}; + let _gpu = gpu_lock(); let mut renderer = WgpuTestRenderer::new(); let mut artifacts = Artifacts::new("widget-palette"); for theme in [Theme::Light, Theme::Dark] {