| 1 | use futures_core::stream::Stream; |
| 2 | use std::{io, pin::Pin}; |
| 3 | use tokio_test::{assert_ready, io::Builder, task}; |
| 4 | use tokio_util::codec::{BytesCodec, FramedRead}; |
| 5 | |
| 6 | macro_rules! pin { |
| 7 | ($id:ident) => { |
| 8 | Pin::new(&mut $id) |
| 9 | }; |
| 10 | } |
| 11 | |
| 12 | macro_rules! assert_read { |
| 13 | ($e:expr, $n:expr) => {{ |
| 14 | let val = assert_ready!($e); |
| 15 | assert_eq!(val.unwrap().unwrap(), $n); |
| 16 | }}; |
| 17 | } |
| 18 | |
| 19 | #[tokio::test ] |
| 20 | async fn return_none_after_error() { |
| 21 | let mut io = FramedRead::new( |
| 22 | Builder::new() |
| 23 | .read(b"abcdef" ) |
| 24 | .read_error(io::Error::new(io::ErrorKind::Other, "Resource errored out" )) |
| 25 | .read(b"more data" ) |
| 26 | .build(), |
| 27 | BytesCodec::new(), |
| 28 | ); |
| 29 | |
| 30 | let mut task = task::spawn(()); |
| 31 | |
| 32 | task.enter(|cx, _| { |
| 33 | assert_read!(pin!(io).poll_next(cx), b"abcdef" .to_vec()); |
| 34 | assert!(assert_ready!(pin!(io).poll_next(cx)).unwrap().is_err()); |
| 35 | assert!(assert_ready!(pin!(io).poll_next(cx)).is_none()); |
| 36 | assert_read!(pin!(io).poll_next(cx), b"more data" .to_vec()); |
| 37 | }) |
| 38 | } |
| 39 | |