summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2019-06-26 17:06:56 -0700
committerGitHub <noreply@github.com>2019-06-26 17:06:56 -0700
commit1f47ed3dcc4b582315cdbfb195495d7d4c76d3f3 (patch)
tree0b9ca199a6b5e5539d75e4fe46dbc7563bb1b20c
parente9aaacddbda25eeb56a40e4f5e85d616d5e08b74 (diff)
tokio: rewrite io_read.rs test to use async/await (#1207)
This simplifies the test
-rw-r--r--tokio/tests/io.rs11
-rw-r--r--tokio/tests/io_copy.rs28
-rw-r--r--tokio/tests/io_read.rs35
-rw-r--r--tokio/tests/io_read_exact.rs25
-rw-r--r--tokio/tests/io_write.rs23
5 files changed, 40 insertions, 82 deletions
diff --git a/tokio/tests/io.rs b/tokio/tests/io.rs
deleted file mode 100644
index 8224c598..00000000
--- a/tokio/tests/io.rs
+++ /dev/null
@@ -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;
-
-
diff --git a/tokio/tests/io_copy.rs b/tokio/tests/io_copy.rs
index ee972681..0179cfb7 100644
--- a/tokio/tests/io_copy.rs
+++ b/tokio/tests/io_copy.rs
@@ -1,18 +1,16 @@
#![deny(warnings, rust_2018_idioms)]
+#![feature(async_await)]
use tokio::io::{AsyncRead, AsyncWrite, AsyncReadExt};
-use tokio_test::assert_ready_ok;
-use tokio_test::task::MockTask;
+use tokio_test::assert_ok;
use bytes::BytesMut;
-use pin_utils::pin_mut;
-use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
-#[test]
-fn copy() {
+#[tokio::test]
+async fn copy() {
struct Rd(bool);
impl AsyncRead for Rd {
@@ -54,18 +52,10 @@ fn copy() {
}
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 mut rd = Rd(true);
- let mut wr = Wr(buf);
-
- 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"[..]);
- });
+ let n = assert_ok!(rd.copy(&mut wr).await);
+ assert_eq!(n, 11);
+ assert_eq!(wr.0[..], b"hello world"[..]);
}
diff --git a/tokio/tests/io_read.rs b/tokio/tests/io_read.rs
index 8544a6c6..b1fc42c4 100644
--- a/tokio/tests/io_read.rs
+++ b/tokio/tests/io_read.rs
@@ -1,41 +1,38 @@
#![deny(warnings, rust_2018_idioms)]
+#![feature(async_await)]
use tokio::io::{AsyncRead, AsyncReadExt};
-use tokio_test::assert_ready_ok;
-use tokio_test::task::MockTask;
+use tokio_test::assert_ok;
-use pin_utils::pin_mut;
-use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
-#[test]
-fn read() {
- struct Rd;
+#[tokio::test]
+async fn read() {
+ #[derive(Default)]
+ struct Rd {
+ poll_cnt: usize,
+ }
impl AsyncRead for Rd {
fn poll_read(
- self: Pin<&mut Self>,
+ mut self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
+ assert_eq!(0, self.poll_cnt);
+ self.poll_cnt +=1 ;
+
buf[0..11].copy_from_slice(b"hello world");
Poll::Ready(Ok(11))
}
}
let mut buf = Box::new([0; 11]);
- let mut task = MockTask::new();
-
- task.enter(|cx| {
- let mut rd = Rd;
-
- let read = rd.read(&mut buf[..]);
- pin_mut!(read);
+ let mut rd = Rd::default();
- let n = assert_ready_ok!(read.poll(cx));
- assert_eq!(n, 11);
- assert_eq!(buf[..], b"hello world"[..]);
- });
+ let n = assert_ok!(rd.read(&mut buf[..]).await);
+ assert_eq!(n, 11);
+ assert_eq!(buf[..], b"hello world"[..]);
}
diff --git a/tokio/tests/io_read_exact.rs b/tokio/tests/io_read_exact.rs
index 94e35514..1e3ab4ed 100644
--- a/tokio/tests/io_read_exact.rs
+++ b/tokio/tests/io_read_exact.rs
@@ -1,17 +1,15 @@
#![deny(warnings, rust_2018_idioms)]
+#![feature(async_await)]
use tokio::io::{AsyncRead, AsyncReadExt};
-use tokio_test::assert_ready_ok;
-use tokio_test::task::MockTask;
+use tokio_test::assert_ok;
-use pin_utils::pin_mut;
-use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
-#[test]
-fn read_exact() {
+#[tokio::test]
+async fn read_exact() {
struct Rd {
val: &'static [u8; 11],
}
@@ -31,16 +29,9 @@ fn read_exact() {
}
let mut buf = Box::new([0; 8]);
- let mut task = MockTask::new();
+ let mut rd = Rd { val: b"hello world" };
- task.enter(|cx| {
- let mut rd = Rd { val: b"hello world" };
-
- 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"[..]);
- });
+ let n = assert_ok!(rd.read_exact(&mut buf[..]).await);
+ assert_eq!(n, 8);
+ assert_eq!(buf[..], b"hello wo"[..]);
}
diff --git a/tokio/tests/io_write.rs b/tokio/tests/io_write.rs
index 7d80ca55..990abdf2 100644
--- a/tokio/tests/io_write.rs
+++ b/tokio/tests/io_write.rs
@@ -1,18 +1,16 @@
#![deny(warnings, rust_2018_idioms)]
+#![feature(async_await)]
use tokio::io::{AsyncWrite, AsyncWriteExt};
-use tokio_test::assert_ready_ok;
-use tokio_test::task::MockTask;
+use tokio_test::assert_ok;
use bytes::BytesMut;
-use pin_utils::pin_mut;
-use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Context, Poll};
-#[test]
-fn write() {
+#[tokio::test]
+async fn write() {
struct Wr(BytesMut);
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 mut wr = Wr(BytesMut::with_capacity(64));
-
- let write = wr.write(b"hello world");
- pin_mut!(write);
-
- let n = assert_ready_ok!(write.poll(cx));
- assert_eq!(n, 11);
- });
+ let n = assert_ok!(wr.write(b"hello world").await);
+ assert_eq!(n, 11);
}