mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Tests: wait for the other thread's state instead of a fixed sleep (#15379)
- automation server: cancelled_queued_commands_never_start waits until both starts hold the session and wait for its lock before cancelling, and until both have returned before it inspects the document. - mcp_wait: the cancelled batch is checked after the connection closed, which waits for every running handler to return. - psd_gate: the gate tests wait until the second thread has checked its interrupt inside the gate's wait loop. - single_instance: the primary shuts down once the waiting launch has queued its path. - gpu_device_loss: poll for the lost device every 10 ms instead of every 250 ms.
This commit is contained in:
1 parent
01b79958a7
commit
19a42910cd
5 files changed
+67
-25
No files matched your search
@@ -126,7 +126,12 @@ fn a_launch_waiting_on_a_closing_instance_takes_over() {
|
||||
let primary = primary(&dir);
|
||||
// The UI never drains (it is shutting down, or never came up).
|
||||
let waiting = launch(&dir, &["a.psd"], FAST);
|
||||
std::thread::sleep(Duration::from_millis(200));
|
||||
// Shut down only once the launch has handed its path over and waits for the UI to take it.
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
while primary.shared.inbox.lock().unwrap().pending.is_empty() {
|
||||
assert!(Instant::now() < deadline, "the launch never queued its path");
|
||||
std::thread::sleep(Duration::from_millis(1));
|
||||
}
|
||||
primary.shutdown();
|
||||
assert!(matches!(waiting.join().unwrap(), Launch::Primary(_)), "the waiting launch becomes the primary");
|
||||
// After shutdown new connections are refused too.
|
||||
|
||||
@@ -1565,14 +1565,26 @@ mod tests {
|
||||
let before = mcp.headless_op(|h| h.inspect(None)).await.unwrap().unwrap();
|
||||
let Backend::Headless(session) = &*mcp.backend else { panic!("headless") };
|
||||
let session = session.clone();
|
||||
// A call holds clones of the session until it returns. A waited start holds three while
|
||||
// its blocking closure waits for the lock: `step`'s, `headless_wait`'s target, and
|
||||
// `headless_op`'s, taken last, right before the closure is queued.
|
||||
let idle = Arc::strong_count(&session);
|
||||
let held = session.clone();
|
||||
let (locked, acquired) = std::sync::mpsc::channel();
|
||||
let (release, released) = std::sync::mpsc::channel();
|
||||
let holder = std::thread::spawn(move || {
|
||||
let _held = session.lock().unwrap();
|
||||
let _held = held.lock().unwrap();
|
||||
locked.send(()).unwrap();
|
||||
released.recv().unwrap();
|
||||
});
|
||||
acquired.recv().unwrap();
|
||||
async fn until(what: &str, done: impl Fn() -> bool) {
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
|
||||
while !done() {
|
||||
assert!(std::time::Instant::now() < deadline, "timed out waiting until {what}");
|
||||
tokio::time::sleep(std::time::Duration::from_millis(5)).await;
|
||||
}
|
||||
}
|
||||
|
||||
let (mut input, server_in) = tokio::io::duplex(65536);
|
||||
let (server_out, output) = tokio::io::duplex(65536);
|
||||
@@ -1591,7 +1603,8 @@ mod tests {
|
||||
)
|
||||
.await;
|
||||
}
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
// The holder's clone, and three for each start.
|
||||
until("both starts wait for the lock", || Arc::strong_count(&session) >= idle + 1 + 2 * 3).await;
|
||||
for id in [2, 3] {
|
||||
send(&mut input, json!({"jsonrpc":"2.0", "method":"notifications/cancelled", "params":{"requestId":id}})).await;
|
||||
}
|
||||
@@ -1604,8 +1617,9 @@ mod tests {
|
||||
}
|
||||
release.send(()).unwrap();
|
||||
holder.join().unwrap();
|
||||
// Let both blocked starts acquire the mutex before inspecting their effects.
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
// Both blocked starts took the lock and returned (their replies are dropped, as cancelled)
|
||||
// before their effects are inspected.
|
||||
until("both starts returned", || Arc::strong_count(&session) == idle).await;
|
||||
let (after, jobs) = mcp.headless_op(|h| Ok((h.inspect(None)?, h.session.jobs_with_recent()))).await.unwrap().unwrap();
|
||||
assert_eq!(after, before);
|
||||
assert!(jobs.is_empty(), "cancelled queued job started: {jobs:?}");
|
||||
|
||||
@@ -149,7 +149,8 @@ async fn a_waited_command_reports_progress_serves_other_calls_and_cancels_cleanl
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn cancelling_a_batch_cancels_its_waited_step_and_runs_no_later_step() {
|
||||
let mut wire = Wire::start(PhotocraftMcp::headless()).await;
|
||||
let mcp = PhotocraftMcp::headless();
|
||||
let mut wire = Wire::start(mcp.clone()).await;
|
||||
wire.big_document().await;
|
||||
let before = wire.history(4).await;
|
||||
|
||||
@@ -158,8 +159,11 @@ async fn cancelling_a_batch_cancels_its_waited_step_and_runs_no_later_step() {
|
||||
assert!(wire.first_progress(5, "batch", 2.0).await < 1.0, "still in the first step");
|
||||
wire.cancel(5).await;
|
||||
wire.settle(6, 5).await;
|
||||
// Give a (wrongly) continuing batch time to start its next step.
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
// The cancelled batch never answers, so its end is not visible on the wire. Closing the
|
||||
// connection waits for every running handler to return, the batch included: after that a
|
||||
// (wrongly) continuing batch has run its next step.
|
||||
wire.finish().await;
|
||||
let mut wire = Wire::start(mcp).await;
|
||||
assert_eq!(wire.history(1000).await, before, "neither the blur nor the later step ran");
|
||||
wire.finish().await;
|
||||
}
|
||||
|
||||
+31
-14
@@ -139,7 +139,7 @@ pub(crate) fn acquire_decode_admission(ctl: &photocraft_raster::Interrupt) -> Re
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
|
||||
#[test]
|
||||
fn reentrant_admission_does_not_deadlock() {
|
||||
@@ -201,29 +201,40 @@ mod tests {
|
||||
assert_eq!(active_attempt_remaining(), Some(1));
|
||||
}
|
||||
|
||||
/// Wait (at most 10 s) until a thread waiting at the gate has checked its interrupt `n`
|
||||
/// times. It checks once on every pass of the wait loop, which it only enters while another
|
||||
/// thread holds admission, so the count says it is waiting there rather than not there yet.
|
||||
fn until_checked(checks: &AtomicUsize, n: usize) {
|
||||
let deadline = std::time::Instant::now() + Duration::from_secs(10);
|
||||
while checks.load(Ordering::SeqCst) < n {
|
||||
assert!(std::time::Instant::now() < deadline, "the second thread never waited at the gate");
|
||||
std::thread::sleep(Duration::from_millis(1));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gate_serializes_concurrent_threads() {
|
||||
let ctl = photocraft_raster::Interrupt::NONE;
|
||||
let guard = acquire_decode_admission(&ctl).unwrap();
|
||||
|
||||
let thread_started = Arc::new(AtomicBool::new(false));
|
||||
let checks = Arc::new(AtomicUsize::new(0));
|
||||
let thread_acquired = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let t_started = Arc::clone(&thread_started);
|
||||
let t_checks = Arc::clone(&checks);
|
||||
let t_acquired = Arc::clone(&thread_acquired);
|
||||
let handle = std::thread::spawn(move || {
|
||||
let ctl = photocraft_raster::Interrupt::NONE;
|
||||
t_started.store(true, Ordering::SeqCst);
|
||||
let counted = || {
|
||||
t_checks.fetch_add(1, Ordering::SeqCst);
|
||||
false
|
||||
};
|
||||
let ctl = photocraft_raster::Interrupt::cancel_only(&counted);
|
||||
let g = acquire_decode_admission(&ctl).unwrap();
|
||||
t_acquired.store(true, Ordering::SeqCst);
|
||||
drop(g);
|
||||
});
|
||||
|
||||
while !thread_started.load(Ordering::SeqCst) {
|
||||
std::thread::yield_now();
|
||||
}
|
||||
// Give background thread time to reach condvar wait
|
||||
std::thread::sleep(Duration::from_millis(60));
|
||||
// Two passes of the wait loop: the thread has waited on the condvar at least once.
|
||||
until_checked(&checks, 2);
|
||||
assert!(!thread_acquired.load(Ordering::SeqCst), "second thread must block while guard held");
|
||||
|
||||
drop(guard);
|
||||
@@ -236,19 +247,25 @@ mod tests {
|
||||
let ctl = photocraft_raster::Interrupt::NONE;
|
||||
let guard = acquire_decode_admission(&ctl).unwrap();
|
||||
|
||||
let checks = Arc::new(AtomicUsize::new(0));
|
||||
let cancelled = Arc::new(AtomicBool::new(false));
|
||||
let c_flag = Arc::clone(&cancelled);
|
||||
|
||||
let flag_for_thread = Arc::clone(&cancelled);
|
||||
let (flag_for_thread, t_checks) = (Arc::clone(&cancelled), Arc::clone(&checks));
|
||||
let handle = std::thread::spawn(move || {
|
||||
let cancelled = || flag_for_thread.load(Ordering::SeqCst);
|
||||
// Read the flag before counting: once the count moves, that check has said "go on".
|
||||
let cancelled = || {
|
||||
let cancelled = flag_for_thread.load(Ordering::SeqCst);
|
||||
t_checks.fetch_add(1, Ordering::SeqCst);
|
||||
cancelled
|
||||
};
|
||||
let progress = |_: f32| {};
|
||||
let interrupt = photocraft_raster::Interrupt::new(&cancelled, &progress);
|
||||
acquire_decode_admission(&interrupt).map(drop)
|
||||
});
|
||||
|
||||
// Let thread block on condvar
|
||||
std::thread::sleep(Duration::from_millis(60));
|
||||
// The thread is in the wait loop, blocked behind the guard.
|
||||
until_checked(&checks, 1);
|
||||
// Cancel waiting thread
|
||||
c_flag.store(true, Ordering::SeqCst);
|
||||
|
||||
|
||||
@@ -335,12 +335,14 @@ fn a_lost_window_device_closes_only_once_unsaved_work_is_saved() {
|
||||
};
|
||||
h.run_steps(4);
|
||||
assert!(h.state_mut().destroy_window_device());
|
||||
for _ in 0..120 {
|
||||
// wgpu reports the loss from its own thread: poll for it, often, for up to 30 s.
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
|
||||
loop {
|
||||
h.run_steps(1);
|
||||
if h.state().window_lost() {
|
||||
if h.state().window_lost() || std::time::Instant::now() >= deadline {
|
||||
break;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(250));
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
assert!(h.state().window_lost(), "the loss was noticed");
|
||||
let tries = || attempts.load(std::sync::atomic::Ordering::SeqCst);
|
||||
|
||||
Reference in new issue
Block a user