File: tokio.rs

package info (click to toggle)
rust-flate2 1.0.13-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 548 kB
  • sloc: makefile: 2
file content (133 lines) | stat: -rw-r--r-- 3,896 bytes parent folder | download | duplicates (20)
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
#![cfg(feature = "tokio")]

extern crate flate2;
extern crate futures;
extern crate rand;
extern crate tokio_io;
extern crate tokio_tcp;
extern crate tokio_threadpool;

use std::io::{Read, Write};
use std::iter;
use std::net::{Shutdown, TcpListener};
use std::thread;

use flate2::read;
use flate2::write;
use flate2::Compression;
use futures::Future;
use rand::{thread_rng, Rng};
use tokio_io::io::{copy, shutdown};
use tokio_io::AsyncRead;
use tokio_tcp::TcpStream;

#[test]
fn tcp_stream_echo_pattern() {
    const N: u8 = 16;
    const M: usize = 16 * 1024;

    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    let t = thread::spawn(move || {
        let a = listener.accept().unwrap().0;
        let b = a.try_clone().unwrap();

        let t = thread::spawn(move || {
            let mut b = read::DeflateDecoder::new(b);
            let mut buf = [0; M];
            for i in 0..N {
                b.read_exact(&mut buf).unwrap();
                for byte in buf.iter() {
                    assert_eq!(*byte, i);
                }
            }

            assert_eq!(b.read(&mut buf).unwrap(), 0);
        });

        let mut a = write::ZlibEncoder::new(a, Compression::default());
        for i in 0..N {
            let buf = [i; M];
            a.write_all(&buf).unwrap();
        }
        a.finish().unwrap().shutdown(Shutdown::Write).unwrap();

        t.join().unwrap();
    });

    let stream = TcpStream::connect(&addr);
    let copy = stream
        .and_then(|s| {
            let (a, b) = s.split();
            let a = read::ZlibDecoder::new(a);
            let b = write::DeflateEncoder::new(b, Compression::default());
            copy(a, b)
        })
        .then(|result| {
            let (amt, _a, b) = result.unwrap();
            assert_eq!(amt, (N as u64) * (M as u64));
            shutdown(b).map(|_| ())
        })
        .map_err(|err| panic!("{}", err));

    let threadpool = tokio_threadpool::Builder::new().build();
    threadpool.spawn(copy);
    threadpool.shutdown().wait().unwrap();
    t.join().unwrap();
}

#[test]
fn echo_random() {
    let v = iter::repeat(())
        .take(1024 * 1024)
        .map(|()| thread_rng().gen::<u8>())
        .collect::<Vec<_>>();
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    let v2 = v.clone();
    let t = thread::spawn(move || {
        let a = listener.accept().unwrap().0;
        let b = a.try_clone().unwrap();

        let mut v3 = v2.clone();
        let t = thread::spawn(move || {
            let mut b = read::DeflateDecoder::new(b);
            let mut buf = [0; 1024];
            while v3.len() > 0 {
                let n = b.read(&mut buf).unwrap();
                for (actual, expected) in buf[..n].iter().zip(&v3) {
                    assert_eq!(*actual, *expected);
                }
                v3.drain(..n);
            }

            assert_eq!(b.read(&mut buf).unwrap(), 0);
        });

        let mut a = write::ZlibEncoder::new(a, Compression::default());
        a.write_all(&v2).unwrap();
        a.finish().unwrap().shutdown(Shutdown::Write).unwrap();

        t.join().unwrap();
    });

    let stream = TcpStream::connect(&addr);
    let copy = stream
        .and_then(|s| {
            let (a, b) = s.split();
            let a = read::ZlibDecoder::new(a);
            let b = write::DeflateEncoder::new(b, Compression::default());
            copy(a, b)
        })
        .then(move |result| {
            let (amt, _a, b) = result.unwrap();
            assert_eq!(amt, v.len() as u64);
            shutdown(b).map(|_| ())
        })
        .map_err(|err| panic!("{}", err));

    let threadpool = tokio_threadpool::Builder::new().build();
    threadpool.spawn(copy);
    threadpool.shutdown().wait().unwrap();
    t.join().unwrap();
}