summaryrefslogtreecommitdiffstats
path: root/vendor/gix-features/tests/pipe.rs
blob: 4815000bf9f8af649793ae40b53e1b28e9754a6e (plain)
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
mod io {
    use std::io::{BufRead, ErrorKind, Read, Write};

    use gix_features::io;

    #[test]
    fn threaded_read_to_end() {
        let (mut writer, mut reader) = gix_features::io::pipe::unidirectional(0);

        let message = "Hello, world!";
        std::thread::spawn(move || {
            writer
                .write_all(message.as_bytes())
                .expect("writes to work if reader is present")
        });

        let mut received = String::new();
        reader.read_to_string(&mut received).unwrap();

        assert_eq!(&received, message);
    }

    #[test]
    fn lack_of_reader_fails_with_broken_pipe() {
        let (mut writer, _) = io::pipe::unidirectional(None);
        assert_eq!(
            writer.write_all(b"must fail").unwrap_err().kind(),
            ErrorKind::BrokenPipe
        );
    }
    #[test]
    fn line_reading_one_by_one() {
        let (mut writer, mut reader) = io::pipe::unidirectional(2);
        writer.write_all(b"a\n").expect("success");
        writer.write_all(b"b\nc").expect("success");
        drop(writer);
        let mut buf = String::new();
        for expected in &["a\n", "b\n", "c"] {
            buf.clear();
            assert_eq!(reader.read_line(&mut buf).expect("success"), expected.len());
            assert_eq!(buf, *expected);
        }
    }

    #[test]
    fn line_reading() {
        let (mut writer, reader) = io::pipe::unidirectional(2);
        writer.write_all(b"a\n").expect("success");
        writer.write_all(b"b\nc\n").expect("success");
        drop(writer);
        assert_eq!(
            reader.lines().flat_map(Result::ok).collect::<Vec<_>>(),
            vec!["a", "b", "c"]
        )
    }

    #[test]
    fn writer_can_inject_errors() {
        let (writer, mut reader) = io::pipe::unidirectional(1);
        writer
            .channel
            .send(Err(std::io::Error::new(std::io::ErrorKind::Other, "the error")))
            .expect("send success");
        let mut buf = [0];
        assert_eq!(
            reader.read(&mut buf).unwrap_err().to_string(),
            "the error",
            "using Read trait, errors are propagated"
        );

        writer
            .channel
            .send(Err(std::io::Error::new(std::io::ErrorKind::Other, "the error")))
            .expect("send success");
        assert_eq!(
            reader.fill_buf().unwrap_err().to_string(),
            "the error",
            "using BufRead trait, errors are propagated"
        );
    }

    #[test]
    fn continue_on_empty_writes() {
        let (mut writer, mut reader) = io::pipe::unidirectional(2);
        writer.write_all(&[]).expect("write successful and non-blocking");
        let input = b"hello";
        writer
            .write_all(input)
            .expect("second write works as well as there is capacity");
        let mut buf = vec![0u8; input.len()];
        assert_eq!(reader.read(&mut buf).expect("read succeeds"), input.len());
        assert_eq!(buf, &input[..]);
    }

    #[test]
    fn small_reads() {
        const BLOCK_SIZE: usize = 20;
        let block_count = 20;
        let (mut writer, mut reader) = io::pipe::unidirectional(Some(4));
        std::thread::spawn(move || {
            for _ in 0..block_count {
                let data = &[0; BLOCK_SIZE];
                writer.write_all(data).unwrap();
            }
        });

        let mut small_read_buf = [0; BLOCK_SIZE / 2];
        let mut bytes_read = 0;
        while let Ok(size) = reader.read(&mut small_read_buf) {
            if size == 0 {
                break;
            }
            bytes_read += size;
        }
        assert_eq!(block_count * BLOCK_SIZE, bytes_read);
    }
}