aboutsummaryrefslogtreecommitdiffstats
path: root/tests/split.rs
blob: f537f173b27ab84594849afdfd063a5489364d1a (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
mod helpers;

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

    let rt = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();
    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 cmd = pty_process::Command::new("perl");
        cmd.args(["-plE", "BEGIN { $SIG{WINCH} = sub { say 'WINCH' } }"]);
        let mut child = cmd.spawn(&pts).unwrap();

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

        {
            let (pty_r, mut pty_w) = pty.split();
            pty_w.write_all(b"foo\n").await.unwrap();
            let mut output = helpers::output_async(pty_r);
            assert_eq!(output.next().await.unwrap(), "foo\r\n");
            assert_eq!(output.next().await.unwrap(), "foo\r\n");
        }

        {
            let (pty_r, pty_w) = pty.split();
            pty_w.resize(pty_process::Size::new(25, 80)).unwrap();
            let mut output = helpers::output_async(pty_r);
            assert_eq!(output.next().await.unwrap(), "WINCH\r\n");
        }

        pty.write_all(&[4u8]).await.unwrap();
        child.wait().await.unwrap()
    });
}

#[cfg(feature = "async")]
#[test]
fn test_into_split() {
    use tokio::io::{AsyncBufReadExt as _, AsyncWriteExt as _};

    let rt = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();
    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 cmd = pty_process::Command::new("perl");
        cmd.args(["-plE", "BEGIN { $SIG{WINCH} = sub { say 'WINCH' } }"]);
        let mut child = cmd.spawn(&pts).unwrap();

        {
            pty.write_all(b"foo\n").await.unwrap();
            let (pty_r, pty_w) = pty.into_split();
            let mut ptybuf = tokio::io::BufReader::new(pty_r);
            for _ in 0..2 {
                let mut buf = vec![];
                tokio::time::timeout(
                    std::time::Duration::from_secs(5),
                    ptybuf.read_until(b'\n', &mut buf),
                )
                .await
                .unwrap()
                .unwrap();
                assert_eq!(&buf[..], b"foo\r\n");
            }
            pty = ptybuf.into_inner().unsplit(pty_w).unwrap();
        }

        {
            let (pty_r, mut pty_w) = pty.into_split();
            pty_w.write_all(b"foo\n").await.unwrap();
            let mut ptybuf = tokio::io::BufReader::new(pty_r);
            for _ in 0..2 {
                let mut buf = vec![];
                tokio::time::timeout(
                    std::time::Duration::from_secs(5),
                    ptybuf.read_until(b'\n', &mut buf),
                )
                .await
                .unwrap()
                .unwrap();
                assert_eq!(&buf[..], b"foo\r\n");
            }
            pty = ptybuf.into_inner().unsplit(pty_w).unwrap();
        }

        {
            let (pty_r, pty_w) = pty.into_split();
            pty_w.resize(pty_process::Size::new(25, 80)).unwrap();
            let mut ptybuf = tokio::io::BufReader::new(pty_r);
            let mut buf = vec![];
            tokio::time::timeout(
                std::time::Duration::from_secs(5),
                ptybuf.read_until(b'\n', &mut buf),
            )
            .await
            .unwrap()
            .unwrap();
            assert_eq!(&buf[..], b"WINCH\r\n");
            pty = ptybuf.into_inner().unsplit(pty_w).unwrap();
        }

        pty.write_all(&[4u8]).await.unwrap();
        child.wait().await.unwrap()
    });
}

#[cfg(feature = "async")]
#[test]
fn test_into_split_error() {
    let rt = tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()
        .unwrap();
    rt.block_on(async {
        let pty1 = pty_process::Pty::new().unwrap();
        let pty2 = pty_process::Pty::new().unwrap();

        let (pty1_r, pty1_w) = pty1.into_split();
        let (pty2_r, pty2_w) = pty2.into_split();

        let (pty1_r, pty2_w) = if let Err(pty_process::Error::Unsplit(r, w)) =
            pty1_r.unsplit(pty2_w)
        {
            (r, w)
        } else {
            panic!("fail");
        };
        let (pty2_r, pty1_w) = if let Err(pty_process::Error::Unsplit(r, w)) =
            pty2_r.unsplit(pty1_w)
        {
            (r, w)
        } else {
            panic!("fail");
        };

        let _pty1 = pty1_r.unsplit(pty1_w).unwrap();
        let _pty2 = pty2_r.unsplit(pty2_w).unwrap();
    });
}