aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-30 03:12:03 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-30 03:13:37 -0500
commit757cc7b4e5223caf6092d014b929955daa6623a4 (patch)
tree426e27475a6b3aa67b9b5b77fd883779397d4c47 /tests
parent909e843a8abf9666f98f8ec62a0ef0f93c2738fc (diff)
downloadpty-process-757cc7b4e5223caf6092d014b929955daa6623a4.tar.gz
pty-process-757cc7b4e5223caf6092d014b929955daa6623a4.zip
async tests
Diffstat (limited to 'tests')
-rw-r--r--tests/fds_async.rs84
-rw-r--r--tests/multiple.rs31
2 files changed, 115 insertions, 0 deletions
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<i32> {
+ (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);
+ });
+}