From: Ian Jackson Date: Mon, 31 May 2021 13:15:35 +0000 (+0100) Subject: childio: tests: refactoring X-Git-Tag: otter-0.7.0~198 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=efb6454bf9bd923863e0b410dbbc609f86cb9637;p=otter.git childio: tests: refactoring Signed-off-by: Ian Jackson --- diff --git a/src/childio.rs b/src/childio.rs index 401db3ce..22059296 100644 --- a/src/childio.rs +++ b/src/childio.rs @@ -120,13 +120,19 @@ use super::*; #[test] fn t_cat() { - let c = Command::new("cat"); - let (mut w, mut r) = run_pair(c, "cat".into()).unwrap(); - assert_eq!( write!(w, "hi").unwrap(), () ); - assert_eq!( w.flush() .unwrap(), () ); - let mut buf = [0;10]; - assert_eq!( r.read(&mut buf).unwrap(), 2 ); - assert_eq!(&buf[0..2], b"hi"); + let setup = ||{ + let c = Command::new("cat"); + run_pair(c, "cat".into()).unwrap() + }; + + { + let (mut w, mut r) = setup(); + assert_eq!( write!(w, "hi").unwrap(), () ); + assert_eq!( w.flush() .unwrap(), () ); + let mut buf = [0;10]; + assert_eq!( r.read(&mut buf).unwrap(), 2 ); + assert_eq!(&buf[0..2], b"hi"); + } } #[test] @@ -151,9 +157,13 @@ fn t_false() { r.read(&mut buf).map(|_|()) }); - one(&|w, _r|{ - // make sure we lose the race and get EPIPE + let lose_race = |w: &mut Stdin| { w.child.lock().child.wait().unwrap(); + }; + + one(&|w, _r|{ + // make sure we will get EPIPE + lose_race(w); write!(w, "hi") }); }