1 | use futures::io::AsyncRead; |
2 | use futures_test::task::panic_context; |
3 | use std::io; |
4 | use std::pin::Pin; |
5 | use std::task::{Context, Poll}; |
6 | |
7 | struct MockReader { |
8 | fun: Box<dyn FnMut(&mut [u8]) -> Poll<io::Result<usize>>>, |
9 | } |
10 | |
11 | impl MockReader { |
12 | fn new(fun: impl FnMut(&mut [u8]) -> Poll<io::Result<usize>> + 'static) -> Self { |
13 | Self { fun: Box::new(fun) } |
14 | } |
15 | } |
16 | |
17 | impl AsyncRead for MockReader { |
18 | fn poll_read( |
19 | self: Pin<&mut Self>, |
20 | _cx: &mut Context<'_>, |
21 | buf: &mut [u8], |
22 | ) -> Poll<io::Result<usize>> { |
23 | (self.get_mut().fun)(buf) |
24 | } |
25 | } |
26 | |
27 | /// Verifies that the default implementation of `poll_read_vectored` |
28 | /// calls `poll_read` with an empty slice if no buffers are provided. |
29 | #[test] |
30 | fn read_vectored_no_buffers() { |
31 | let mut reader = MockReader::new(|buf| { |
32 | assert_eq!(buf, b"" ); |
33 | Err(io::ErrorKind::BrokenPipe.into()).into() |
34 | }); |
35 | let cx = &mut panic_context(); |
36 | let bufs = &mut []; |
37 | |
38 | let res = Pin::new(&mut reader).poll_read_vectored(cx, bufs); |
39 | let res = res.map_err(|e| e.kind()); |
40 | assert_eq!(res, Poll::Ready(Err(io::ErrorKind::BrokenPipe))) |
41 | } |
42 | |
43 | /// Verifies that the default implementation of `poll_read_vectored` |
44 | /// calls `poll_read` with the first non-empty buffer. |
45 | #[test] |
46 | fn read_vectored_first_non_empty() { |
47 | let mut reader = MockReader::new(|buf| { |
48 | assert_eq!(buf.len(), 4); |
49 | buf.copy_from_slice(b"four" ); |
50 | Poll::Ready(Ok(4)) |
51 | }); |
52 | let cx = &mut panic_context(); |
53 | let mut buf = [0; 4]; |
54 | let bufs = &mut [ |
55 | io::IoSliceMut::new(&mut []), |
56 | io::IoSliceMut::new(&mut []), |
57 | io::IoSliceMut::new(&mut buf), |
58 | ]; |
59 | |
60 | let res = Pin::new(&mut reader).poll_read_vectored(cx, bufs); |
61 | let res = res.map_err(|e| e.kind()); |
62 | assert_eq!(res, Poll::Ready(Ok(4))); |
63 | assert_eq!(buf, b"four" [..]); |
64 | } |
65 | |