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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 tests/fds_async.rs (limited to '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() +} -- cgit v1.2.3-54-g00ecf