Skip to content

Commit e167e08

Browse files
committed
Merge #1292: Tests: UI tests that render with wgpu take turns, so the Windows test run no longer crashes
2 parents 6a1c735 + 44d5a0e commit e167e08

12 files changed

Lines changed: 74 additions & 0 deletions

‎crates/ui-egui/src/animpane_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ fn preview_rejects_a_from_that_is_not_an_effect_index() {
192192
#[test]
193193
#[ignore = "writes a screenshot for review"]
194194
fn screenshot() {
195+
let _gpu = crate::gpu_lock::gpu_lock();
195196
let mut h = harness();
196197
h.run_steps(2);
197198
let img = h.render().unwrap();

‎crates/ui-egui/src/compare_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn reject_all_empties_the_pane_and_keeps_the_deck() {
6565
#[test]
6666
#[ignore = "writes a screenshot for review"]
6767
fn screenshot() {
68+
let _gpu = crate::gpu_lock::gpu_lock();
6869
let (mut h, _) = harness();
6970
// Review tab.
7071
h.state_mut().ui.tab = "Review".into();

‎crates/ui-egui/src/gpu_lock.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Tests that render a harness run one at a time in each test process.
2+
//!
3+
//! `Harness::render` creates a wgpu device on its first call, and egui_kittest prefers a CPU
4+
//! adapter. When `cargo test` runs several of these tests on parallel threads, the test binary can
5+
//! exit with `0xc0000005` (`STATUS_ACCESS_VIOLATION`). Under cargo-nextest each test is its own
6+
//! process and the lock has no effect.
7+
//!
8+
//! Every test that calls `render()` on a harness takes [`gpu_lock`] on its first line
9+
//! (`let _gpu = crate::gpu_lock::gpu_lock();`) and holds the guard for its whole run.
10+
11+
use std::sync::{Mutex, MutexGuard, PoisonError};
12+
13+
static GPU_LOCK: Mutex<()> = Mutex::new(());
14+
15+
/// Returns a guard that the test holds for its whole run, so tests that render run one at a time.
16+
/// A test that failed while holding it does not block the tests after it.
17+
pub(crate) fn gpu_lock() -> MutexGuard<'static, ()> {
18+
GPU_LOCK.lock().unwrap_or_else(PoisonError::into_inner)
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
25+
#[test]
26+
fn a_panicked_holder_does_not_block_the_next_test() {
27+
let _ = std::thread::spawn(|| {
28+
let _gpu = gpu_lock();
29+
panic!("a GPU test failed");
30+
})
31+
.join();
32+
drop(gpu_lock());
33+
}
34+
35+
#[test]
36+
fn holders_take_turns() {
37+
use std::sync::atomic::{AtomicUsize, Ordering};
38+
static INSIDE: AtomicUsize = AtomicUsize::new(0);
39+
static MAX: AtomicUsize = AtomicUsize::new(0);
40+
let threads: Vec<_> = (0..8)
41+
.map(|_| {
42+
std::thread::spawn(|| {
43+
let _gpu = gpu_lock();
44+
let now = INSIDE.fetch_add(1, Ordering::SeqCst) + 1;
45+
MAX.fetch_max(now, Ordering::SeqCst);
46+
std::thread::sleep(std::time::Duration::from_millis(2));
47+
INSIDE.fetch_sub(1, Ordering::SeqCst);
48+
})
49+
})
50+
.collect();
51+
for t in threads {
52+
assert!(t.join().is_ok());
53+
}
54+
assert_eq!(MAX.load(Ordering::SeqCst), 1);
55+
}
56+
}

‎crates/ui-egui/src/guides_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{Services, SlideApp};
1010

1111
#[test]
1212
fn turn_guides_on_drag_one_and_drop_it_off_the_slide() {
13+
let _gpu = crate::gpu_lock::gpu_lock();
1314
let mut s = Session::with_new();
1415
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
1516
let app = SlideApp::new(s, Services::default());

‎crates/ui-egui/src/inktoshape_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::{Services, SlideApp};
1111

1212
#[test]
1313
fn pen_a_rectangle_then_ink_to_shape() {
14+
let _gpu = crate::gpu_lock::gpu_lock();
1415
let mut s = Session::with_new();
1516
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
1617
let mut app = SlideApp::new(s, Services::default());

‎crates/ui-egui/src/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,8 @@ mod field_frame_tests;
21202120
#[cfg(test)]
21212121
mod find_dialog_tests;
21222122
#[cfg(test)]
2123+
mod gpu_lock;
2124+
#[cfg(test)]
21232125
mod huge_deck_tests;
21242126
#[cfg(test)]
21252127
mod ime_tests;
@@ -2867,6 +2869,7 @@ mod tests {
28672869
/// they can be selected and moved.
28682870
#[test]
28692871
fn off_slide_objects_show_on_the_pasteboard_and_can_be_selected_and_moved() {
2872+
let _gpu = crate::gpu_lock::gpu_lock();
28702873
let mut harness = canvas_harness();
28712874
let off =
28722875
harness.state_mut().session.execute("shape.insert", &json!({"preset": "rect", "rect": [1000.0, 100.0, 120.0, 80.0]})).unwrap()["id"]

‎crates/ui-egui/src/replacefonts_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ fn ribbon_button_opens_the_dialog_and_replaces() {
6767
#[test]
6868
#[ignore = "writes a screenshot for review"]
6969
fn screenshot() {
70+
let _gpu = crate::gpu_lock::gpu_lock();
7071
let mut h = harness();
7172
h.render().unwrap().save("/tmp/replacefonts0.png").unwrap();
7273
h.state_mut().dialog = Some(crate::dialogs::Dialog::new("replaceFonts"));

‎crates/ui-egui/src/ruler_tests.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn ruler(h: &mut Harness<'_, SlideApp>) -> serde_json::Value {
3434

3535
#[test]
3636
fn drag_indents_add_move_and_remove_tabs_on_the_ruler() {
37+
let _gpu = crate::gpu_lock::gpu_lock();
3738
let mut s = Session::with_new();
3839
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
3940
let id = s.execute("insert.textBox", &json!({"rect": [100, 150, 500, 40]})).unwrap()["id"].as_u64().unwrap();
@@ -105,6 +106,7 @@ fn editing_harness() -> Harness<'static, SlideApp> {
105106

106107
#[test]
107108
fn the_tab_selector_picks_the_kind_of_stop_a_click_sets() {
109+
let _gpu = crate::gpu_lock::gpu_lock();
108110
use egui_kittest::kittest::Queryable;
109111
let mut h = editing_harness();
110112
let (slide, k) = (h.state().slide_rect.unwrap(), h.state().canvas_scale);
@@ -127,6 +129,7 @@ fn the_tab_selector_picks_the_kind_of_stop_a_click_sets() {
127129

128130
#[test]
129131
fn tabs_dialog_sets_clears_and_applies_on_ok() {
132+
let _gpu = crate::gpu_lock::gpu_lock();
130133
use egui_kittest::kittest::Queryable;
131134
let mut h = editing_harness();
132135
h.state_mut().session.execute("format.tabs", &json!({"add": {"pos": 36}})).unwrap();

‎crates/ui-egui/src/smartart_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ fn home_convert_to_smartart_gallery_converts_the_text() {
7878

7979
#[test]
8080
fn layout_gallery_shows_cached_pictures_and_previews_on_hover() {
81+
let _gpu = crate::gpu_lock::gpu_lock();
8182
let mut s = Session::with_new();
8283
s.execute("slide.new", &json!({"layout": "blank"})).unwrap();
8384
s.execute("insert.smartArt", &json!({"kind": "process", "items": ["Plan", "Build", "Ship"]})).unwrap();

‎crates/ui-egui/src/spelling_tests.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{Services, SlideApp};
1010

1111
#[test]
1212
fn spelling_dialog_changes_and_ignores_words() {
13+
let _gpu = crate::gpu_lock::gpu_lock();
1314
let aff = "SET UTF-8\nTRY esianrtolcdugmphbyfvkwzxjq\nSFX S Y 1\nSFX S 0 s .\n";
1415
let dic = "6\nthe/S\nslide/S\nquarter/S\nplan/S\nour\nfor\n";
1516
deckcraft_engine::spell::install("en-US", deckcraft_engine::spell::Dictionary::parse(aff.as_bytes(), dic.as_bytes()));
@@ -49,6 +50,7 @@ fn spelling_dialog_changes_and_ignores_words() {
4950
/// typed; the context menu's correction and Add to Dictionary clear them.
5051
#[test]
5152
fn squiggles_mark_misspelt_words_in_the_background() {
53+
let _gpu = crate::gpu_lock::gpu_lock();
5254
let aff = "SET UTF-8\nTRY esianrtolcdugmphbyfvkwzxjq\nSFX S Y 1\nSFX S 0 s .\n";
5355
let dic = "7\nthe/S\nslide/S\nquarter/S\nplan/S\nour\nfor\nreview\n";
5456
deckcraft_engine::spell::install("xx-SQ", deckcraft_engine::spell::Dictionary::parse(aff.as_bytes(), dic.as_bytes()));
@@ -115,6 +117,7 @@ fn squiggles_mark_misspelt_words_in_the_background() {
115117
/// gets there a few milliseconds per frame.
116118
#[test]
117119
fn squiggles_in_table_cells_and_notes_and_on_the_ui_thread() {
120+
let _gpu = crate::gpu_lock::gpu_lock();
118121
let aff = "SET UTF-8\nTRY esianrtolcdugmphbyfvkwzxjq\nSFX S Y 1\nSFX S 0 s .\n";
119122
let dic = "10\nthe/S\nplan/S\nsales\nregion/S\ntotal/S\nnorth\nsouth\nremember\nto\nshow\n";
120123
deckcraft_engine::spell::install("xx-TB", deckcraft_engine::spell::Dictionary::parse(aff.as_bytes(), dic.as_bytes()));
@@ -192,6 +195,7 @@ fn right_click(h: &mut Harness<'_, SlideApp>, pos: egui::Pos2) {
192195
/// dialog marks text and Do Not Check Spelling clears its squiggles.
193196
#[test]
194197
fn notes_menu_language_dialog_and_repeated_words() {
198+
let _gpu = crate::gpu_lock::gpu_lock();
195199
use egui::accesskit::Role;
196200
let aff = "SET UTF-8\nTRY esianrtolcdugmphbyfvkwzxjq\n";
197201
let dic = "6\nthe\nplan\nremember\nis\ngood\nwork\n";

0 commit comments

Comments
 (0)