diff --git a/crates/tools/pbxgen-stress/tests/music_cartridge.rs b/crates/tools/pbxgen-stress/tests/music_cartridge.rs index 29f0a19..2ea1c57 100644 --- a/crates/tools/pbxgen-stress/tests/music_cartridge.rs +++ b/crates/tools/pbxgen-stress/tests/music_cartridge.rs @@ -1,25 +1,45 @@ use prometeu_drivers::bank_format::{decode_audio_bank, DecodedAudioBank}; use prometeu_host_headless::{AdvanceFrameOutcome, HeadlessHost}; use std::path::PathBuf; +use std::sync::atomic::{AtomicU64, Ordering}; struct Cartridge(PathBuf); impl Cartridge { fn new() -> Self { let unique = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_nanos(); - let path = std::env::temp_dir() - .join(format!("stress-console-music-{}-{unique}", std::process::id())); - std::fs::create_dir_all(&path).unwrap(); - std::fs::write(path.join("program.pbx"), pbxgen_stress::build_program()).unwrap(); - std::fs::write(path.join("manifest.json"), pbxgen_stress::manifest_json()).unwrap(); - std::fs::write(path.join("assets.pa"), pbxgen_stress::build_assets_pack().unwrap()) + Self::with_timestamp(unique) + } + + fn with_timestamp(timestamp: u128) -> Self { + static NEXT_ID: AtomicU64 = AtomicU64::new(0); + let path = loop { + let id = NEXT_ID.fetch_add(1, Ordering::Relaxed); + let candidate = std::env::temp_dir() + .join(format!("stress-console-music-{}-{timestamp}-{id}", std::process::id())); + match std::fs::create_dir(&candidate) { + Ok(()) => break candidate, + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => continue, + Err(error) => panic!("create cartridge directory {candidate:?}: {error}"), + } + }; + let cartridge = Self(path); + std::fs::write(cartridge.0.join("program.pbx"), pbxgen_stress::build_program()).unwrap(); + std::fs::write(cartridge.0.join("manifest.json"), pbxgen_stress::manifest_json()).unwrap(); + std::fs::write(cartridge.0.join("assets.pa"), pbxgen_stress::build_assets_pack().unwrap()) .unwrap(); - Self(path) + cartridge } fn boot(&self) -> HeadlessHost { let mut host = HeadlessHost::new(); - host.boot(self.0.to_string_lossy()).unwrap(); + if let Err(error) = host.boot(self.0.to_string_lossy()) { + panic!( + "boot cartridge {:?}: {error:?}; logs: {:?}", + self.0, + host.log_snapshot(None, 128) + ); + } assert!(matches!( host.advance_frame().unwrap(), AdvanceFrameOutcome::FrameCompleted { .. } @@ -34,6 +54,19 @@ impl Drop for Cartridge { } } +#[test] +fn equal_timestamps_keep_cartridge_lifetimes_independent() { + let (first, second) = std::thread::scope(|scope| { + let first = scope.spawn(|| Cartridge::with_timestamp(0)); + let second = scope.spawn(|| Cartridge::with_timestamp(0)); + (first.join().unwrap(), second.join().unwrap()) + }); + assert_ne!(first.0, second.0); + drop(first); + drop(second.boot()); + drop(second.boot()); +} + #[test] fn bank_preserves_transcription_and_mixed_samples() { let bytes = include_bytes!("../music/storm-eagle.mbnk");