aboutsummaryrefslogtreecommitdiffstats
path: root/tests/fds_async.rs
blob: e2c369dd1e66f9ca6f183827fae8bf2d711649ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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()
}