Merge pull request #1203 from dotCooCoo/fix/ui-tests-share-one-gpu

craft-ui: tests that render take turns with the wgpu renderer
This commit is contained in:
FredApps
2026-11-15 03:31:42 +01:00
committed by GitHub
4 changed files with 29 additions and 0 deletions
+1
View File
@@ -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();
+11
View File
@@ -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)
}
+1
View File
@@ -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));
+16
View File
@@ -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<Vec<Mesh>>,
}
/// 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] {