diff options
author | Lucio Franco <luciofranco14@gmail.com> | 2019-08-02 15:23:44 -0400 |
---|---|---|
committer | Carl Lerche <me@carllerche.com> | 2019-08-02 12:23:44 -0700 |
commit | ff41108834ab8fdf92e5c337b46637ba696d89c7 (patch) | |
tree | b11dd84a9443281b03432a68a016be813f58017a /tokio/tests | |
parent | 6b202722ea78548b941b3aa6e92ff281a7ce42c5 (diff) |
io: move io helpers back into `tokio-io` (#1377)
Utilities are made optional with a feature flag.
Diffstat (limited to 'tokio/tests')
-rw-r--r-- | tokio/tests/io_copy.rs | 61 | ||||
-rw-r--r-- | tokio/tests/io_lines.rs | 53 | ||||
-rw-r--r-- | tokio/tests/io_read.rs | 38 | ||||
-rw-r--r-- | tokio/tests/io_read_exact.rs | 39 | ||||
-rw-r--r-- | tokio/tests/io_read_line.rs | 60 | ||||
-rw-r--r-- | tokio/tests/io_read_to_end.rs | 40 | ||||
-rw-r--r-- | tokio/tests/io_read_to_string.rs | 40 | ||||
-rw-r--r-- | tokio/tests/io_read_until.rs | 56 | ||||
-rw-r--r-- | tokio/tests/io_write.rs | 47 | ||||
-rw-r--r-- | tokio/tests/io_write_all.rs | 51 |
10 files changed, 0 insertions, 485 deletions
diff --git a/tokio/tests/io_copy.rs b/tokio/tests/io_copy.rs deleted file mode 100644 index 2fac7e85..00000000 --- a/tokio/tests/io_copy.rs +++ /dev/null @@ -1,61 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite}; -use tokio_test::assert_ok; - -use bytes::BytesMut; -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -#[tokio::test] -async fn copy() { - struct Rd(bool); - - impl AsyncRead for Rd { - fn poll_read( - mut self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &mut [u8], - ) -> Poll<io::Result<usize>> { - if self.0 { - buf[0..11].copy_from_slice(b"hello world"); - self.0 = false; - Poll::Ready(Ok(11)) - } else { - Poll::Ready(Ok(0)) - } - } - } - - struct Wr(BytesMut); - - impl Unpin for Wr {} - impl AsyncWrite for Wr { - fn poll_write( - mut self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &[u8], - ) -> Poll<io::Result<usize>> { - self.0.extend(buf); - Ok(buf.len()).into() - } - - fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { - Ok(()).into() - } - - fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { - Ok(()).into() - } - } - - let buf = BytesMut::with_capacity(64); - let mut rd = Rd(true); - let mut wr = Wr(buf); - - 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_lines.rs b/tokio/tests/io_lines.rs deleted file mode 100644 index 02ce6149..00000000 --- a/tokio/tests/io_lines.rs +++ /dev/null @@ -1,53 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use futures_util::StreamExt; -use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncRead}; -use tokio_test::assert_ok; - -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -#[tokio::test] -async fn lines() { - struct Rd { - val: &'static [u8], - } - - impl AsyncRead for Rd { - fn poll_read( - self: Pin<&mut Self>, - _: &mut Context<'_>, - _: &mut [u8], - ) -> Poll<io::Result<usize>> { - unimplemented!() - } - } - - impl AsyncBufRead for Rd { - fn poll_fill_buf<'a>( - self: Pin<&'a mut Self>, - _: &mut Context<'_>, - ) -> Poll<io::Result<&'a [u8]>> { - Poll::Ready(Ok(self.val)) - } - - fn consume(mut self: Pin<&mut Self>, amt: usize) { - self.val = &self.val[amt..]; - } - } - - let rd = Rd { - val: b"hello\r\nworld\n\n", - }; - let mut st = rd.lines(); - - let b = assert_ok!(st.next().await.unwrap()); - assert_eq!(b, "hello"); - let b = assert_ok!(st.next().await.unwrap()); - assert_eq!(b, "world"); - let b = assert_ok!(st.next().await.unwrap()); - assert_eq!(b, ""); - assert!(st.next().await.is_none()); -} diff --git a/tokio/tests/io_read.rs b/tokio/tests/io_read.rs deleted file mode 100644 index 0ec2bef3..00000000 --- a/tokio/tests/io_read.rs +++ /dev/null @@ -1,38 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncRead, AsyncReadExt}; -use tokio_test::assert_ok; - -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -#[tokio::test] -async fn read() { - #[derive(Default)] - struct Rd { - poll_cnt: usize, - } - - impl AsyncRead for Rd { - fn poll_read( - 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 rd = Rd::default(); - - 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 deleted file mode 100644 index eae7435f..00000000 --- a/tokio/tests/io_read_exact.rs +++ /dev/null @@ -1,39 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncRead, AsyncReadExt}; -use tokio_test::assert_ok; - -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -#[tokio::test] -async fn read_exact() { - struct Rd { - val: &'static [u8; 11], - } - - impl AsyncRead for Rd { - fn poll_read( - mut self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &mut [u8], - ) -> Poll<io::Result<usize>> { - let me = &mut *self; - let len = buf.len(); - - buf[..].copy_from_slice(&me.val[..len]); - Poll::Ready(Ok(buf.len())) - } - } - - let mut buf = Box::new([0; 8]); - let mut rd = Rd { - val: b"hello world", - }; - - 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_read_line.rs b/tokio/tests/io_read_line.rs deleted file mode 100644 index 99b3b59e..00000000 --- a/tokio/tests/io_read_line.rs +++ /dev/null @@ -1,60 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncRead}; -use tokio_test::assert_ok; - -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -#[tokio::test] -async fn read_line() { - struct Rd { - val: &'static [u8], - } - - impl AsyncRead for Rd { - fn poll_read( - self: Pin<&mut Self>, - _: &mut Context<'_>, - _: &mut [u8], - ) -> Poll<io::Result<usize>> { - unimplemented!() - } - } - - impl AsyncBufRead for Rd { - fn poll_fill_buf<'a>( - self: Pin<&'a mut Self>, - _: &mut Context<'_>, - ) -> Poll<io::Result<&'a [u8]>> { - Poll::Ready(Ok(self.val)) - } - - fn consume(mut self: Pin<&mut Self>, amt: usize) { - self.val = &self.val[amt..]; - } - } - - let mut buf = String::new(); - let mut rd = Rd { - val: b"hello\nworld\n\n", - }; - - let n = assert_ok!(rd.read_line(&mut buf).await); - assert_eq!(n, 6); - assert_eq!(buf, "hello\n"); - buf.clear(); - let n = assert_ok!(rd.read_line(&mut buf).await); - assert_eq!(n, 6); - assert_eq!(buf, "world\n"); - buf.clear(); - let n = assert_ok!(rd.read_line(&mut buf).await); - assert_eq!(n, 1); - assert_eq!(buf, "\n"); - buf.clear(); - let n = assert_ok!(rd.read_line(&mut buf).await); - assert_eq!(n, 0); - assert_eq!(buf, ""); -} diff --git a/tokio/tests/io_read_to_end.rs b/tokio/tests/io_read_to_end.rs deleted file mode 100644 index ac089625..00000000 --- a/tokio/tests/io_read_to_end.rs +++ /dev/null @@ -1,40 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncRead, AsyncReadExt}; -use tokio_test::assert_ok; - -use std::pin::Pin; -use std::task::{Context, Poll}; -use std::{cmp, io}; - -#[tokio::test] -async fn read_to_end() { - struct Rd { - val: &'static [u8], - } - - impl AsyncRead for Rd { - fn poll_read( - mut self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &mut [u8], - ) -> Poll<io::Result<usize>> { - let me = &mut *self; - let len = cmp::min(buf.len(), me.val.len()); - - buf[..len].copy_from_slice(&me.val[..len]); - me.val = &me.val[len..]; - Poll::Ready(Ok(len)) - } - } - - let mut buf = vec![]; - let mut rd = Rd { - val: b"hello world", - }; - - let n = assert_ok!(rd.read_to_end(&mut buf).await); - assert_eq!(n, 11); - assert_eq!(buf[..], b"hello world"[..]); -} diff --git a/tokio/tests/io_read_to_string.rs b/tokio/tests/io_read_to_string.rs deleted file mode 100644 index 4bbee177..00000000 --- a/tokio/tests/io_read_to_string.rs +++ /dev/null @@ -1,40 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncRead, AsyncReadExt}; -use tokio_test::assert_ok; - -use std::pin::Pin; -use std::task::{Context, Poll}; -use std::{cmp, io}; - -#[tokio::test] -async fn read_to_string() { - struct Rd { - val: &'static [u8], - } - - impl AsyncRead for Rd { - fn poll_read( - mut self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &mut [u8], - ) -> Poll<io::Result<usize>> { - let me = &mut *self; - let len = cmp::min(buf.len(), me.val.len()); - - buf[..len].copy_from_slice(&me.val[..len]); - me.val = &me.val[len..]; - Poll::Ready(Ok(len)) - } - } - - let mut buf = String::new(); - let mut rd = Rd { - val: b"hello world", - }; - - let n = assert_ok!(rd.read_to_string(&mut buf).await); - assert_eq!(n, 11); - assert_eq!(buf[..], "hello world"[..]); -} diff --git a/tokio/tests/io_read_until.rs b/tokio/tests/io_read_until.rs deleted file mode 100644 index 22945f55..00000000 --- a/tokio/tests/io_read_until.rs +++ /dev/null @@ -1,56 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncRead}; -use tokio_test::assert_ok; - -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -#[tokio::test] -async fn read_until() { - struct Rd { - val: &'static [u8], - } - - impl AsyncRead for Rd { - fn poll_read( - self: Pin<&mut Self>, - _: &mut Context<'_>, - _: &mut [u8], - ) -> Poll<io::Result<usize>> { - unimplemented!() - } - } - - impl AsyncBufRead for Rd { - fn poll_fill_buf<'a>( - self: Pin<&'a mut Self>, - _: &mut Context<'_>, - ) -> Poll<io::Result<&'a [u8]>> { - Poll::Ready(Ok(self.val)) - } - - fn consume(mut self: Pin<&mut Self>, amt: usize) { - self.val = &self.val[amt..]; - } - } - - let mut buf = vec![]; - let mut rd = Rd { - val: b"hello world", - }; - - let n = assert_ok!(rd.read_until(b' ', &mut buf).await); - assert_eq!(n, 6); - assert_eq!(buf, b"hello "); - buf.clear(); - let n = assert_ok!(rd.read_until(b' ', &mut buf).await); - assert_eq!(n, 5); - assert_eq!(buf, b"world"); - buf.clear(); - let n = assert_ok!(rd.read_until(b' ', &mut buf).await); - assert_eq!(n, 0); - assert_eq!(buf, []); -} diff --git a/tokio/tests/io_write.rs b/tokio/tests/io_write.rs deleted file mode 100644 index 482f21ba..00000000 --- a/tokio/tests/io_write.rs +++ /dev/null @@ -1,47 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncWrite, AsyncWriteExt}; -use tokio_test::assert_ok; - -use bytes::BytesMut; -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -#[tokio::test] -async fn write() { - struct Wr { - buf: BytesMut, - cnt: usize, - } - - impl AsyncWrite for Wr { - fn poll_write( - mut self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &[u8], - ) -> Poll<io::Result<usize>> { - assert_eq!(self.cnt, 0); - self.buf.extend(&buf[0..4]); - Ok(4).into() - } - - fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { - Ok(()).into() - } - - fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { - Ok(()).into() - } - } - - let mut wr = Wr { - buf: BytesMut::with_capacity(64), - cnt: 0, - }; - - let n = assert_ok!(wr.write(b"hello world").await); - assert_eq!(n, 4); - assert_eq!(wr.buf, b"hell"[..]); -} diff --git a/tokio/tests/io_write_all.rs b/tokio/tests/io_write_all.rs deleted file mode 100644 index b12e0f81..00000000 --- a/tokio/tests/io_write_all.rs +++ /dev/null @@ -1,51 +0,0 @@ -#![deny(warnings, rust_2018_idioms)] -#![feature(async_await)] - -use tokio::io::{AsyncWrite, AsyncWriteExt}; -use tokio_test::assert_ok; - -use bytes::BytesMut; -use std::cmp; -use std::io; -use std::pin::Pin; -use std::task::{Context, Poll}; - -#[tokio::test] -async fn write_all() { - struct Wr { - buf: BytesMut, - cnt: usize, - } - - impl AsyncWrite for Wr { - fn poll_write( - mut self: Pin<&mut Self>, - _cx: &mut Context<'_>, - buf: &[u8], - ) -> Poll<io::Result<usize>> { - let n = cmp::min(4, buf.len()); - let buf = &buf[0..n]; - - self.cnt += 1; - self.buf.extend(buf); - Ok(buf.len()).into() - } - - fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { - Ok(()).into() - } - - fn poll_shutdown(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<io::Result<()>> { - Ok(()).into() - } - } - - let mut wr = Wr { - buf: BytesMut::with_capacity(64), - cnt: 0, - }; - - assert_ok!(wr.write_all(b"hello world").await); - assert_eq!(wr.buf, b"hello world"[..]); - assert_eq!(wr.cnt, 3); -} |