use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::{Arc, Barrier};
use std::thread;
use mod_tempdir::TempDir;
const THREADS: usize = 256;
#[test]
fn concurrent_tempdir_new_yields_unique_paths() {
let barrier = Arc::new(Barrier::new(THREADS));
let mut handles = Vec::with_capacity(THREADS);
for _ in 0..THREADS {
let b = Arc::clone(&barrier);
handles.push(thread::spawn(move || {
b.wait();
TempDir::new().expect("TempDir::new failed under contention")
}));
}
let dirs: Vec<TempDir> = handles
.into_iter()
.map(|h| h.join().expect("worker thread panicked"))
.collect();
let paths: HashSet<PathBuf> = dirs.iter().map(|d| d.path().to_path_buf()).collect();
assert_eq!(
paths.len(),
THREADS,
"path collision under contention: {} unique paths for {THREADS} threads",
paths.len()
);
for d in &dirs {
assert!(
d.path().exists(),
"TempDir reports {:?} but the directory is not on disk",
d.path()
);
}
}