1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
use starbase_sandbox::create_empty_sandbox; use starbase_utils::fs; use std::thread; use std::time::Duration; use std::time::Instant; mod fs_lock { use super::*; mod lock_directory { use super::*; #[test] fn all_wait() { let sandbox = create_empty_sandbox(); let dir = sandbox.path().join("dir"); let mut handles = vec![]; let start = Instant::now(); for i in 0..10 { let dir_clone = dir.clone(); handles.push(thread::spawn(move || { // Stagger thread::sleep(Duration::from_millis(i * 25)); let _lock = fs::lock_directory(dir_clone).unwrap(); thread::sleep(Duration::from_millis(250)); })); } for handle in handles { handle.join().unwrap(); } let elapsed = start.elapsed(); assert!(elapsed >= Duration::from_millis(2500)); } } mod lock_file { use super::*; #[test] fn all_wait() { let sandbox = create_empty_sandbox(); let file = sandbox.path().join(".lock"); let mut handles = vec![]; let start = Instant::now(); for i in 0..10 { let file_clone = file.clone(); handles.push(thread::spawn(move || { // Stagger thread::sleep(Duration::from_millis(i * 25)); let _lock = fs::lock_directory(file_clone).unwrap(); thread::sleep(Duration::from_millis(250)); })); } for handle in handles { handle.join().unwrap(); } let elapsed = start.elapsed(); assert!(elapsed >= Duration::from_millis(2500)); } } }