forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplex_stream.rs
47 lines (35 loc) · 1.22 KB
/
duplex_stream.rs
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
#![warn(rust_2018_idioms)]
#![cfg(feature = "full")]
use std::io::IoSlice;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
const HELLO: &[u8] = b"hello world...";
#[tokio::test]
async fn write_vectored() {
let (mut client, mut server) = tokio::io::duplex(64);
let ret = client
.write_vectored(&[IoSlice::new(HELLO), IoSlice::new(HELLO)])
.await
.unwrap();
assert_eq!(ret, HELLO.len() * 2);
client.flush().await.unwrap();
drop(client);
let mut buf = Vec::with_capacity(HELLO.len() * 2);
let bytes_read = server.read_to_end(&mut buf).await.unwrap();
assert_eq!(bytes_read, HELLO.len() * 2);
assert_eq!(buf, [HELLO, HELLO].concat());
}
#[tokio::test]
async fn write_vectored_and_shutdown() {
let (mut client, mut server) = tokio::io::duplex(64);
let ret = client
.write_vectored(&[IoSlice::new(HELLO), IoSlice::new(HELLO)])
.await
.unwrap();
assert_eq!(ret, HELLO.len() * 2);
client.shutdown().await.unwrap();
drop(client);
let mut buf = Vec::with_capacity(HELLO.len() * 2);
let bytes_read = server.read_to_end(&mut buf).await.unwrap();
assert_eq!(bytes_read, HELLO.len() * 2);
assert_eq!(buf, [HELLO, HELLO].concat());
}