Skip to main content

futures_test/stream/
mod.rs

1//! Additional combinators for testing streams.
2
3use futures_core::stream::Stream;
4
5pub use crate::assert_unmoved::AssertUnmoved;
6pub use crate::interleave_pending::InterleavePending;
7
8/// Additional combinators for testing streams.
9pub trait StreamTestExt: Stream {
10    /// Asserts that the given is not moved after being polled.
11    ///
12    /// A check for movement is performed each time the stream is polled
13    /// and when `Drop` is called.
14    ///
15    /// Aside from keeping track of the location at which the stream was first
16    /// polled and providing assertions, this stream adds no runtime behavior
17    /// and simply delegates to the child stream.
18    fn assert_unmoved(self) -> AssertUnmoved<Self>
19    where
20        Self: Sized,
21    {
22        AssertUnmoved::new(self)
23    }
24
25    /// Introduces an extra [`Poll::Pending`](futures_core::task::Poll::Pending)
26    /// in between each item of the stream.
27    ///
28    /// # Examples
29    ///
30    /// ```
31    /// use core::pin::pin;
32    ///
33    /// use futures::task::Poll;
34    /// use futures::stream::{self, Stream};
35    /// use futures_test::task::noop_context;
36    /// use futures_test::stream::StreamTestExt;
37    ///
38    /// let stream = stream::iter(vec![1, 2]).interleave_pending();
39    /// let mut stream = pin!(stream);
40    ///
41    /// let mut cx = noop_context();
42    ///
43    /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Pending);
44    /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Ready(Some(1)));
45    /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Pending);
46    /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Ready(Some(2)));
47    /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Pending);
48    /// assert_eq!(stream.as_mut().poll_next(&mut cx), Poll::Ready(None));
49    /// ```
50    fn interleave_pending(self) -> InterleavePending<Self>
51    where
52        Self: Sized,
53    {
54        InterleavePending::new(self)
55    }
56}
57
58impl<St> StreamTestExt for St where St: Stream {}