File: pipe.rs

package info (click to toggle)
rust-sluice 0.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 128 kB
  • sloc: makefile: 2
file content (113 lines) | stat: -rw-r--r-- 2,671 bytes parent folder | download
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
use futures::{
    executor::block_on,
    join,
    prelude::*,
};
use quickcheck_macros::quickcheck;
use sluice::pipe::pipe;
use std::io;

#[test]
fn read_empty() {
    block_on(async {
        let (mut reader, writer) = pipe();
        drop(writer);

        let mut out = String::new();
        reader.read_to_string(&mut out).await.unwrap();
        assert_eq!(out, "");
    })
}

#[test]
fn read_then_write() {
    block_on(async {
        let (mut reader, mut writer) = pipe();

        writer.write_all(b"hello world").await.unwrap();

        let mut dest = [0; 6];

        assert_eq!(reader.read(&mut dest).await.unwrap(), 6);
        assert_eq!(&dest, b"hello ");

        assert_eq!(reader.read(&mut dest).await.unwrap(), 5);
        assert_eq!(&dest[..5], b"world");
    })
}

#[test]
fn reader_still_drainable_after_writer_disconnects() {
    block_on(async {
        let (mut reader, mut writer) = pipe();

        writer.write_all(b"hello").await.unwrap();

        drop(writer);

        let mut dest = [0; 5];
        assert_eq!(reader.read(&mut dest).await.unwrap(), 5);
        assert_eq!(&dest, b"hello");

        // Continue returning Ok(0) forever.
        for _ in 0..3 {
            assert_eq!(reader.read(&mut dest).await.unwrap(), 0);
        }
    })
}

#[test]
fn writer_errors_if_reader_is_dropped() {
    block_on(async {
        let (reader, mut writer) = pipe();

        drop(reader);

        for _ in 0..3 {
            assert_eq!(writer.write(b"hello").await.unwrap_err().kind(), io::ErrorKind::BrokenPipe);
        }
    })
}

#[test]
fn pipe_lots_of_data() {
    block_on(async {
        let data = vec![0xff; 1_000_000];
        let (mut reader, mut writer) = pipe();

        join!(
            async {
                writer.write_all(&data).await.unwrap();
                writer.close().await.unwrap();
            },
            async {
                let mut out = Vec::new();
                reader.read_to_end(&mut out).await.unwrap();
                assert_eq!(&out[..], &data[..]);
            },
        );
    })
}

#[quickcheck]
fn read_write_chunks_random(chunks: u8) {
    block_on(async {
        let data = [0; 8192];
        let (mut reader, mut writer) = pipe();

        join!(
            async {
                for _chunk in 0..chunks {
                    writer.write_all(&data).await.unwrap();
                }
            },
            async {
                for _chunk in 0..chunks {
                    let mut buf = data.clone();
                    reader.read(&mut buf).await.unwrap();
                    assert_eq!(&buf[..], &data[..]);
                }
            },
        );
    })
}