Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Test: hovering a large body checks the pick index doesn't block, without timing (#10325)

bherbruck604515cfb3f0
diff --git a/crates/ui-egui/src/pick_index.rs b/crates/ui-egui/src/pick_index.rs
index 8ff6956b2b..859f97de68 100644
--- a/crates/ui-egui/src/pick_index.rs
+++ b/crates/ui-egui/src/pick_index.rs
@@ -17,7 +17,30 @@ use crate::viewport::Proj;
/// Indexes by mesh address, each with its mesh held (so the address stays its own); `None`
/// while a worker is still making it.
#[derive(Default)]
-pub(crate) struct PickIndexes(Arc<Mutex<Indexes>>);
+pub(crate) struct PickIndexes(Arc<Mutex<Indexes>>, #[cfg(test)] Arc<TestGate>);
+
+/// Lets a test hold the worker before it starts making an index, so "still being made" can be
+/// observed without racing the clock. Open unless a test closes it.
+#[cfg(test)]
+#[derive(Default)]
+pub(crate) struct TestGate {
+ closed: Mutex<bool>,
+ changed: std::sync::Condvar,
+}
+
+#[cfg(test)]
+impl TestGate {
+ fn wait_open(&self) {
+ let mut closed = self.closed.lock().unwrap();
+ while *closed {
+ closed = self.changed.wait(closed).unwrap();
+ }
+ }
+ pub(crate) fn set_closed(&self, value: bool) {
+ *self.closed.lock().unwrap() = value;
+ self.changed.notify_all();
+ }
+}
type Indexes = HashMap<usize, (Arc<Mesh>, Option<Arc<MeshIndex>>)>;
@@ -39,7 +62,11 @@ impl PickIndexes {
if size > AT_ONCE {
map.insert(key, (mesh.clone(), None));
let (indexes, source) = (self.0.clone(), mesh.clone());
+ #[cfg(test)]
+ let gate = self.1.clone();
let started = std::thread::Builder::new().name("pick-index".into()).spawn(move || {
+ #[cfg(test)]
+ gate.wait_open();
let index = Arc::new(MeshIndex::new(&source));
if let Ok(mut map) = indexes.lock()
&& let Some(slot) = map.get_mut(&key)
@@ -64,6 +91,17 @@ impl PickIndexes {
}
}
+ #[cfg(test)]
+ pub(crate) fn gate(&self) -> Arc<TestGate> {
+ self.1.clone()
+ }
+
+ /// Indexes still being made by a worker.
+ #[cfg(test)]
+ pub(crate) fn pending(&self) -> usize {
+ self.0.lock().map_or(0, |m| m.values().filter(|(_, i)| i.is_none()).count())
+ }
+
#[cfg(test)]
pub(crate) fn len(&self) -> usize {
self.0.lock().map_or(0, |m| m.len())
diff --git a/crates/ui-egui/src/preselect_tests.rs b/crates/ui-egui/src/preselect_tests.rs
index dadd634af7..df85f2323e 100644
--- a/crates/ui-egui/src/preselect_tests.rs
+++ b/crates/ui-egui/src/preselect_tests.rs
@@ -2724,8 +2724,10 @@ fn convert_mesh_asks_for_the_body_and_converts_the_selected_one() {
/// #3745: hovering a large body never waits for its pick index. A long modelled thread's mesh
/// has a quarter of a million triangles and its index took a third of a second to make, on the
/// first hover, in the frame. Now the index is made on a worker and the first hovers scan the
-/// mesh: the first hover takes a small part of what making the index takes, and still finds
-/// the face under the pointer.
+/// mesh. Checked without timing: the worker is held at a gate, so the index is certainly still
+/// being made during the hover, and the hover must still find the face under the pointer. Were
+/// the hover to build the index itself it would not be pending, and a hover that waited on the
+/// worker would never return while the gate is closed.
#[test]
fn hovering_a_large_body_does_not_wait_for_its_pick_index() {
// A closed ball of 300 000 triangles, inserted as a mesh body.
@@ -2763,19 +2765,21 @@ fn hovering_a_large_body_does_not_wait_for_its_pick_index() {
h.app.run("file.insert_mesh", json!({"path": path.to_string_lossy()})).unwrap();
let mesh = h.app.session.world_state().bodies[0].mesh();
assert!(mesh.triangles.len() > 250_000, "{}", mesh.triangles.len());
- let t = std::time::Instant::now();
- let _ = solidcraft_engine::geom::MeshIndex::new(&mesh);
- let index_ms = t.elapsed().as_secs_f64() * 1000.0;
+ let gate = h.app.viewport.pick_index.gate();
+ gate.set_closed(true);
h.app.fit_view();
h.frames(3);
let at = h.at(&json!({"world": [0.0, -20.0, 0.0]})).unwrap();
- let t = std::time::Instant::now();
h.step(&json!({"move": at})).unwrap();
- let hover_ms = t.elapsed().as_secs_f64() * 1000.0;
assert!(matches!(h.app.viewport.hover, Some(crate::viewport::Hit::Face { .. })), "{:?}", h.app.viewport.hover);
- // Half, not a third: on a loaded runner the hover alone has taken 70 ms against a 190 ms index.
- // Waiting for the index would cost the whole of it, so half still tells the two apart.
- assert!(hover_ms < index_ms / 2.0, "the first hover took {hover_ms:.1} ms; making the index takes {index_ms:.1} ms");
+ assert_eq!(h.app.viewport.pick_index.pending(), 1, "the hover should have left the index to the worker");
+ // Let the worker finish so it does not outlive the test holding the mesh.
+ gate.set_closed(false);
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(120);
+ while h.app.viewport.pick_index.pending() > 0 && std::time::Instant::now() < deadline {
+ std::thread::sleep(std::time::Duration::from_millis(10));
+ }
+ assert_eq!(h.app.viewport.pick_index.pending(), 0, "the index should be made once the worker is let go");
}
/// #4452: with Combine open, bodies clicked in the Browser fill its inputs, as clicks on them