Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(engine): list running jobs before ended ones in jobs.list (#2395) (#2411)

Kaustubh Tripathi1a49942b635c
diff --git a/crates/engine/src/jobs.rs b/crates/engine/src/jobs.rs
index b469fc778..9b612e3c4 100644
--- a/crates/engine/src/jobs.rs
+++ b/crates/engine/src/jobs.rs
@@ -562,7 +562,7 @@ impl Session {
/// Running jobs followed by the last few that ended (newest last), for `jobs.list`.
pub fn jobs_with_recent(&self) -> Vec<JobInfo> {
- self.jobs.recent.iter().cloned().chain(self.jobs()).collect()
+ self.jobs().into_iter().chain(self.jobs.recent.iter().cloned()).collect()
}
/// The running job that locks document `doc`, if any.
diff --git a/crates/engine/src/jobs_tests.rs b/crates/engine/src/jobs_tests.rs
index 3dba16d0e..043aa9db7 100644
--- a/crates/engine/src/jobs_tests.rs
+++ b/crates/engine/src/jobs_tests.rs
@@ -237,6 +237,23 @@ fn a_running_job_locks_its_document_but_not_others() {
assert_eq!(s.documents()[0].history.past_len(), 3);
}
+#[test]
+fn jobs_list_puts_running_jobs_before_ended_ones() {
+ let mut s = session(64, 64);
+ let gate = Arc::new(std::sync::atomic::AtomicBool::new(true));
+ let done = gated_job(&mut s, &gate);
+ assert!(matches!(wait_event(&mut s, done).outcome, JobOutcome::Done(_)));
+ // A second document, so the next job doesn't wait on the first one's.
+ s.execute("file.new", json!({"width": 8, "height": 8})).unwrap();
+ let gate = Arc::new(std::sync::atomic::AtomicBool::new(false));
+ let running = gated_job(&mut s, &gate);
+ let r = s.execute("jobs.list", json!({})).unwrap();
+ let listed: Vec<_> = r["jobs"].as_array().unwrap().iter().map(|j| (j["id"].clone(), j["state"].clone())).collect();
+ assert_eq!(listed, [(json!(running.0), json!("running")), (json!(done.0), json!("done"))], "{r}");
+ gate.store(true, std::sync::atomic::Ordering::Relaxed);
+ wait_event(&mut s, running);
+}
+
#[test]
fn background_job_finishes_bookkeeping_on_edited_document_after_tab_switch() {
let mut s = session(64, 64);