|
1 | 1 | //! Additional combinators for testing async IO.
|
2 | 2 |
|
3 |
| -use futures_io::AsyncRead; |
| 3 | +pub mod read; |
| 4 | +pub use read::AsyncReadTestExt; |
4 | 5 |
|
5 |
| -mod interleave_pending; |
6 |
| -pub use self::interleave_pending::InterleavePending; |
7 |
| - |
8 |
| -/// Additional combinators for testing async readers. |
9 |
| -pub trait AsyncReadTestExt: AsyncRead { |
10 |
| - /// Introduces an extra [`Poll::Pending`](futures_core::task::Poll::Pending) |
11 |
| - /// in between each read of the reader. |
12 |
| - /// |
13 |
| - /// # Examples |
14 |
| - /// |
15 |
| - /// ``` |
16 |
| - /// #![feature(async_await)] |
17 |
| - /// use futures::task::Poll; |
18 |
| - /// use futures::io::AsyncRead; |
19 |
| - /// use futures_test::task::noop_context; |
20 |
| - /// use futures_test::io::AsyncReadTestExt; |
21 |
| - /// use pin_utils::pin_mut; |
22 |
| - /// |
23 |
| - /// let reader = std::io::Cursor::new(&[1, 2, 3]).interleave_pending(); |
24 |
| - /// pin_mut!(reader); |
25 |
| - /// |
26 |
| - /// let mut cx = noop_context(); |
27 |
| - /// |
28 |
| - /// let mut buf = [0, 0]; |
29 |
| - /// |
30 |
| - /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Pending); |
31 |
| - /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Ready(2)); |
32 |
| - /// assert_eq!(buf, [1, 2]); |
33 |
| - /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Pending); |
34 |
| - /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Ready(1)); |
35 |
| - /// assert_eq!(buf, [3, 2]); |
36 |
| - /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Pending); |
37 |
| - /// assert_eq!(reader.as_mut().poll_read(&mut cx, &mut buf[..])?, Poll::Ready(0)); |
38 |
| - /// |
39 |
| - /// # Ok::<(), std::io::Error>(()) |
40 |
| - /// ``` |
41 |
| - /// |
42 |
| - /// ## `AsyncBufRead` |
43 |
| - /// |
44 |
| - /// The returned reader will also implement `AsyncBufRead` if the underlying reader does. |
45 |
| - /// |
46 |
| - /// ``` |
47 |
| - /// #![feature(async_await)] |
48 |
| - /// use futures::task::Poll; |
49 |
| - /// use futures::io::AsyncBufRead; |
50 |
| - /// use futures_test::task::noop_context; |
51 |
| - /// use futures_test::io::AsyncReadTestExt; |
52 |
| - /// use pin_utils::pin_mut; |
53 |
| - /// |
54 |
| - /// let reader = std::io::Cursor::new(&[1, 2, 3]).interleave_pending(); |
55 |
| - /// pin_mut!(reader); |
56 |
| - /// |
57 |
| - /// let mut cx = noop_context(); |
58 |
| - /// |
59 |
| - /// assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Pending); |
60 |
| - /// assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Ready(&[1, 2, 3][..])); |
61 |
| - /// reader.as_mut().consume(2); |
62 |
| - /// assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Pending); |
63 |
| - /// assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Ready(&[3][..])); |
64 |
| - /// reader.as_mut().consume(1); |
65 |
| - /// assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Pending); |
66 |
| - /// assert_eq!(reader.as_mut().poll_fill_buf(&mut cx)?, Poll::Ready(&[][..])); |
67 |
| - /// |
68 |
| - /// # Ok::<(), std::io::Error>(()) |
69 |
| - /// ``` |
70 |
| - fn interleave_pending(self) -> InterleavePending<Self> |
71 |
| - where |
72 |
| - Self: Sized, |
73 |
| - { |
74 |
| - InterleavePending::new(self) |
75 |
| - } |
76 |
| -} |
77 |
| - |
78 |
| -impl<R> AsyncReadTestExt for R where R: AsyncRead {} |
0 commit comments