mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-09-25 12:00:35 +00:00

A first pass at updating Tokio to use `std::future`. Implementations of `Future` from the futures crate are updated to implement `Future` from std. Implementations of `Stream` are moved to a feature flag. This commits disables a number of crates that have not yet been updated.
167 lines
3.6 KiB
Rust
167 lines
3.6 KiB
Rust
use tokio_io::AsyncRead;
|
|
use tokio_test::{assert_ready_ok, assert_ready_err};
|
|
use tokio_test::task::MockTask;
|
|
|
|
use bytes::{BufMut, BytesMut};
|
|
use pin_utils::pin_mut;
|
|
use std::io;
|
|
use std::pin::Pin;
|
|
use std::task::{Context, Poll};
|
|
|
|
#[test]
|
|
fn assert_obj_safe() {
|
|
fn _assert<T>() {}
|
|
_assert::<Box<dyn AsyncRead>>();
|
|
}
|
|
|
|
#[test]
|
|
fn read_buf_success() {
|
|
struct Rd;
|
|
|
|
impl AsyncRead for Rd {
|
|
fn poll_read(
|
|
self: Pin<&mut Self>,
|
|
_cx: &mut Context<'_>,
|
|
buf: &mut [u8]) -> Poll<io::Result<usize>>
|
|
{
|
|
buf[0..11].copy_from_slice(b"hello world");
|
|
Poll::Ready(Ok(11))
|
|
}
|
|
}
|
|
|
|
let mut buf = BytesMut::with_capacity(65);
|
|
let mut task = MockTask::new();
|
|
|
|
task.enter(|cx| {
|
|
let rd = Rd;
|
|
pin_mut!(rd);
|
|
|
|
let n = assert_ready_ok!(rd.poll_read_buf(cx, &mut buf));
|
|
|
|
assert_eq!(11, n);
|
|
assert_eq!(buf[..], b"hello world"[..]);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn read_buf_error() {
|
|
struct Rd;
|
|
|
|
impl AsyncRead for Rd {
|
|
fn poll_read(
|
|
self: Pin<&mut Self>,
|
|
_cx: &mut Context<'_>,
|
|
_buf: &mut [u8]) -> Poll<io::Result<usize>>
|
|
{
|
|
let err = io::ErrorKind::Other.into();
|
|
Poll::Ready(Err(err))
|
|
}
|
|
}
|
|
|
|
let mut buf = BytesMut::with_capacity(65);
|
|
let mut task = MockTask::new();
|
|
|
|
task.enter(|cx| {
|
|
let rd = Rd;
|
|
pin_mut!(rd);
|
|
|
|
let err = assert_ready_err!(rd.poll_read_buf(cx, &mut buf));
|
|
assert_eq!(err.kind(), io::ErrorKind::Other);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn read_buf_no_capacity() {
|
|
struct Rd;
|
|
|
|
impl AsyncRead for Rd {
|
|
fn poll_read(
|
|
self: Pin<&mut Self>,
|
|
_cx: &mut Context<'_>,
|
|
_buf: &mut [u8]) -> Poll<io::Result<usize>>
|
|
{
|
|
unimplemented!();
|
|
}
|
|
}
|
|
|
|
// Can't create BytesMut w/ zero capacity, so fill it up
|
|
let mut buf = BytesMut::with_capacity(64);
|
|
let mut task = MockTask::new();
|
|
|
|
buf.put(&[0; 64][..]);
|
|
|
|
task.enter(|cx| {
|
|
let rd = Rd;
|
|
pin_mut!(rd);
|
|
|
|
let n = assert_ready_ok!(rd.poll_read_buf(cx, &mut buf));
|
|
assert_eq!(0, n);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn read_buf_no_uninitialized() {
|
|
struct Rd;
|
|
|
|
impl AsyncRead for Rd {
|
|
fn poll_read(
|
|
self: Pin<&mut Self>,
|
|
_cx: &mut Context<'_>,
|
|
buf: &mut [u8]) -> Poll<io::Result<usize>>
|
|
{
|
|
for b in buf {
|
|
assert_eq!(0, *b);
|
|
}
|
|
|
|
Poll::Ready(Ok(0))
|
|
}
|
|
}
|
|
|
|
let mut buf = BytesMut::with_capacity(64);
|
|
let mut task = MockTask::new();
|
|
|
|
task.enter(|cx| {
|
|
let rd = Rd;
|
|
pin_mut!(rd);
|
|
|
|
let n = assert_ready_ok!(rd.poll_read_buf(cx, &mut buf));
|
|
assert_eq!(0, n);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn read_buf_uninitialized_ok() {
|
|
struct Rd;
|
|
|
|
impl AsyncRead for Rd {
|
|
unsafe fn prepare_uninitialized_buffer(&self, _: &mut [u8]) -> bool {
|
|
false
|
|
}
|
|
|
|
fn poll_read(
|
|
self: Pin<&mut Self>,
|
|
_cx: &mut Context<'_>,
|
|
buf: &mut [u8]) -> Poll<io::Result<usize>>
|
|
{
|
|
assert_eq!(buf[0..11], b"hello world"[..]);
|
|
Poll::Ready(Ok(0))
|
|
}
|
|
}
|
|
|
|
// Can't create BytesMut w/ zero capacity, so fill it up
|
|
let mut buf = BytesMut::with_capacity(64);
|
|
let mut task = MockTask::new();
|
|
|
|
unsafe {
|
|
buf.bytes_mut()[0..11].copy_from_slice(b"hello world");
|
|
}
|
|
|
|
task.enter(|cx| {
|
|
let rd = Rd;
|
|
pin_mut!(rd);
|
|
|
|
let n = assert_ready_ok!(rd.poll_read_buf(cx, &mut buf));
|
|
assert_eq!(0, n);
|
|
});
|
|
}
|