-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a tiny issue with BufWriter (#222)
* fix: tiny fix Signed-off-by: Anthony Griffon <[email protected]> * test: add buf_writter test Signed-off-by: Anthony Griffon <[email protected]> --------- Signed-off-by: Anthony Griffon <[email protected]>
- Loading branch information
Showing
2 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use monoio::{ | ||
io::{AsyncReadRent, AsyncWriteRent, BufReader, BufWriter, Splitable}, | ||
net::{TcpListener, TcpStream}, | ||
}; | ||
|
||
#[monoio::test_all] | ||
async fn ensure_buf_writter_write_properly() { | ||
let srv = TcpListener::bind("127.0.0.1:0").unwrap(); | ||
let addr = srv.local_addr().unwrap(); | ||
|
||
monoio::spawn(async move { | ||
let stream = TcpStream::connect(&addr).await.unwrap(); | ||
let (_, stream_write) = stream.into_split(); | ||
|
||
let mut buf_w = BufWriter::new(stream_write); | ||
assert!(buf_w.write(&[b'1']).await.0.is_ok()); | ||
assert!(buf_w.write(&[b'2']).await.0.is_ok()); | ||
assert!(buf_w.write(&[b'3']).await.0.is_ok()); | ||
assert!(buf_w.flush().await.is_ok()); | ||
}); | ||
|
||
let (stream, _) = srv.accept().await.unwrap(); | ||
let (rd, _) = stream.into_split(); | ||
|
||
let s: Vec<u8> = Vec::with_capacity(16); | ||
let mut buf = BufReader::new(rd); | ||
let (size, s) = buf.read(s).await; | ||
|
||
assert!(size.is_ok()); | ||
assert_eq!(s, b"123"); | ||
} |