futures_test::io::read

Trait AsyncReadTestExt

source
pub trait AsyncReadTestExt: AsyncRead {
    // Provided methods
    fn assert_unmoved(self) -> AssertUnmoved<Self> 
       where Self: Sized { ... }
    fn interleave_pending(self) -> InterleavePending<Self> 
       where Self: Sized { ... }
    fn limited(self, limit: usize) -> Limited<Self>
       where Self: Sized { ... }
}
Expand description

Additional combinators for testing async readers.

Provided Methods§

source

fn assert_unmoved(self) -> AssertUnmoved<Self>
where Self: Sized,

Asserts that the given is not moved after being polled.

A check for movement is performed each time the reader is polled and when Drop is called.

Aside from keeping track of the location at which the reader was first polled and providing assertions, this reader adds no runtime behavior and simply delegates to the child reader.

source

fn interleave_pending(self) -> InterleavePending<Self>
where Self: Sized,

Introduces an extra Poll::Pending in between each read of the reader.

§Examples
use futures::task::Poll;
use futures::io::{AsyncRead, Cursor};
use futures_test::task::noop_context;
use futures_test::io::AsyncReadTestExt;
use futures::pin_mut;

let reader = 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.

use futures::task::Poll;
use futures::io::{AsyncBufRead, Cursor};
use futures_test::task::noop_context;
use futures_test::io::AsyncReadTestExt;
use futures::pin_mut;

let reader = 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(&[][..]));
source

fn limited(self, limit: usize) -> Limited<Self>
where Self: Sized,

Limit the number of bytes allowed to be read on each call to poll_read.

§Examples
use futures::task::Poll;
use futures::io::{AsyncRead, Cursor};
use futures_test::task::noop_context;
use futures_test::io::AsyncReadTestExt;
use futures::pin_mut;

let reader = 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]);

Implementors§

source§

impl<R> AsyncReadTestExt for R
where R: AsyncRead,