From f4027f583182b2d9ec30818cb381dcc2ba9bf10a Mon Sep 17 00:00:00 2001 From: yihuaf Date: Thu, 29 Jul 2021 21:55:54 +0200 Subject: [PATCH] adds unit test --- src/process/fork.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/process/fork.rs b/src/process/fork.rs index b7a4a760e..576ac7c77 100644 --- a/src/process/fork.rs +++ b/src/process/fork.rs @@ -120,6 +120,7 @@ pub fn clone(cb: CloneCb, clone_flags: sched::CloneFlags) -> Result { mod tests { use super::*; use anyhow::bail; + use nix::sys::wait; use nix::unistd; #[test] @@ -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 = (0..101).into_iter().collect(); + Box::new(move || { + assert_eq!(numbers.iter().sum::(), 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(()) + } }