1 mod mock_writer {
2     use futures::io::AsyncWrite;
3     use std::io;
4     use std::pin::Pin;
5     use std::task::{Context, Poll};
6 
7     pub struct MockWriter {
8         fun: Box<dyn FnMut(&[u8]) -> Poll<io::Result<usize>>>,
9     }
10 
11     impl MockWriter {
new(fun: impl FnMut(&[u8]) -> Poll<io::Result<usize>> + 'static) -> Self12         pub fn new(fun: impl FnMut(&[u8]) -> Poll<io::Result<usize>> + 'static) -> Self {
13             Self { fun: Box::new(fun) }
14         }
15     }
16 
17     impl AsyncWrite for MockWriter {
poll_write( self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &[u8], ) -> Poll<io::Result<usize>>18         fn poll_write(
19             self: Pin<&mut Self>,
20             _cx: &mut Context<'_>,
21             buf: &[u8],
22         ) -> Poll<io::Result<usize>> {
23             (self.get_mut().fun)(buf)
24         }
25 
poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>>26         fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
27             panic!()
28         }
29 
poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>>30         fn poll_close(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> {
31             panic!()
32         }
33     }
34 }
35 
36 /// Verifies that the default implementation of `poll_write_vectored`
37 /// calls `poll_write` with an empty slice if no buffers are provided.
38 #[test]
write_vectored_no_buffers()39 fn write_vectored_no_buffers() {
40     use futures::io::AsyncWrite;
41     use futures_test::task::panic_context;
42     use std::io;
43     use std::pin::Pin;
44     use std::task::Poll;
45 
46     use mock_writer::MockWriter;
47 
48     let mut writer = MockWriter::new(|buf| {
49         assert_eq!(buf, b"");
50         Err(io::ErrorKind::BrokenPipe.into()).into()
51     });
52     let cx = &mut panic_context();
53     let bufs = &mut [];
54 
55     let res = Pin::new(&mut writer).poll_write_vectored(cx, bufs);
56     let res = res.map_err(|e| e.kind());
57     assert_eq!(res, Poll::Ready(Err(io::ErrorKind::BrokenPipe)))
58 }
59 
60 /// Verifies that the default implementation of `poll_write_vectored`
61 /// calls `poll_write` with the first non-empty buffer.
62 #[test]
write_vectored_first_non_empty()63 fn write_vectored_first_non_empty() {
64     use futures::io::AsyncWrite;
65     use futures_test::task::panic_context;
66     use std::io;
67     use std::pin::Pin;
68     use std::task::Poll;
69 
70     use mock_writer::MockWriter;
71 
72     let mut writer = MockWriter::new(|buf| {
73         assert_eq!(buf, b"four");
74         Poll::Ready(Ok(4))
75     });
76     let cx = &mut panic_context();
77     let bufs = &mut [
78         io::IoSlice::new(&[]),
79         io::IoSlice::new(&[]),
80         io::IoSlice::new(b"four")
81     ];
82 
83     let res = Pin::new(&mut writer).poll_write_vectored(cx, bufs);
84     let res = res.map_err(|e| e.kind());
85     assert_eq!(res, Poll::Ready(Ok(4)));
86 }
87