futures_test/io/write/
mod.rs

1//! Additional combinators for testing async writers.
2
3use futures_io::AsyncWrite;
4
5pub use super::limited::Limited;
6pub use crate::interleave_pending::InterleavePending;
7
8/// Additional combinators for testing async writers.
9pub trait AsyncWriteTestExt: AsyncWrite {
10    /// Introduces an extra [`Poll::Pending`](futures_core::task::Poll::Pending)
11    /// in between each operation on the writer.
12    ///
13    /// # Examples
14    ///
15    /// ```
16    /// use futures::task::Poll;
17    /// use futures::io::AsyncWrite;
18    /// use futures_test::task::noop_context;
19    /// use futures_test::io::AsyncWriteTestExt;
20    /// use futures::pin_mut;
21    ///
22    /// let writer = std::io::Cursor::new([0u8; 4]).interleave_pending_write();
23    /// pin_mut!(writer);
24    ///
25    /// let mut cx = noop_context();
26    ///
27    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[1, 2])?, Poll::Pending);
28    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[1, 2])?, Poll::Ready(2));
29    /// assert_eq!(writer.get_ref().get_ref(), &[1, 2, 0, 0]);
30    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[3, 4])?, Poll::Pending);
31    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[3, 4])?, Poll::Ready(2));
32    /// assert_eq!(writer.get_ref().get_ref(), &[1, 2, 3, 4]);
33    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[5, 6])?, Poll::Pending);
34    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[5, 6])?, Poll::Ready(0));
35    ///
36    /// assert_eq!(writer.as_mut().poll_flush(&mut cx)?, Poll::Pending);
37    /// assert_eq!(writer.as_mut().poll_flush(&mut cx)?, Poll::Ready(()));
38    ///
39    /// assert_eq!(writer.as_mut().poll_close(&mut cx)?, Poll::Pending);
40    /// assert_eq!(writer.as_mut().poll_close(&mut cx)?, Poll::Ready(()));
41    ///
42    /// # Ok::<(), std::io::Error>(())
43    /// ```
44    fn interleave_pending_write(self) -> InterleavePending<Self>
45    where
46        Self: Sized,
47    {
48        InterleavePending::new(self)
49    }
50
51    /// Limit the number of bytes allowed to be written on each call to `poll_write`.
52    ///
53    /// # Examples
54    ///
55    /// ```
56    /// use futures::task::Poll;
57    /// use futures::io::AsyncWrite;
58    /// use futures_test::task::noop_context;
59    /// use futures_test::io::AsyncWriteTestExt;
60    /// use futures::pin_mut;
61    ///
62    /// let writer = std::io::Cursor::new([0u8; 4]).limited_write(2);
63    /// pin_mut!(writer);
64    ///
65    /// let mut cx = noop_context();
66    ///
67    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[1, 2])?, Poll::Ready(2));
68    /// assert_eq!(writer.get_ref().get_ref(), &[1, 2, 0, 0]);
69    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[3])?, Poll::Ready(1));
70    /// assert_eq!(writer.get_ref().get_ref(), &[1, 2, 3, 0]);
71    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[4, 5])?, Poll::Ready(1));
72    /// assert_eq!(writer.get_ref().get_ref(), &[1, 2, 3, 4]);
73    /// assert_eq!(writer.as_mut().poll_write(&mut cx, &[5])?, Poll::Ready(0));
74    ///
75    /// # Ok::<(), std::io::Error>(())
76    /// ```
77    fn limited_write(self, limit: usize) -> Limited<Self>
78    where
79        Self: Sized,
80    {
81        Limited::new(self, limit)
82    }
83}
84
85impl<W> AsyncWriteTestExt for W where W: AsyncWrite {}