Commit 9a93e3db authored by Ian Jackson's avatar Ian Jackson 💬
Browse files

Merge branch 'fslock-guard-loop-test' into 'main'

fslock-guard: Add test case for tight acquire/drop loop

See merge request !4348
parents bbe4e1ac 2495ded7
Loading
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
@@ -476,4 +476,32 @@ mod tests {
            assert!(!file.try_exists().unwrap());
        });
    }

    #[test]
    fn tight_loop() {
        let tmp = std::sync::Arc::new(test_temp_dir!());

        // We make several threads in case there are any cross-thread interactions
        // that we're not aware of.  There shouldn't be.
        let threads = (0..10)
            .map(|i| {
                let tmp = tmp.clone();
                std::thread::spawn(move || {
                    tmp.used_by(|dir| {
                        let file = dir.join(format!("{i}"));
                        for _ in 0..1000 {
                            // Test that the lock is immediately re-requirable after drop.
                            let lock: LockFileGuard =
                                LockFileGuard::try_lock(&file).unwrap().unwrap();
                            drop(lock);
                        }
                    });
                })
            })
            .collect::<Vec<_>>();

        for t in threads {
            t.join().unwrap_or_else(|e| std::panic::resume_unwind(e));
        }
    }
}