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