Merge pull request #9824 from dotCooCoo/fix/placing-tests-handshakes

Tests: placing tests wait for the read instead of a time window
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-27 21:39:44 -08:00
commit dfc19ff3a7
4 files changed
+116 -96

No files matched your search

@@ -501,12 +501,9 @@ fn place_cursor_exec_checks_recording_step_limit_on_completion() {
assert_ne!(app.ui.status, steps, "nothing is checked before the file is placed");
// The read runs on the place lane; a frame after it ends places the file.
let ctx = crate::testing::ui_context();
let t0 = std::time::Instant::now();
while !app.placing.pending.is_empty() && t0.elapsed().as_secs() < 60 {
craft::ui::shell::embed::before_input(&ctx, |out| app.frame_input(out));
ctx.run_ui(egui::RawInput::default(), |ui| app.tick(ui.ctx())).textures_delta.clear();
std::thread::yield_now();
}
crate::placing::wait(&mut app);
craft::ui::shell::embed::before_input(&ctx, |out| app.frame_input(out));
ctx.run_ui(egui::RawInput::default(), |ui| app.tick(ui.ctx())).textures_delta.clear();
assert!(app.placing.pending.is_empty(), "a frame after the read places the file");
}
assert_eq!(app.session.journal.len(), journal + 1, "background {background}: the place journals one step");
+13
View File
@@ -537,6 +537,19 @@ pub(crate) fn fill_lines(app: &mut VectorcraftApp, files: &[Value], info: &mut V
}
}
/// Unit tests: waits until the open Place dialog's lines have been read, and leaves them for
/// [`fill_lines`].
#[cfg(test)]
pub(crate) fn wait_lines(app: &mut VectorcraftApp) {
let Some((_, read)) = &mut app.place.reading_lines else { return };
let (tx, rx) = std::sync::mpsc::channel();
// A read that stopped without lines leaves the new channel closed too.
if let Ok(lines) = read.recv() {
let _ = tx.send(lines);
}
*read = rx;
}
/// One line about `file` for the Place dialog, read at once ([`line`]).
fn summary(app: &mut VectorcraftApp, file: &Value, units: vectorcraft_doc::Unit) -> String {
let Some(p) = source(app, file) else { return "not found".into() };
+57 -6
View File
@@ -206,7 +206,61 @@ pub struct Placing {
lane: Lane,
/// Unit tests: how many reads run now, and the most that ran at once.
#[cfg(test)]
pub(crate) reading: std::sync::Arc<[std::sync::atomic::AtomicUsize; 2]>,
pub(crate) reading: std::sync::Arc<Reading>,
}
/// Unit tests: how many reads run now and the most that ran at once, and a signal each time a read
/// starts or ends.
#[cfg(test)]
#[derive(Default)]
pub(crate) struct Reading {
counts: std::sync::Mutex<[usize; 2]>,
changed: std::sync::Condvar,
}
#[cfg(test)]
impl Reading {
fn counts(&self) -> std::sync::MutexGuard<'_, [usize; 2]> {
self.counts.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
fn started(&self) {
let mut counts = self.counts();
counts[0] += 1;
counts[1] = counts[1].max(counts[0]);
self.changed.notify_all();
}
fn ended(&self) {
self.counts()[0] -= 1;
self.changed.notify_all();
}
/// The most reads that ran at once.
pub(crate) fn most(&self) -> usize {
self.counts()[1]
}
/// Waits until a read has run and none runs now.
pub(crate) fn wait_ended(&self) {
let mut counts = self.counts();
while !(counts[1] >= 1 && counts[0] == 0) {
counts = self.changed.wait(counts).unwrap_or_else(std::sync::PoisonError::into_inner);
}
}
}
/// Unit tests: waits until the first file read for Place has been read, and leaves the result for
/// [`poll`].
#[cfg(test)]
pub(crate) fn wait(app: &mut VectorcraftApp) {
let Some(p) = app.placing.pending.first_mut() else { return };
let (tx, done) = channel();
// A read that stopped without a result leaves the new channel closed too.
if let Ok(r) = p.done.recv() {
let _ = tx.send(r);
}
p.done = done;
}
struct PendingSwatch {
@@ -349,15 +403,12 @@ fn spawn_labeled(
let raised = token.clone();
let job: Box<dyn FnOnce() + Send> = Box::new(move || {
#[cfg(test)]
{
let now = reading[0].fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1;
reading[1].fetch_max(now, std::sync::atomic::Ordering::SeqCst);
}
reading.started();
let r = scope
.run(|| vectorcraft_engine::guard::catch_panic(|| cancel::scope(&raised, None, read)))
.unwrap_or_else(|msg| Err(format!("internal error: {msg} (please report this bug)")));
#[cfg(test)]
reading[0].fetch_sub(1, std::sync::atomic::Ordering::SeqCst);
reading.ended();
// The app may have quit meanwhile: nothing to hand it to.
let _ = tx.send(r);
});
+43 -84
View File
@@ -92,25 +92,26 @@ fn pdf() -> Vec<u8> {
vectorcraft_format::base64_decode(v["dataBase64"].as_str().unwrap()).unwrap()
}
/// Poll each frame until every file read for Place has been placed (at most 60 s).
/// Waits for each file read for Place in turn and polls once it is read, until every one has been
/// placed.
fn placed(app: &mut VectorcraftApp) {
let t0 = std::time::Instant::now();
while !app.placing.pending.is_empty() && t0.elapsed().as_secs() < 60 {
while !app.placing.pending.is_empty() {
let waiting = app.placing.pending.len();
wait(app);
poll(app);
std::thread::yield_now();
assert!(app.placing.pending.len() < waiting, "the file that was read is placed");
}
assert!(app.placing.pending.is_empty(), "read within 60 s");
}
/// Waits until the read that was started has finished on its job (the held reader was let go).
fn read_finished(app: &VectorcraftApp) {
let t0 = std::time::Instant::now();
while !(app.placing.reading[1].load(std::sync::atomic::Ordering::SeqCst) >= 1
&& app.placing.reading[0].load(std::sync::atomic::Ordering::SeqCst) == 0)
{
assert!(t0.elapsed().as_secs() < 60, "read within 60 s");
std::thread::yield_now();
}
app.placing.reading.wait_ended();
}
/// Waits until the first file read for Place has been read, then polls once.
fn poll_read(app: &mut VectorcraftApp) {
wait(app);
poll(app);
}
/// What a place changes: the active document (without the second it was made in), its
@@ -250,7 +251,8 @@ impl ClipboardBackend for OtherApps {
let hold = if self.holds.is_empty() { None } else { self.holds.remove(0) };
Ok(Some(Box::new(move || {
if let Some(hold) = hold {
let _ = hold.recv_timeout(std::time::Duration::from_secs(10));
// A send lets the read go on, and so does a sender dropped without one.
let _ = hold.recv();
}
*thread.lock().unwrap() = std::thread::current().name().map(str::to_string);
bitmap.map(platform)
@@ -623,11 +625,8 @@ fn a_find_font_dialog_releases_a_deferred_swatch_paste_before_queued_work() {
app.run("select.set", json!({"ids": [anchor]})).unwrap();
crate::menus::invoke(&mut app, id, json!({}));
crate::menus::invoke(&mut app, id, json!({}));
let start = std::time::Instant::now();
while open_dialogs(&app).is_empty() && start.elapsed().as_secs() < 60 {
poll(&mut app);
std::thread::yield_now();
}
poll_read(&mut app);
assert!(!open_dialogs(&app).is_empty(), "the first paste asks its question");
assert!(swatches_asked(&app));
assert_eq!(app.placing.pending.len(), 1);
let later = app.run("shape.rectangle", json!({"x": 200, "y": 10, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
@@ -871,11 +870,8 @@ fn a_queued_paste_stacks_after_a_deferred_swatch_confirmation() {
app.run("select.set", json!({"ids": [anchor]})).unwrap();
crate::menus::invoke(&mut app, id, json!({}));
crate::menus::invoke(&mut app, id, json!({}));
let start = std::time::Instant::now();
while open_dialogs(&app).is_empty() && start.elapsed().as_secs() < 60 {
poll(&mut app);
std::thread::yield_now();
}
poll_read(&mut app);
assert!(!open_dialogs(&app).is_empty(), "the first paste asks its question");
assert_eq!(app.placing.pending.len(), 1);
answer_swatches(&mut app, true);
placed(&mut app);
@@ -985,11 +981,7 @@ fn a_second_paste_in_front_follows_the_first() {
app.run("select.set", json!({"ids": [b]})).unwrap();
crate::menus::invoke(&mut app, "edit.pasteInFront", json!({}));
crate::menus::invoke(&mut app, "edit.pasteInFront", json!({}));
let t0 = std::time::Instant::now();
while app.placing.pending.len() > 1 && t0.elapsed().as_secs() < 60 {
poll(&mut app);
std::thread::yield_now();
}
poll_read(&mut app);
assert_eq!(app.placing.pending.len(), 1, "the first paste ran");
if third {
crate::menus::invoke(&mut app, "edit.pasteInFront", json!({}));
@@ -1023,11 +1015,7 @@ fn a_paste_beside_a_finished_picture_does_not_follow_another_chain() {
crate::menus::invoke(&mut app, "edit.pasteInFront", json!({}));
app.run("select.set", json!({"ids": [y]})).unwrap();
crate::menus::invoke(&mut app, "edit.pasteInFront", json!({}));
let t0 = std::time::Instant::now();
while app.placing.pending.len() > 1 && t0.elapsed().as_secs() < 60 {
poll(&mut app);
std::thread::yield_now();
}
poll_read(&mut app);
assert_eq!(app.placing.pending.len(), 1, "the first paste ran");
// The first picture is selected.
crate::menus::invoke(&mut app, "edit.pasteInFront", json!({}));
@@ -1055,11 +1043,7 @@ fn pastes_in_two_documents_follow_their_own() {
app.run("select.set", json!({"ids": [b]})).unwrap();
crate::menus::invoke(&mut app, "edit.pasteInFront", json!({}));
crate::menus::invoke(&mut app, "edit.pasteInFront", json!({}));
let t0 = std::time::Instant::now();
while app.placing.pending.len() > 1 && t0.elapsed().as_secs() < 60 {
poll(&mut app);
std::thread::yield_now();
}
poll_read(&mut app);
assert_eq!(app.placing.pending.len(), 1, "the first paste ran");
// A paste in another document, then one more in the first.
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
@@ -1413,17 +1397,12 @@ fn the_place_dialogs_lines_are_read_off_the_ui_thread() {
app.run("file.place", p).unwrap();
assert_eq!(place_lines(&app), ["", ""]);
let host = app.dialogs().expect("the window's dialog host");
let t0 = std::time::Instant::now();
loop {
let reading = host.with(|h| {
let d = h.get_mut::<crate::dialogs::place::PlaceDialog>().expect("the dialog stays open");
crate::place::fill_lines(&mut app, &d.files, &mut d.info)
});
if !reading || t0.elapsed().as_secs() > 60 {
break;
}
std::thread::yield_now();
}
crate::place::wait_lines(&mut app);
let reading = host.with(|h| {
let d = h.get_mut::<crate::dialogs::place::PlaceDialog>().expect("the dialog stays open");
crate::place::fill_lines(&mut app, &d.files, &mut d.info)
});
assert!(!reading, "the lines were read");
assert_eq!(place_lines(&app), want);
}
@@ -1455,7 +1434,7 @@ fn files_dropped_together_are_read_one_at_a_time() {
crate::place::drop_files(&mut app, dropped);
placed(&mut app);
assert_eq!(names(&app).len(), 4);
assert_eq!(app.placing.reading[1].load(std::sync::atomic::Ordering::SeqCst), 1, "the most reads at once");
assert_eq!(app.placing.reading.most(), 1, "the most reads at once");
}
/// A PNG read off the UI thread is decoded there once: its pixels are in the image store the
@@ -1828,11 +1807,7 @@ fn a_file_read_while_a_dialog_is_open_waits_for_it_to_close() {
let mut app = app(true);
start(&mut app, &json!({"path": path}), false).unwrap();
let question = crate::tests::picks::show_question(&mut app);
let t0 = std::time::Instant::now();
while t0.elapsed().as_millis() < 400 {
poll(&mut app);
std::thread::yield_now();
}
poll_read(&mut app);
assert!(names(&app).is_empty() && app.placing.pending.len() == 1, "the file waits for the dialog");
crate::tests::picks::close_shown(&mut app, question);
placed(&mut app);
@@ -1859,11 +1834,7 @@ fn a_file_read_during_a_drag_waits_for_it_to_end() {
app.session.pointer(&PointerEvent::new(kind, x, 20.0), view).unwrap();
}
assert!(app.session.in_interaction());
let t0 = std::time::Instant::now();
while t0.elapsed().as_millis() < 400 {
poll(&mut app);
std::thread::yield_now();
}
poll_read(&mut app);
assert!(app.placing.pending.len() == 1 && app.session.in_interaction(), "other document: {other_document}");
// The drag moves on before it ends.
for kind in [PointerKind::Drag, PointerKind::Up] {
@@ -1892,11 +1863,7 @@ fn a_file_read_during_a_place_cursor_drag_waits_for_it_to_end() {
crate::canvas::dispatch(&mut app, &PointerEvent::new(kind, x, 20.0), view);
}
assert!(app.session.tool_busy() && !app.session.in_interaction());
let t0 = std::time::Instant::now();
while t0.elapsed().as_millis() < 400 {
poll(&mut app);
std::thread::yield_now();
}
poll_read(&mut app);
assert!(app.placing.pending.len() == 1 && app.session.tool_busy(), "the file waits for the drag");
crate::canvas::dispatch(&mut app, &PointerEvent::new(PointerKind::Up, 60.0, 20.0), view);
placed(&mut app);
@@ -1961,13 +1928,7 @@ fn a_file_read_again_when_placed_stays_within_the_roots_it_was_read_in() {
let mut app = app(true);
let p = json!({"path": link.to_string_lossy(), "dxf": {"fit": true}, "at": [100, 150]});
vectorcraft_engine::file_access::confine(Some(&roots), || start(&mut app, &p, false)).unwrap();
let t0 = std::time::Instant::now();
while !(app.placing.reading[1].load(std::sync::atomic::Ordering::SeqCst) == 1
&& app.placing.reading[0].load(std::sync::atomic::Ordering::SeqCst) == 0)
{
assert!(t0.elapsed().as_secs() < 60, "read within 60 s");
std::thread::yield_now();
}
wait(&mut app);
// Read; now the path leads outside the roots and the artboard under `at` changes.
std::fs::remove_file(&link).unwrap();
std::os::unix::fs::symlink(base.join("out").join("real.dxf"), &link).unwrap();
@@ -2003,13 +1964,12 @@ fn progress(app: &mut VectorcraftApp, ctx: &egui::Context) -> String {
/// an indeterminate moving bar and a Cancel button (Escape); never two modals at once.
#[test]
fn a_file_that_takes_a_while_to_place_shows_a_progress_window() {
let hold = Arc::new(std::sync::atomic::AtomicBool::new(true));
let hold_read = hold.clone();
// Each read waits until `go` is dropped.
let (go, hold) = channel::<()>();
let hold = Mutex::new(hold);
let services = Services {
read: Some(Box::new(move |p: &str| {
while hold_read.load(std::sync::atomic::Ordering::SeqCst) {
std::thread::sleep(std::time::Duration::from_millis(5));
}
let _ = hold.lock().map(|hold| hold.recv());
std::fs::read(p).map_err(|e| e.to_string())
})),
..Default::default()
@@ -2046,7 +2006,7 @@ fn a_file_that_takes_a_while_to_place_shows_a_progress_window() {
assert!(texts.contains("Placing slow.png…") && texts.contains("waiting"), "{texts}");
// Release and finish
hold.store(false, std::sync::atomic::Ordering::SeqCst);
drop(go);
placed(&mut app);
}
@@ -2054,13 +2014,12 @@ fn a_file_that_takes_a_while_to_place_shows_a_progress_window() {
/// completely unmodified (no undo step added, no nodes added), and clears the status bar.
#[test]
fn cancelling_a_place_places_nothing() {
let hold = Arc::new(std::sync::atomic::AtomicBool::new(true));
let hold_read = hold.clone();
// Each read waits until `go` is dropped.
let (go, hold) = channel::<()>();
let hold = Mutex::new(hold);
let services = Services {
read: Some(Box::new(move |p: &str| {
while hold_read.load(std::sync::atomic::Ordering::SeqCst) {
std::thread::sleep(std::time::Duration::from_millis(5));
}
let _ = hold.lock().map(|hold| hold.recv());
std::fs::read(p).map_err(|e| e.to_string())
})),
..Default::default()
@@ -2091,7 +2050,7 @@ fn cancelling_a_place_places_nothing() {
assert!(!crate::menus::enabled(&app, "file.cancelPlace"));
// Let the background read finish and drain
hold.store(false, std::sync::atomic::Ordering::SeqCst);
drop(go);
read_finished(&app);
poll(&mut app);