aboutsummaryrefslogtreecommitdiffstats
path: root/tests/fds_async.rs
blob: 6f3bcdef885e668c2f7069678929876d7d5d8cb7 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
mod helpers;

#[cfg(feature = "async")]
#[test]
fn test_fds_async() {
    use futures::stream::StreamExt as _;

    check_open_fds(&[0, 1, 2]);

    let rt = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();

    // run once to ensure all of the fds in the tokio machinery are
    // allocated
    rt.block_on(async {
        let mut pty = pty_process::Pty::new().unwrap();
        let pts = pty.pts().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(&pts)
            .unwrap();

        let (pty_r, _) = pty.split();
        let mut output = helpers::output_async(pty_r);
        assert_eq!(output.next().await.unwrap(), "012\r\n");

        let status = child.wait().await.unwrap();
        assert_eq!(status.code().unwrap(), 0);
    });

    rt.block_on(async {
        let fds = get_open_fds();

        let mut pty = pty_process::Pty::new().unwrap();
        let pts = pty.pts().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(&pts)
            .unwrap();

        let (pty_r, _) = pty.split();
        let mut output = helpers::output_async(pty_r);
        assert_eq!(output.next().await.unwrap(), "012\r\n");

        let status = child.wait().await.unwrap();
        assert_eq!(status.code().unwrap(), 0);
        drop(output);
        drop(pts);
        drop(pty);

        check_open_fds(&fds);
    });

    rt.block_on(async {
        let fds = get_open_fds();

        let mut pty = pty_process::Pty::new().unwrap();
        let pts = pty.pts().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(std::process::Stdio::null())
            .spawn(&pts)
            .unwrap();

        let (pty_r, _) = pty.split();
        let mut output = helpers::output_async(pty_r);
        assert_eq!(output.next().await.unwrap(), "012\r\n");

        let status = child.wait().await.unwrap();
        assert_eq!(status.code().unwrap(), 0);
        drop(output);
        drop(pts);
        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()
}