LR-BEHAV-GPU: a deterministic test for the app's warm-up wait on drop (issue #620)

Fred's review of #4697: the first regression test could pass without
exercising the wait — ready() || !enabled() also holds when the warm-up
finished or never started, and no pending warm-up was ever established.

The gpu crate exposes doc-hidden test support, hold_warm_up_for_test(),
entering the same gate the real warm_up enters: a substitute warm-up the
caller holds. The test proves it is in flight (wait_warm_up(ZERO) is
false), builds an app that never ran a frame on another thread (the app
is not Send — its host-service callbacks are not — so it is built there),
and verifies with channel synchronization that dropping the app does not
return while the hold is kept, and returns once it is released. Without
the wait in Drop for LightcraftApp it fails at that assertion; with it,
it passes. No GPU, no device, no timing beyond a one-second observation
window whose proof is the release.
This commit is contained in:
skylartaylor
2026-11-26 11:46:23 +01:00
parent c38ce6e26d
commit 44d1275f82
2 changed files with 60 additions and 11 deletions
+24
View File
@@ -411,6 +411,30 @@ pub fn wait_warm_up(timeout: std::time::Duration) -> bool {
}
}
/// The guard [`hold_warm_up_for_test`] returns: the substitute warm-up, held until it is dropped.
#[cfg(not(target_arch = "wasm32"))]
struct WarmUpHold(Option<exit::Work<'static>>);
/// A substitute device warm-up in flight, held until the returned guard is dropped — the same
/// gate the real [`warm_up`] enters, so a caller can prove a warm-up is pending and that a wait
/// waits for it, without a GPU, a device or a race. Test support for one test,
/// `tests::gpu_warm_up::dropping_the_app_waits_for_a_warm_up_that_is_in_flight` in
/// lightcraft-ui-egui (a `#[cfg(test)]` helper here would not be visible to that crate's test
/// build); not for production code — a real warm-up is started with [`warm_up`]. `None` only
/// when the process is ending, when no warm-up can be in flight anyway.
#[cfg(not(target_arch = "wasm32"))]
#[doc(hidden)]
pub fn hold_warm_up_for_test() -> Option<impl Drop> {
WARM_UP.enter().map(|work| WarmUpHold(Some(work)))
}
#[cfg(not(target_arch = "wasm32"))]
impl Drop for WarmUpHold {
fn drop(&mut self) {
drop(self.0.take());
}
}
/// Whether a usable GPU adapter exists and GPU rendering is enabled (creates the device on first
/// use).
pub fn available() -> bool {
+36 -11
View File
@@ -1,19 +1,44 @@
//! A test is a process of its own under nextest, and that process ends as soon as the test
//! returns. The app's first frame starts the GPU device warm-up on a thread of its own; a process
//! that ends while that thread is still loading the driver crashes there (issue #620; in CI,
//! tests killed by SIGSEGV after they had passed). So dropping the app must leave no warm-up
//! running.
//! tests killed by SIGSEGV after they had passed). So dropping the app waits for a warm-up that
//! is still in flight. This exercises that wait with a substitute warm-up held on the same gate,
//! so it needs no GPU, no device and no race.
use std::sync::mpsc;
use std::time::Duration;
use crate::{LightcraftApp, Services};
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn dropping_the_app_leaves_no_gpu_device_creation_running() {
let ctx = egui::Context::default();
let mut app = LightcraftApp::new(lightcraft_engine::Session::new(), Services { png: None, ..Default::default() });
assert!(app.ui.settings.gpu, "GPU rendering is on by default, so the first frame starts the warm-up");
let mut out = ctx.run_ui(egui::RawInput::default(), |ui| app.logic(ui.ctx()));
out.textures_delta.clear();
drop(app);
// checked at once: nothing waits between the end of a test and the end of its process
assert!(lightcraft_engine::gpu::ready() || !lightcraft_engine::gpu::enabled(), "the GPU device is still being created after the app is gone");
fn dropping_the_app_waits_for_a_warm_up_that_is_in_flight() {
// In flight from here until `hold` is dropped (test support in the gpu crate, on the gate the
// real warm-up enters)
let hold = lightcraft_engine::gpu::hold_warm_up_for_test().expect("the warm-up gate is open in a test");
assert!(!lightcraft_engine::gpu::wait_warm_up(Duration::ZERO), "the substitute warm-up is in flight");
let (tx, rx) = mpsc::channel::<&'static str>();
let dropping = std::thread::spawn(move || {
// Built here, not moved in: `LightcraftApp` is not `Send` (its host-service callbacks are
// not), and this test does not need it to be. An app that never ran a frame has nothing of
// its own pending, so the only thing that can hold up its drop is the warm-up wait.
let app = LightcraftApp::new(lightcraft_engine::Session::new(), Services { png: None, ..Default::default() });
tx.send("dropping").unwrap();
drop(app);
tx.send("dropped").unwrap();
});
assert_eq!(rx.recv_timeout(Duration::from_secs(30)).unwrap(), "dropping");
// The drop has not returned while the warm-up is held. The one second is only the
// observation window for that — the thread is already past "dropping", sent after the
// construction and just before the drop, so an unwaiting drop would have torn the app down
// and returned well inside it — and the proof is the step after: releasing the hold is what
// lets the drop return.
assert_eq!(
rx.recv_timeout(Duration::from_secs(1)),
Err(mpsc::RecvTimeoutError::Timeout),
"dropping the app returned while a warm-up was in flight"
);
drop(hold);
assert_eq!(rx.recv_timeout(Duration::from_secs(30)).unwrap(), "dropped");
dropping.join().unwrap();
}