1 | #![warn (rust_2018_idioms)] |
2 | #![cfg (all(feature = "full" , not(target_os = "wasi" )))] // Wasi doesn't support bind |
3 | |
4 | use tokio::io::AsyncReadExt; |
5 | use tokio::net::TcpStream; |
6 | |
7 | use tokio_test::assert_ok; |
8 | |
9 | use std::thread; |
10 | use std::{io::Write, net}; |
11 | |
12 | #[tokio::test ] |
13 | async fn peek() { |
14 | let listener = net::TcpListener::bind("127.0.0.1:0" ).unwrap(); |
15 | let addr = listener.local_addr().unwrap(); |
16 | let t = thread::spawn(move || assert_ok!(listener.accept()).0); |
17 | |
18 | let left = net::TcpStream::connect(addr).unwrap(); |
19 | let mut right = t.join().unwrap(); |
20 | let _ = right.write(&[1, 2, 3, 4]).unwrap(); |
21 | |
22 | let mut left: TcpStream = left.try_into().unwrap(); |
23 | let mut buf = [0u8; 16]; |
24 | let n = assert_ok!(left.peek(&mut buf).await); |
25 | assert_eq!([1, 2, 3, 4], buf[..n]); |
26 | |
27 | let n = assert_ok!(left.read(&mut buf).await); |
28 | assert_eq!([1, 2, 3, 4], buf[..n]); |
29 | } |
30 | |