mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-09 00:00:08 +02:00
tokio: rewrite io_read.rs test to use async/await (#1207)
This simplifies the test
This commit is contained in:
@@ -1,11 +0,0 @@
|
|||||||
use bytes::BytesMut;
|
|
||||||
use pin_utils::pin_mut;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::io;
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::task::{Context, Poll};
|
|
||||||
use tokio::io::{AsyncRead, AsyncWrite};
|
|
||||||
use tokio_test::assert_ready_ok;
|
|
||||||
use tokio_test::task::MockTask;
|
|
||||||
|
|
||||||
|
|
||||||
+9
-19
@@ -1,18 +1,16 @@
|
|||||||
#![deny(warnings, rust_2018_idioms)]
|
#![deny(warnings, rust_2018_idioms)]
|
||||||
|
#![feature(async_await)]
|
||||||
|
|
||||||
use tokio::io::{AsyncRead, AsyncWrite, AsyncReadExt};
|
use tokio::io::{AsyncRead, AsyncWrite, AsyncReadExt};
|
||||||
use tokio_test::assert_ready_ok;
|
use tokio_test::assert_ok;
|
||||||
use tokio_test::task::MockTask;
|
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use pin_utils::pin_mut;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn copy() {
|
async fn copy() {
|
||||||
struct Rd(bool);
|
struct Rd(bool);
|
||||||
|
|
||||||
impl AsyncRead for Rd {
|
impl AsyncRead for Rd {
|
||||||
@@ -54,18 +52,10 @@ fn copy() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let buf = BytesMut::with_capacity(64);
|
let buf = BytesMut::with_capacity(64);
|
||||||
let mut task = MockTask::new();
|
let mut rd = Rd(true);
|
||||||
|
let mut wr = Wr(buf);
|
||||||
|
|
||||||
task.enter(|cx| {
|
let n = assert_ok!(rd.copy(&mut wr).await);
|
||||||
let mut rd = Rd(true);
|
assert_eq!(n, 11);
|
||||||
let mut wr = Wr(buf);
|
assert_eq!(wr.0[..], b"hello world"[..]);
|
||||||
|
|
||||||
let copy = rd.copy(&mut wr);
|
|
||||||
pin_mut!(copy);
|
|
||||||
|
|
||||||
let n = assert_ready_ok!(copy.poll(cx));
|
|
||||||
|
|
||||||
assert_eq!(n, 11);
|
|
||||||
assert_eq!(wr.0[..], b"hello world"[..]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-19
@@ -1,41 +1,38 @@
|
|||||||
#![deny(warnings, rust_2018_idioms)]
|
#![deny(warnings, rust_2018_idioms)]
|
||||||
|
#![feature(async_await)]
|
||||||
|
|
||||||
use tokio::io::{AsyncRead, AsyncReadExt};
|
use tokio::io::{AsyncRead, AsyncReadExt};
|
||||||
use tokio_test::assert_ready_ok;
|
use tokio_test::assert_ok;
|
||||||
use tokio_test::task::MockTask;
|
|
||||||
|
|
||||||
use pin_utils::pin_mut;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn read() {
|
async fn read() {
|
||||||
struct Rd;
|
#[derive(Default)]
|
||||||
|
struct Rd {
|
||||||
|
poll_cnt: usize,
|
||||||
|
}
|
||||||
|
|
||||||
impl AsyncRead for Rd {
|
impl AsyncRead for Rd {
|
||||||
fn poll_read(
|
fn poll_read(
|
||||||
self: Pin<&mut Self>,
|
mut self: Pin<&mut Self>,
|
||||||
_cx: &mut Context<'_>,
|
_cx: &mut Context<'_>,
|
||||||
buf: &mut [u8],
|
buf: &mut [u8],
|
||||||
) -> Poll<io::Result<usize>> {
|
) -> Poll<io::Result<usize>> {
|
||||||
|
assert_eq!(0, self.poll_cnt);
|
||||||
|
self.poll_cnt +=1 ;
|
||||||
|
|
||||||
buf[0..11].copy_from_slice(b"hello world");
|
buf[0..11].copy_from_slice(b"hello world");
|
||||||
Poll::Ready(Ok(11))
|
Poll::Ready(Ok(11))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut buf = Box::new([0; 11]);
|
let mut buf = Box::new([0; 11]);
|
||||||
let mut task = MockTask::new();
|
let mut rd = Rd::default();
|
||||||
|
|
||||||
task.enter(|cx| {
|
let n = assert_ok!(rd.read(&mut buf[..]).await);
|
||||||
let mut rd = Rd;
|
assert_eq!(n, 11);
|
||||||
|
assert_eq!(buf[..], b"hello world"[..]);
|
||||||
let read = rd.read(&mut buf[..]);
|
|
||||||
pin_mut!(read);
|
|
||||||
|
|
||||||
let n = assert_ready_ok!(read.poll(cx));
|
|
||||||
assert_eq!(n, 11);
|
|
||||||
assert_eq!(buf[..], b"hello world"[..]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
#![deny(warnings, rust_2018_idioms)]
|
#![deny(warnings, rust_2018_idioms)]
|
||||||
|
#![feature(async_await)]
|
||||||
|
|
||||||
use tokio::io::{AsyncRead, AsyncReadExt};
|
use tokio::io::{AsyncRead, AsyncReadExt};
|
||||||
use tokio_test::assert_ready_ok;
|
use tokio_test::assert_ok;
|
||||||
use tokio_test::task::MockTask;
|
|
||||||
|
|
||||||
use pin_utils::pin_mut;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn read_exact() {
|
async fn read_exact() {
|
||||||
struct Rd {
|
struct Rd {
|
||||||
val: &'static [u8; 11],
|
val: &'static [u8; 11],
|
||||||
}
|
}
|
||||||
@@ -31,16 +29,9 @@ fn read_exact() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut buf = Box::new([0; 8]);
|
let mut buf = Box::new([0; 8]);
|
||||||
let mut task = MockTask::new();
|
let mut rd = Rd { val: b"hello world" };
|
||||||
|
|
||||||
task.enter(|cx| {
|
let n = assert_ok!(rd.read_exact(&mut buf[..]).await);
|
||||||
let mut rd = Rd { val: b"hello world" };
|
assert_eq!(n, 8);
|
||||||
|
assert_eq!(buf[..], b"hello wo"[..]);
|
||||||
let read = rd.read_exact(&mut buf[..]);
|
|
||||||
pin_mut!(read);
|
|
||||||
|
|
||||||
let n = assert_ready_ok!(read.poll(cx));
|
|
||||||
assert_eq!(n, 8);
|
|
||||||
assert_eq!(buf[..], b"hello wo"[..]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-16
@@ -1,18 +1,16 @@
|
|||||||
#![deny(warnings, rust_2018_idioms)]
|
#![deny(warnings, rust_2018_idioms)]
|
||||||
|
#![feature(async_await)]
|
||||||
|
|
||||||
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
||||||
use tokio_test::assert_ready_ok;
|
use tokio_test::assert_ok;
|
||||||
use tokio_test::task::MockTask;
|
|
||||||
|
|
||||||
use bytes::BytesMut;
|
use bytes::BytesMut;
|
||||||
use pin_utils::pin_mut;
|
|
||||||
use std::future::Future;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::task::{Context, Poll};
|
use std::task::{Context, Poll};
|
||||||
|
|
||||||
#[test]
|
#[tokio::test]
|
||||||
fn write() {
|
async fn write() {
|
||||||
struct Wr(BytesMut);
|
struct Wr(BytesMut);
|
||||||
|
|
||||||
impl AsyncWrite for Wr {
|
impl AsyncWrite for Wr {
|
||||||
@@ -34,15 +32,8 @@ fn write() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut task = MockTask::new();
|
let mut wr = Wr(BytesMut::with_capacity(64));
|
||||||
|
|
||||||
task.enter(|cx| {
|
let n = assert_ok!(wr.write(b"hello world").await);
|
||||||
let mut wr = Wr(BytesMut::with_capacity(64));
|
assert_eq!(n, 11);
|
||||||
|
|
||||||
let write = wr.write(b"hello world");
|
|
||||||
pin_mut!(write);
|
|
||||||
|
|
||||||
let n = assert_ready_ok!(write.poll(cx));
|
|
||||||
assert_eq!(n, 11);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user