| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#![allow(dead_code)] |
| 7 |
|
| 8 |
use std::{ |
| 9 |
env, fs, |
| 10 |
path::{Path, PathBuf}, |
| 11 |
sync::atomic::{AtomicU64, Ordering}, |
| 12 |
thread, |
| 13 |
time::{Duration, Instant}, |
| 14 |
}; |
| 15 |
|
| 16 |
static TEST_COUNTER: AtomicU64 = AtomicU64::new(0); |
| 17 |
|
| 18 |
|
| 19 |
pub fn get_unique_test_dir(test_name: &str) -> PathBuf { |
| 20 |
let counter = TEST_COUNTER.fetch_add(1, Ordering::SeqCst); |
| 21 |
env::temp_dir().join(format!("fork_test_{}_{}", test_name, counter)) |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
pub fn get_test_dir(prefix: &str) -> PathBuf { |
| 26 |
env::temp_dir().join(format!("fork_test_{}", prefix)) |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
pub fn setup_test_dir(path: PathBuf) -> PathBuf { |
| 31 |
let _ = fs::remove_dir_all(&path); |
| 32 |
fs::create_dir_all(&path).expect("Failed to create test directory"); |
| 33 |
path |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
pub fn wait_for_file(path: &Path, timeout_ms: u64) -> bool { |
| 38 |
let start = Instant::now(); |
| 39 |
while start.elapsed().as_millis() < u128::from(timeout_ms) { |
| 40 |
if path.exists() { |
| 41 |
return true; |
| 42 |
} |
| 43 |
thread::sleep(Duration::from_millis(10)); |
| 44 |
} |
| 45 |
false |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
pub fn cleanup_test_dir(path: &Path) { |
| 50 |
let _ = fs::remove_dir_all(path); |
| 51 |
} |