Make the off-thread OK test deterministic (#3622)
The test could lose a race: with a zero wait the evaluation sometimes finished and applied before the check that the dialog was still computing. The evaluation is now held at a test gate until the dialog has been seen saying so. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4dcca282da
commit
db75b7bfac
3 files changed
+15
-7
No files matched your search
@@ -43,15 +43,12 @@ fn until_applied(h: &mut Harness) {
|
||||
fn ok_computes_off_the_window_thread_saying_so_then_applies() {
|
||||
let mut h = square_on_a_block();
|
||||
h.app.apply_off_thread = Some(Duration::ZERO);
|
||||
// The evaluation is held until the dialog has been seen saying so (it is in the cache all
|
||||
// the same), so no frame can apply it before the checks below.
|
||||
let (finish, gate) = std::sync::mpsc::channel();
|
||||
h.app.apply_gate = Some(std::sync::Arc::new(std::sync::Mutex::new(gate)));
|
||||
h.step(&json!({"key": "Enter"})).unwrap();
|
||||
assert!(h.app.applying.is_some() && h.app.dialog.is_some(), "computing, the dialog open");
|
||||
// The evaluation may be in by the next frame: it is held until the dialog has been seen
|
||||
// saying so (the evaluation itself is in the cache all the same).
|
||||
let (finish, done) = std::sync::mpsc::channel();
|
||||
if let Some(job) = h.app.applying.as_mut() {
|
||||
let _ = job.done.recv();
|
||||
job.done = done;
|
||||
}
|
||||
h.frames(1);
|
||||
assert!(h.widget("Computing… 0 s").is_some(), "the dialog says it computes");
|
||||
// Enter again does not start it twice.
|
||||
|
||||
@@ -451,10 +451,16 @@ fn begin_applying(app: &SolveApp, d: &Dialog) -> Option<Applying> {
|
||||
let scratch = app.session.scratch();
|
||||
let (tx, rx) = std::sync::mpsc::channel();
|
||||
let computed = cmds.clone();
|
||||
#[cfg(test)]
|
||||
let gate = app.apply_gate.clone();
|
||||
std::thread::Builder::new()
|
||||
.name("apply".into())
|
||||
.spawn(move || {
|
||||
let _ = scratch.preview(&computed);
|
||||
#[cfg(test)]
|
||||
if let Some(gate) = gate {
|
||||
let _ = gate.lock().unwrap().recv();
|
||||
}
|
||||
let _ = tx.send(());
|
||||
})
|
||||
.ok()?;
|
||||
|
||||
@@ -384,6 +384,9 @@ 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<std::time::Duration>,
|
||||
/// Tests: the off-thread evaluation waits for this before it reports done.
|
||||
#[cfg(test)]
|
||||
pub(crate) apply_gate: Option<std::sync::Arc<std::sync::Mutex<std::sync::mpsc::Receiver<()>>>>,
|
||||
/// The last command started interactively (id, label), for Repeat.
|
||||
pub last_command: Option<(String, String)>,
|
||||
/// Esc was already acted on this frame (by the shortcuts), so later handlers skip it.
|
||||
@@ -512,6 +515,8 @@ impl SolveApp {
|
||||
deferred_queue: Vec::new(),
|
||||
delete_check: None,
|
||||
apply_off_thread: None,
|
||||
#[cfg(test)]
|
||||
apply_gate: None,
|
||||
last_command: None,
|
||||
esc_handled: false,
|
||||
status: None,
|
||||
|
||||
Reference in new issue
Block a user