Skip to content

Commit

Permalink
Add tests for AsyncBufRead::read_line and AsyncBufReadExt::lines
Browse files Browse the repository at this point in the history
…corner cases.
  • Loading branch information
hkratz committed Sep 14, 2024
1 parent c507ff8 commit fd71f88
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
26 changes: 25 additions & 1 deletion futures/tests/io_lines.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use futures::executor::block_on;
use futures::future::{Future, FutureExt};
use futures::io::{AsyncBufReadExt, Cursor};
use futures::io::{AsyncBufReadExt, AsyncRead, Cursor};
use futures::stream::{self, StreamExt, TryStreamExt};
use futures::task::Poll;
use futures_test::io::AsyncReadTestExt;
Expand All @@ -27,6 +27,24 @@ macro_rules! run_next {
};
}

struct IOErrorRead(bool);

impl AsyncRead for IOErrorRead {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
b: &mut [u8],
) -> Poll<std::io::Result<usize>> {
if self.0 {
Poll::Ready(Err(std::io::ErrorKind::InvalidInput.into()))
} else {
self.0 = true;
b.fill(b'x');
Ok(b.len()).into()
}
}
}

#[test]
fn lines() {
let buf = Cursor::new(&b"12\r"[..]);
Expand Down Expand Up @@ -58,3 +76,9 @@ fn maybe_pending() {
assert_eq!(run_next!(s), "".to_string());
assert!(run(s.next()).is_none());
}

#[test]
fn issue2862() {
let mut lines = futures::io::BufReader::new(IOErrorRead(false)).lines();
assert!(block_on(lines.next()).unwrap().is_err())
}
43 changes: 43 additions & 0 deletions futures/tests/io_read_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use futures::future::{Future, FutureExt};
use futures::io::{AsyncBufReadExt, Cursor};
use futures::stream::{self, StreamExt, TryStreamExt};
use futures::task::Poll;
use futures::AsyncRead;
use futures_test::io::AsyncReadTestExt;
use futures_test::task::noop_context;

Expand All @@ -15,6 +16,24 @@ fn run<F: Future + Unpin>(mut f: F) -> F::Output {
}
}

struct IOErrorRead(bool);

impl AsyncRead for IOErrorRead {
fn poll_read(
mut self: std::pin::Pin<&mut Self>,
_cx: &mut std::task::Context<'_>,
b: &mut [u8],
) -> Poll<std::io::Result<usize>> {
if self.0 {
Poll::Ready(Err(std::io::ErrorKind::InvalidInput.into()))
} else {
self.0 = true;
b.fill(b'x');
Ok(b.len()).into()
}
}
}

#[test]
fn read_line() {
let mut buf = Cursor::new(b"12");
Expand All @@ -34,6 +53,30 @@ fn read_line() {
assert_eq!(v, "");
}

#[test]
fn read_line_drop() {
// string contents should be preserved if the future is dropped
let mut buf = Cursor::new(b"12\n\n");
let mut v = String::from("abc");
drop(buf.read_line(&mut v));
assert_eq!(v, "abc");
}

#[test]
fn read_line_io_error() {
let mut r = futures::io::BufReader::new(IOErrorRead(false));
let _ = block_on(r.read_line(&mut String::new()));
}

#[test]
fn read_line_utf8_error() {
let mut buf = Cursor::new(b"12\xFF\n\n");
let mut v = String::from("abc");
let res = block_on(buf.read_line(&mut v));
assert_eq!(res.unwrap_err().kind(), std::io::ErrorKind::InvalidData);
assert_eq!(v, "abc");
}

#[test]
fn maybe_pending() {
let mut buf = b"12".interleave_pending();
Expand Down

0 comments on commit fd71f88

Please sign in to comment.