Skip to content

Commit

Permalink
adds unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuaf committed Jul 30, 2021
1 parent bd54128 commit f4027f5
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/process/fork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub fn clone(cb: CloneCb, clone_flags: sched::CloneFlags) -> Result<Pid> {
mod tests {
use super::*;
use anyhow::bail;
use nix::sys::wait;
use nix::unistd;

#[test]
Expand Down Expand Up @@ -186,4 +187,27 @@ mod tests {

bail!("Process didn't exit correctly")
}

fn clone_closure_ownership_test_payload() -> super::CloneCb {
// The vec should not be deallocated after this function returns. The
// ownership should correctly transfer to the closure returned, to be
// passed to the clone and new child process.
let numbers: Vec<i32> = (0..101).into_iter().collect();
Box::new(move || {
assert_eq!(numbers.iter().sum::<i32>(), 5050);
0
})
}

#[test]
fn test_clone_closure_ownership() -> Result<()> {
let flags = sched::CloneFlags::empty();

let pid = super::clone(clone_closure_ownership_test_payload(), flags)?;
let exit_status =
wait::waitpid(pid, Some(wait::WaitPidFlag::__WALL)).expect("Waiting for child");
assert_eq!(exit_status, wait::WaitStatus::Exited(pid, 0));

Ok(())
}
}

0 comments on commit f4027f5

Please sign in to comment.