diff options
author | Carl Lerche <me@carllerche.com> | 2019-11-03 14:10:14 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-03 14:10:14 -0800 |
commit | 966ccd5d5306adf6b6c39721331c2a3c32be6fa8 (patch) | |
tree | 832d287b7667d79f500d6ac0a336200d054b41dc /tokio-util | |
parent | 3948e162927584def39eefaa92284ae73d3b1673 (diff) |
test: unify MockTask and task::spawn (#1728)
Delete `MockTask` in favor of `task::spawn`. Both are functionally
equivalent.
Diffstat (limited to 'tokio-util')
-rw-r--r-- | tokio-util/tests/framed_read.rs | 42 | ||||
-rw-r--r-- | tokio-util/tests/framed_write.rs | 11 | ||||
-rw-r--r-- | tokio-util/tests/length_delimited.rs | 34 |
3 files changed, 43 insertions, 44 deletions
diff --git a/tokio-util/tests/framed_read.rs b/tokio-util/tests/framed_read.rs index 2064b9fb..6636b880 100644 --- a/tokio-util/tests/framed_read.rs +++ b/tokio-util/tests/framed_read.rs @@ -2,7 +2,7 @@ use tokio::prelude::*; use tokio_test::assert_ready; -use tokio_test::task::MockTask; +use tokio_test::task; use tokio_util::codec::{Decoder, FramedRead}; use bytes::{Buf, BytesMut, IntoBuf}; @@ -51,13 +51,13 @@ impl Decoder for U32Decoder { #[test] fn read_multi_frame_in_packet() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mock = mock! { Ok(b"\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02".to_vec()), }; let mut framed = FramedRead::new(mock, U32Decoder); - task.enter(|cx| { + task.enter(|cx, _| { assert_read!(pin!(framed).poll_next(cx), 0); assert_read!(pin!(framed).poll_next(cx), 1); assert_read!(pin!(framed).poll_next(cx), 2); @@ -67,7 +67,7 @@ fn read_multi_frame_in_packet() { #[test] fn read_multi_frame_across_packets() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mock = mock! { Ok(b"\x00\x00\x00\x00".to_vec()), Ok(b"\x00\x00\x00\x01".to_vec()), @@ -75,7 +75,7 @@ fn read_multi_frame_across_packets() { }; let mut framed = FramedRead::new(mock, U32Decoder); - task.enter(|cx| { + task.enter(|cx, _| { assert_read!(pin!(framed).poll_next(cx), 0); assert_read!(pin!(framed).poll_next(cx), 1); assert_read!(pin!(framed).poll_next(cx), 2); @@ -85,7 +85,7 @@ fn read_multi_frame_across_packets() { #[test] fn read_not_ready() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mock = mock! { Err(io::Error::new(io::ErrorKind::WouldBlock, "")), Ok(b"\x00\x00\x00\x00".to_vec()), @@ -93,7 +93,7 @@ fn read_not_ready() { }; let mut framed = FramedRead::new(mock, U32Decoder); - task.enter(|cx| { + task.enter(|cx, _| { assert!(pin!(framed).poll_next(cx).is_pending()); assert_read!(pin!(framed).poll_next(cx), 0); assert_read!(pin!(framed).poll_next(cx), 1); @@ -103,7 +103,7 @@ fn read_not_ready() { #[test] fn read_partial_then_not_ready() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mock = mock! { Ok(b"\x00\x00".to_vec()), Err(io::Error::new(io::ErrorKind::WouldBlock, "")), @@ -111,7 +111,7 @@ fn read_partial_then_not_ready() { }; let mut framed = FramedRead::new(mock, U32Decoder); - task.enter(|cx| { + task.enter(|cx, _| { assert!(pin!(framed).poll_next(cx).is_pending()); assert_read!(pin!(framed).poll_next(cx), 0); assert_read!(pin!(framed).poll_next(cx), 1); @@ -122,13 +122,13 @@ fn read_partial_then_not_ready() { #[test] fn read_err() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mock = mock! { Err(io::Error::new(io::ErrorKind::Other, "")), }; let mut framed = FramedRead::new(mock, U32Decoder); - task.enter(|cx| { + task.enter(|cx, _| { assert_eq!( io::ErrorKind::Other, assert_ready!(pin!(framed).poll_next(cx)) @@ -141,14 +141,14 @@ fn read_err() { #[test] fn read_partial_then_err() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mock = mock! { Ok(b"\x00\x00".to_vec()), Err(io::Error::new(io::ErrorKind::Other, "")), }; let mut framed = FramedRead::new(mock, U32Decoder); - task.enter(|cx| { + task.enter(|cx, _| { assert_eq!( io::ErrorKind::Other, assert_ready!(pin!(framed).poll_next(cx)) @@ -161,7 +161,7 @@ fn read_partial_then_err() { #[test] fn read_partial_would_block_then_err() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mock = mock! { Ok(b"\x00\x00".to_vec()), Err(io::Error::new(io::ErrorKind::WouldBlock, "")), @@ -169,7 +169,7 @@ fn read_partial_would_block_then_err() { }; let mut framed = FramedRead::new(mock, U32Decoder); - task.enter(|cx| { + task.enter(|cx, _| { assert!(pin!(framed).poll_next(cx).is_pending()); assert_eq!( io::ErrorKind::Other, @@ -183,11 +183,11 @@ fn read_partial_would_block_then_err() { #[test] fn huge_size() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let data = [0; 32 * 1024]; let mut framed = FramedRead::new(Slice(&data[..]), BigDecoder); - task.enter(|cx| { + task.enter(|cx, _| { assert_read!(pin!(framed).poll_next(cx), 0); assert!(assert_ready!(pin!(framed).poll_next(cx)).is_none()); }); @@ -210,11 +210,11 @@ fn huge_size() { #[test] fn data_remaining_is_error() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let slice = Slice(&[0; 5]); let mut framed = FramedRead::new(slice, U32Decoder); - task.enter(|cx| { + task.enter(|cx, _| { assert_read!(pin!(framed).poll_next(cx), 0); assert!(assert_ready!(pin!(framed).poll_next(cx)).unwrap().is_err()); }); @@ -222,7 +222,7 @@ fn data_remaining_is_error() { #[test] fn multi_frames_on_eof() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); struct MyDecoder(Vec<u32>); impl Decoder for MyDecoder { @@ -244,7 +244,7 @@ fn multi_frames_on_eof() { let mut framed = FramedRead::new(mock!(), MyDecoder(vec![0, 1, 2, 3])); - task.enter(|cx| { + task.enter(|cx, _| { assert_read!(pin!(framed).poll_next(cx), 0); assert_read!(pin!(framed).poll_next(cx), 1); assert_read!(pin!(framed).poll_next(cx), 2); diff --git a/tokio-util/tests/framed_write.rs b/tokio-util/tests/framed_write.rs index 03f0f08b..9d760766 100644 --- a/tokio-util/tests/framed_write.rs +++ b/tokio-util/tests/framed_write.rs @@ -1,8 +1,7 @@ #![warn(rust_2018_idioms)] use tokio::io::AsyncWrite; -use tokio_test::assert_ready; -use tokio_test::task::MockTask; +use tokio_test::{assert_ready, task}; use tokio_util::codec::{Encoder, FramedWrite}; use bytes::{BufMut, BytesMut}; @@ -43,13 +42,13 @@ impl Encoder for U32Encoder { #[test] fn write_multi_frame_in_packet() { - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mock = mock! { Ok(b"\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x02".to_vec()), }; let mut framed = FramedWrite::new(mock, U32Encoder); - task.enter(|cx| { + task.enter(|cx, _| { assert!(assert_ready!(pin!(framed).poll_ready(cx)).is_ok()); assert!(pin!(framed).start_send(0).is_ok()); assert!(assert_ready!(pin!(framed).poll_ready(cx)).is_ok()); @@ -99,9 +98,9 @@ fn write_hits_backpressure() { // 1 'wouldblock', 4 * 2KB buffers, 1 b-byte buffer assert_eq!(mock.calls.len(), 6); - let mut task = MockTask::new(); + let mut task = task::spawn(()); let mut framed = FramedWrite::new(mock, U32Encoder); - task.enter(|cx| { + task.enter(|cx, _| { // Send 8KB. This fills up FramedWrite2 buffer for i in 0..ITER { assert!(assert_ready!(pin!(framed).poll_ready(cx)).is_ok()); diff --git a/tokio-util/tests/length_delimited.rs b/tokio-util/tests/length_delimited.rs index c78dfac8..b287bb35 100644 --- a/tokio-util/tests/length_delimited.rs +++ b/tokio-util/tests/length_delimited.rs @@ -2,7 +2,7 @@ use tokio::io::{AsyncRead, AsyncWrite}; use tokio::prelude::*; -use tokio_test::task::MockTask; +use tokio_test::task; use tokio_test::{ assert_err, assert_ok, assert_pending, assert_ready, assert_ready_err, assert_ready_ok, }; @@ -26,7 +26,7 @@ macro_rules! mock { macro_rules! assert_next_eq { ($io:ident, $expect:expr) => {{ - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { let res = assert_ready!($io.as_mut().poll_next(cx)); match res { Some(Ok(v)) => assert_eq!(v, $expect.as_ref()), @@ -39,7 +39,7 @@ macro_rules! assert_next_eq { macro_rules! assert_next_pending { ($io:ident) => {{ - MockTask::new().enter(|cx| match $io.as_mut().poll_next(cx) { + task::spawn(()).enter(|cx, _| match $io.as_mut().poll_next(cx) { Ready(Some(Ok(v))) => panic!("value = {:?}", v), Ready(Some(Err(e))) => panic!("error = {:?}", e), Ready(None) => panic!("done"), @@ -50,7 +50,7 @@ macro_rules! assert_next_pending { macro_rules! assert_next_err { ($io:ident) => {{ - MockTask::new().enter(|cx| match $io.as_mut().poll_next(cx) { + task::spawn(()).enter(|cx, _| match $io.as_mut().poll_next(cx) { Ready(Some(Ok(v))) => panic!("value = {:?}", v), Ready(Some(Err(_))) => {} Ready(None) => panic!("done"), @@ -61,7 +61,7 @@ macro_rules! assert_next_err { macro_rules! assert_done { ($io:ident) => {{ - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { let res = assert_ready!($io.as_mut().poll_next(cx)); match res { Some(Ok(v)) => panic!("value = {:?}", v), @@ -405,7 +405,7 @@ fn write_single_frame_length_adjusted() { }); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdefghi"))); assert_ready_ok!(io.as_mut().poll_flush(cx)); @@ -418,7 +418,7 @@ fn write_nothing_yields_nothing() { let io = FramedWrite::new(mock!(), LengthDelimitedCodec::new()); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.poll_flush(cx)); }); } @@ -435,7 +435,7 @@ fn write_single_frame_one_packet() { ); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdefghi"))); assert_ready_ok!(io.as_mut().poll_flush(cx)); @@ -459,7 +459,7 @@ fn write_single_multi_frame_one_packet() { ); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdefghi"))); @@ -492,7 +492,7 @@ fn write_single_multi_frame_multi_packet() { ); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdefghi"))); @@ -526,7 +526,7 @@ fn write_single_frame_would_block() { ); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdefghi"))); @@ -549,7 +549,7 @@ fn write_single_frame_little_endian() { }); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdefghi"))); @@ -569,7 +569,7 @@ fn write_single_frame_with_short_length_field() { }); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdefghi"))); @@ -586,7 +586,7 @@ fn write_max_frame_len() { .new_write(mock! {}); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_err!(io.as_mut().start_send(Bytes::from("abcdef"))); @@ -603,7 +603,7 @@ fn write_update_max_frame_len_at_rest() { }); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdef"))); @@ -628,7 +628,7 @@ fn write_update_max_frame_len_in_flight() { }); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdef"))); @@ -648,7 +648,7 @@ fn write_zero() { let io = length_delimited::Builder::new().new_write(mock! {}); pin_mut!(io); - MockTask::new().enter(|cx| { + task::spawn(()).enter(|cx, _| { assert_ready_ok!(io.as_mut().poll_ready(cx)); assert_ok!(io.as_mut().start_send(Bytes::from("abcdef"))); |