hyperactor/test_utils/
process_assertion.rs1use std::future::Future;
10
11use nix::sys::wait::WaitStatus;
12use nix::sys::wait::waitpid;
13use nix::unistd::ForkResult;
14use nix::unistd::fork;
15
16pub async fn assert_termination<F, Fut>(f: F, expected_code: i32) -> anyhow::Result<()>
19where
20 F: FnOnce() -> Fut,
21 Fut: Future<Output = ()>,
22{
23 unsafe {
25 match fork() {
26 Ok(ForkResult::Parent { child, .. }) => match waitpid(child, None)? {
27 WaitStatus::Exited(_, exit_code) => {
28 anyhow::ensure!(exit_code == expected_code);
29 Ok(())
30 }
31 status => Err(anyhow::anyhow!(
32 "didn't receive expected status. got: {:?}",
33 status
34 )),
35 },
36 Ok(ForkResult::Child) => Ok(f().await),
37 Err(_) => Err(anyhow::anyhow!("fork failed")),
38 }
39 }
40}