diff --git a/crates/engine/src/tests/project_release.rs b/crates/engine/src/tests/project_release.rs index 490bb6576..c8ba5f9ea 100644 --- a/crates/engine/src/tests/project_release.rs +++ b/crates/engine/src/tests/project_release.rs @@ -61,3 +61,83 @@ fn replacing_project_releases_old_footage() { assert!(recorded().contains(&"/old.mp4".to_string()), "{:?}", recorded()); assert!(s.drain_events().contains(&Event::PurgeCaches)); } + +/// A footage source with a memory cache, like the media pool: a frame read keeps its file's +/// entry until the file is forgotten. With a gate, the first read waits for the test. +#[derive(Default)] +struct Caching { + cached: Mutex>, + gate: Mutex, std::sync::mpsc::Receiver<()>)>>, +} + +impl FootageSource for Caching { + fn frame(&self, _: ItemId, footage: &Footage, _: Tick) -> Option> { + let gate = self.gate.lock().unwrap().take(); + if let Some((entered, go)) = gate { + entered.send(()).unwrap(); + go.recv().unwrap(); + } + self.cached.lock().unwrap().insert(footage.path.clone()); + Some(Arc::new(Image::new(footage.width, footage.height))) + } + fn forget(&self, path: &str) { + self.cached.lock().unwrap().remove(path); + } +} + +/// A session showing `/old.png` (a footage item, and a comp with a layer of it) through a +/// [`Caching`] source: (session, source, footage item, comp). +fn old_project() -> (Session, Arc, ItemId, ItemId) { + let mut s = Session::default(); + let src = Arc::new(Caching::default()); + s.footage = src.clone(); + let still = Footage { path: "/old.png".into(), has_video: true, width: 8, height: 8, ..Default::default() }; + let mut p = Project::default(); + let item = p.add_item("old", Default::default(), None, ItemKind::Footage(still)); + s.replace_project(p, None); + s.execute("comp.new", json!({"name": "Main", "width": 8, "height": 8, "frameRate": 30, "duration": 1})).unwrap(); + s.execute("layer.addItem", json!({"item": item.0})).unwrap(); + let comp = s.active_comp_id().unwrap(); + (s, src, item, comp) +} + +#[test] +fn held_thumbnail_jobs_cannot_refill_the_closed_projects_caches() { + for close in ["file.closeProject", "file.newProject"] { + let (mut s, src, item, comp) = old_project(); + // Project panel thumbnails, made while the project is open and run after it closed (a + // thread that started late). + let jobs = [s.thumbnail_job(item, Tick(0), 16).unwrap(), s.thumbnail_job(comp, Tick(0), 16).unwrap()]; + s.execute(close, json!({})).unwrap(); + let shown: Vec<_> = jobs.into_iter().map(|job| job()).collect(); + assert!(src.cached.lock().unwrap().is_empty(), "{close}: the closed project's frames came back"); + assert_eq!(s.layer_cache.stats().entries, 0, "{close}: the closed project's layers came back"); + assert!(shown.iter().all(Option::is_none), "{close}: a closed project's thumbnail isn't shown"); + } +} + +#[test] +fn a_thumbnail_reading_through_close_lets_go_of_what_it_read() { + let (mut s, src, item, _) = old_project(); + let (entered, entered_rx) = std::sync::mpsc::channel(); + let (go, go_rx) = std::sync::mpsc::channel(); + *src.gate.lock().unwrap() = Some((entered, go_rx)); + let job = s.thumbnail_job(item, Tick(0), 16).unwrap(); + let running = std::thread::spawn(job); + // The job is decoding the closed project's still when the project closes. + entered_rx.recv().unwrap(); + s.execute("file.closeProject", json!({})).unwrap(); + go.send(()).unwrap(); + let shown = running.join().unwrap(); + assert!(src.cached.lock().unwrap().is_empty(), "the frame decoded through Close was kept"); + assert!(shown.is_none(), "a closed project's thumbnail isn't shown"); +} + +#[test] +fn an_open_projects_thumbnails_still_render() { + let (s, src, item, comp) = old_project(); + let (w, h, px) = s.thumbnail_job(item, Tick(0), 16).unwrap()().unwrap(); + assert_eq!((w, h, px.len()), (8, 8, 8 * 8 * 4)); + assert!(s.thumbnail_job(comp, Tick(0), 16).unwrap()().is_some()); + assert!(src.cached.lock().unwrap().contains("/old.png")); +}