#[cfg(unix)]
#[test]
fn test_fork_detection() {
let mut guard = forkguard::new();
assert!(!guard.detected_fork(), "initial check should be false");
let pid = unsafe { libc::fork() };
if pid == 0 {
if !guard.detected_fork() {
std::process::exit(1); }
if guard.detected_fork() {
std::process::exit(2); }
std::process::exit(0);
} else if pid > 0 {
let mut status = 0;
unsafe { libc::waitpid(pid, &mut status, 0) };
assert!(libc::WIFEXITED(status), "child should exit normally");
assert_eq!(
libc::WEXITSTATUS(status),
0,
"child should have detected the fork"
);
assert!(!guard.detected_fork(), "parent should not detect a fork");
} else {
panic!("fork failed");
}
}