diff --git a/crates/ui-egui/src/design_properties.rs b/crates/ui-egui/src/design_properties.rs index 0d432b1d9..254f6e572 100644 --- a/crates/ui-egui/src/design_properties.rs +++ b/crates/ui-egui/src/design_properties.rs @@ -133,8 +133,20 @@ pub fn open_object(app: &mut SolveApp, command: &str, params: &Value, fallback: // copy beside it, the card shows when done. Headless and in tests (no wait set), here. // The worker reports how many bodies it has measured, and stops when the card closes (#8706). let progress = Arc::new(Progress::default()); + // Tests hold the reply until let go (`apply_gate`), so a window thread held up under load + // cannot find the measurement already in where the card should still be measuring. + #[cfg(test)] + let gate = app.apply_gate.clone(); + let hold = move || { + #[cfg(test)] + if let Some(gate) = gate + && let Ok(gate) = gate.lock() + { + let _ = gate.recv(); + } + }; if let Some(wait) = app.apply_off_thread - && let Some(rx) = measure_beside(app.session.scratch(), command, params, progress.clone()) + && let Some(rx) = measure_beside(app.session.scratch(), command, params, progress.clone(), hold) { match rx.recv_timeout(wait) { Ok(reply) => opened(app, reply, fallback), @@ -190,13 +202,14 @@ fn opened(app: &mut SolveApp, reply: Result, fallback: String) { } /// Run `command` on `copy` on a worker, watched by `progress`; the reply comes on the returned -/// channel. +/// channel once `hold` returns (which only tests make wait). #[cfg(not(target_arch = "wasm32"))] fn measure_beside( mut copy: solidcraft_engine::Session, command: &str, params: &Value, progress: Arc, + hold: impl FnOnce() + Send + 'static, ) -> Option>> { let (tx, rx) = std::sync::mpsc::channel(); let (command, params) = (command.to_string(), params.clone()); @@ -204,6 +217,7 @@ fn measure_beside( .name("properties".into()) .spawn(move || { let reply = solidcraft_engine::doc::progress::watch(progress, || copy.execute(&command, ¶ms)); + hold(); let _ = tx.send(reply.map_err(|e| e.to_string())); }) .ok()?; @@ -217,6 +231,7 @@ fn measure_beside( _command: &str, _params: &Value, _progress: Arc, + _hold: impl FnOnce() + Send + 'static, ) -> Option>> { None } @@ -631,9 +646,14 @@ mod tests { h.app.run("solid.box", json!({"length": 10, "width": 20, "height": 30, "body_name": "Block"})).unwrap(); let here = h.app.session.execute("component.properties", &json!({})).unwrap(); h.app.apply_off_thread = Some(std::time::Duration::ZERO); + // The worker holds its reply until let go, so a window thread held up under load cannot + // find it already in. + let (finish, gate) = std::sync::mpsc::channel(); + h.app.apply_gate = Some(std::sync::Arc::new(std::sync::Mutex::new(gate))); super::open_object(&mut h.app, "component.properties", &json!({}), String::new()); assert!(h.app.menu.props.is_none(), "nothing is waited for on the window thread"); let m = h.app.menu.props_measuring.take().expect("measuring beside the window"); + finish.send(()).unwrap(); assert_eq!(m.rx.recv().unwrap().unwrap(), here, "the copy measures what the design here does"); let (tx, rx) = std::sync::mpsc::channel(); @@ -674,11 +694,15 @@ mod tests { h.app.run("occurrence.copy", json!({"component": block, "translate": [20 * i, 0, 0]})).unwrap(); } h.app.apply_off_thread = Some(std::time::Duration::ZERO); + // Held until checked: under load the worker could otherwise finish before the menu returns. + let (finish, gate) = std::sync::mpsc::channel(); + h.app.apply_gate = Some(std::sync::Arc::new(std::sync::Mutex::new(gate))); let items = crate::context_menu::items(&h.app, &crate::context_menu::Target::Component { id: 0 }); let item = items.iter().find(|i| i.label == "Properties").expect("Properties item"); crate::context_menu::run_item(&mut h.app, item, egui::pos2(0.0, 0.0)); assert!(h.app.menu.props_measuring.is_some(), "the menu returns at once, the card measuring"); assert!(h.app.menu.props.is_none()); + finish.send(()).unwrap(); let deadline = std::time::Instant::now() + std::time::Duration::from_secs(300); while h.app.menu.props.is_none() { assert!(std::time::Instant::now() < deadline, "the measurements never came in"); diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 1b8df2ad7..f960a6876 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -619,7 +619,8 @@ pub struct SolveApp { /// How long OK waits for a feature before computing it off the window thread, saying so; /// `None`: it waits as long as it takes (headless use, tests). The desktop app sets it. pub apply_off_thread: Option, - /// Tests: the off-thread evaluation waits for this before it reports done. + /// Tests: work started off the window thread (a feature's evaluation, a Properties + /// measurement) waits for this before it reports done. #[cfg(test)] pub(crate) apply_gate: Option>>>, /// The last command started interactively (id, label), for Repeat.