Skip to content

FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust

WIN-02: Serialize shared codec test fixture publication

jahvarifbbb57d32548
diff --git a/crates/h264/tests/common/mod.rs b/crates/h264/tests/common/mod.rs
index ab75ea00e..e100c3289 100644
--- a/crates/h264/tests/common/mod.rs
+++ b/crates/h264/tests/common/mod.rs
@@ -4,6 +4,11 @@
use filmcraft_h264::{Decoder, Picture};
use std::path::{Path, PathBuf};
use std::process::Command;
+use std::sync::{Mutex, PoisonError};
+
+// Tests share fixture names within each binary. Serialize the existence check,
+// generation and publication so a late producer cannot replace a file in use.
+static FIXTURE_GENERATION: Mutex<()> = Mutex::new(());
/// One encoder configuration of the fixture matrix.
pub struct Fixture {
@@ -334,6 +339,7 @@ fn run(cmd: &mut Command) -> bool {
/// Returns None (with a message) when ffmpeg is unavailable.
pub fn ensure(f: &Fixture) -> Option<(PathBuf, PathBuf)> {
let ff = filmcraft_testkit::ffmpeg_or_skip(f.name)?;
+ let _generation = FIXTURE_GENERATION.lock().unwrap_or_else(PoisonError::into_inner);
let dir = fixtures_dir();
let h264 = dir.join(format!("{}.h264", f.name));
let yuv = dir.join(format!("{}.yuv", f.name));
diff --git a/crates/h264/tests/conformance.rs b/crates/h264/tests/conformance.rs
index 154bb8e95..c06694b79 100644
--- a/crates/h264/tests/conformance.rs
+++ b/crates/h264/tests/conformance.rs
@@ -148,3 +148,50 @@ fn generate_fixtures() {
filmcraft_testkit::fixtures::generate_and_report(&format!("h264/{}", f.name), &outs, || common::ensure(f));
}
}
+
+/// Concurrent consumers must publish one complete fixture without replacing an open file.
+#[test]
+fn concurrent_fixture_publication_is_safe() {
+ if filmcraft_testkit::ffmpeg_or_skip("concurrent_fixture_publication").is_none() {
+ return;
+ }
+ let stamp = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos();
+ let name = Box::leak(format!("concurrent_fixture_{}_{stamp}", std::process::id()).into_boxed_str());
+ let fixture =
+ common::Fixture { name, source: "testsrc2", width: 64, height: 48, frames: 2, filter: "", args: &["-preset", "ultrafast", "-x264-params", "keyint=1"] };
+ let open_published = |path: &std::path::Path| {
+ let mut options = std::fs::OpenOptions::new();
+ options.read(true);
+ #[cfg(windows)]
+ {
+ use std::os::windows::fs::OpenOptionsExt;
+ // Like the external decoder, allow reads but deny replacement while open.
+ options.share_mode(1);
+ }
+ options.open(path).unwrap()
+ };
+ let start = std::sync::Barrier::new(4);
+ let results = std::thread::scope(|scope| {
+ let jobs: Vec<_> = (0..4)
+ .map(|_| {
+ scope.spawn(|| {
+ start.wait();
+ let paths = common::ensure(&fixture).expect("software fixture must generate");
+ // Keep both published files open until every producer has finished.
+ let files = (open_published(&paths.0), open_published(&paths.1));
+ (paths, files)
+ })
+ })
+ .collect();
+ jobs.into_iter().map(|j| j.join().expect("fixture producer panicked")).collect::<Vec<_>>()
+ });
+ let paths = results[0].0.clone();
+ for (got, _) in &results {
+ assert_eq!(got, &paths);
+ }
+ assert!(!std::fs::read(&paths.0).unwrap().is_empty());
+ assert_eq!(std::fs::metadata(&paths.1).unwrap().len(), 2 * (64 * 48 + 2 * 32 * 24));
+ drop(results);
+ std::fs::remove_file(paths.0).unwrap();
+ std::fs::remove_file(paths.1).unwrap();
+}