[−][src]Trait futures_test::io::read::AsyncReadTestExt
Additional combinators for testing async readers.
Provided methods
ⓘImportant traits for InterleavePending<Fut>fn interleave_pending(self) -> InterleavePending<Self> where
Self: Sized,
ⓘImportant traits for InterleavePending<Fut>
Self: Sized,
Introduces an extra Poll::Pending
in between each read of the reader.
Examples
#![feature(async_await)] use futures::task::Poll; use futures::io::AsyncRead; use futures_test::task::noop_context; use futures_test::io::AsyncReadTestExt; use futures::pin_mut; let reader = std::io::Cursor::new(&[1, 2, 3]).interleave_pending(); pin_mut!(reader); let mut cx = noop_context(); let mut buf = [0, 0]; assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Pending); assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Ready(2)); assert_eq!(buf, [1, 2]); assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Pending); assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Ready(1)); assert_eq!(buf, [3, 2]); assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Pending); assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Ready(0));
AsyncBufRead
The returned reader will also implement AsyncBufRead
if the underlying reader does.
#![feature(async_await)] use futures::task::Poll; use futures::io::AsyncBufRead; use futures_test::task::noop_context; use futures_test::io::AsyncReadTestExt; use futures::pin_mut; let reader = std::io::Cursor::new(&[1, 2, 3]).interleave_pending(); pin_mut!(reader); let mut cx = noop_context(); assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Pending); assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Ready(&[1, 2, 3][..])); reader.as_mut().consume(2); assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Pending); assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Ready(&[3][..])); reader.as_mut().consume(1); assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Pending); assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Ready(&[][..]));
fn limited(self, limit: usize) -> Limited<Self> where
Self: Sized,
Self: Sized,
Limit the number of bytes allowed to be read on each call to poll_read
.
Examples
#![feature(async_await)] use futures::task::Poll; use futures::io::AsyncRead; use futures_test::task::noop_context; use futures_test::io::AsyncReadTestExt; use futures::pin_mut; let reader = std::io::Cursor::new(&[1, 2, 3, 4, 5]).limited(2); pin_mut!(reader); let mut cx = noop_context(); let mut buf = [0; 10]; assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf)?, Poll::Ready(2)); assert_eq!(&buf[..2], &[1, 2]); assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf)?, Poll::Ready(2)); assert_eq!(&buf[..2], &[3, 4]); assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf)?, Poll::Ready(1)); assert_eq!(&buf[..1], &[5]);