From 757cc7b4e5223caf6092d014b929955daa6623a4 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 30 Dec 2021 03:12:03 -0500 Subject: async tests --- tests/fds_async.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/multiple.rs | 31 ++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 tests/fds_async.rs diff --git a/tests/fds_async.rs b/tests/fds_async.rs new file mode 100644 index 0000000..e2c369d --- /dev/null +++ b/tests/fds_async.rs @@ -0,0 +1,84 @@ +#[cfg(feature = "async")] +#[test] +fn test_fds_async() { + use async_std::io::prelude::BufReadExt as _; + + check_open_fds(&[0, 1, 2]); + + // run once to ensure all of the fds in the async_std machinery are + // allocated + async_std::task::block_on(async { + let pty = pty_process::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::Command::new("perl") + .arg("-Efor my $fd (0..255) { open my $fh, \"<&=$fd\"; print $fd if stat $fh }; say") + .spawn(&pty) + .unwrap(); + let mut buf = vec![]; + async_std::io::BufReader::new(&pty) + .read_until(b'\n', &mut buf) + .await + .unwrap(); + assert_eq!(&buf, b"012\r\n"); + let status = child.status().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + }); + + async_std::task::block_on(async { + let fds = get_open_fds(); + + let pty = pty_process::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::Command::new("perl") + .arg("-Efor my $fd (0..255) { open my $fh, \"<&=$fd\"; print $fd if stat $fh }; say") + .spawn(&pty) + .unwrap(); + let mut buf = vec![]; + async_std::io::BufReader::new(&pty) + .read_until(b'\n', &mut buf) + .await + .unwrap(); + assert_eq!(&buf, b"012\r\n"); + let status = child.status().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + drop(pty); + + check_open_fds(&fds); + }); + + async_std::task::block_on(async { + let fds = get_open_fds(); + + let pty = pty_process::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::Command::new("perl") + .arg("-Efor my $fd (0..255) { open my $fh, \"<&=$fd\"; print $fd if stat $fh }; say") + .stderr(Some(std::process::Stdio::null())) + .spawn(&pty) + .unwrap(); + let mut buf = vec![]; + async_std::io::BufReader::new(&pty) + .read_until(b'\n', &mut buf) + .await + .unwrap(); + assert_eq!(&buf, b"012\r\n"); + let status = child.status().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + drop(pty); + + check_open_fds(&fds); + }); +} + +#[cfg(feature = "async")] +#[track_caller] +fn check_open_fds(expected: &[i32]) { + assert_eq!(get_open_fds(), expected); +} + +#[cfg(feature = "async")] +fn get_open_fds() -> Vec { + (0..=255) + .filter(|fd| nix::sys::stat::fstat(*fd).is_ok()) + .collect() +} diff --git a/tests/multiple.rs b/tests/multiple.rs index 8d96050..7e27512 100644 --- a/tests/multiple.rs +++ b/tests/multiple.rs @@ -25,3 +25,34 @@ fn test_multiple() { let status = child.wait().unwrap(); assert_eq!(status.code().unwrap(), 0); } + +#[cfg(feature = "async")] +#[test] +fn test_multiple_async() { + use async_std::io::ReadExt as _; + + async_std::task::block_on(async { + let mut pty = pty_process::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + + let mut child = pty_process::Command::new("echo") + .arg("foo") + .spawn(&pty) + .unwrap(); + let mut buf = [0u8; 1024]; + let bytes = pty.read(&mut buf).await.unwrap(); + assert_eq!(&buf[..bytes], b"foo\r\n"); + let status = child.status().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + + let mut child = pty_process::Command::new("echo") + .arg("bar") + .spawn(&pty) + .unwrap(); + let mut buf = [0u8; 1024]; + let bytes = pty.read(&mut buf).await.unwrap(); + assert_eq!(&buf[..bytes], b"bar\r\n"); + let status = child.status().await.unwrap(); + assert_eq!(status.code().unwrap(), 0); + }); +} -- cgit v1.2.3-54-g00ecf