test(rewind): re-enable common::io::Rewind tests
This commit is contained in:
@@ -95,94 +95,65 @@ where
|
|||||||
mod tests {
|
mod tests {
|
||||||
// FIXME: re-implement tests with `async/await`, this import should
|
// FIXME: re-implement tests with `async/await`, this import should
|
||||||
// trigger a warning to remind us
|
// trigger a warning to remind us
|
||||||
|
use bytes::Bytes;
|
||||||
|
use tokio::io::AsyncReadExt;
|
||||||
use super::Rewind;
|
use super::Rewind;
|
||||||
/*
|
|
||||||
use super::*;
|
|
||||||
use tokio_mockstream::MockStream;
|
|
||||||
use std::io::Cursor;
|
|
||||||
|
|
||||||
// Test a partial rewind
|
#[tokio::test]
|
||||||
#[test]
|
async fn partial_rewind() {
|
||||||
fn async_partial_rewind() {
|
let underlying = [104, 101, 108, 108, 111];
|
||||||
let bs = &mut [104, 101, 108, 108, 111];
|
|
||||||
let o1 = &mut [0, 0];
|
let mock = tokio_test::io::Builder::new()
|
||||||
let o2 = &mut [0, 0, 0, 0, 0];
|
.read(&underlying)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let mut stream = Rewind::new(mock);
|
||||||
|
|
||||||
let mut stream = Rewind::new(MockStream::new(bs));
|
|
||||||
let mut o1_cursor = Cursor::new(o1);
|
|
||||||
// Read off some bytes, ensure we filled o1
|
// Read off some bytes, ensure we filled o1
|
||||||
match stream.read_buf(&mut o1_cursor).unwrap() {
|
let mut buf = [0; 2];
|
||||||
Async::NotReady => panic!("should be ready"),
|
stream
|
||||||
Async::Ready(cnt) => assert_eq!(2, cnt),
|
.read_exact(&mut buf)
|
||||||
}
|
.await
|
||||||
|
.expect("read1");
|
||||||
|
|
||||||
|
|
||||||
// Rewind the stream so that it is as if we never read in the first place.
|
// Rewind the stream so that it is as if we never read in the first place.
|
||||||
let read_buf = Bytes::from(&o1_cursor.into_inner()[..]);
|
stream.rewind(Bytes::from(&buf[..]));
|
||||||
stream.rewind(read_buf);
|
|
||||||
|
|
||||||
// We poll 2x here since the first time we'll only get what is in the
|
let mut buf = [0; 5];
|
||||||
// prefix (the rewinded part) of the Rewind.\
|
stream
|
||||||
let mut o2_cursor = Cursor::new(o2);
|
.read_exact(&mut buf)
|
||||||
stream.read_buf(&mut o2_cursor).unwrap();
|
.await
|
||||||
stream.read_buf(&mut o2_cursor).unwrap();
|
.expect("read1");
|
||||||
let o2_final = o2_cursor.into_inner();
|
|
||||||
|
|
||||||
// At this point we should have read everything that was in the MockStream
|
// At this point we should have read everything that was in the MockStream
|
||||||
assert_eq!(&o2_final, &bs);
|
assert_eq!(&buf, &underlying);
|
||||||
}
|
}
|
||||||
// Test a full rewind
|
|
||||||
#[test]
|
|
||||||
fn async_full_rewind() {
|
|
||||||
let bs = &mut [104, 101, 108, 108, 111];
|
|
||||||
let o1 = &mut [0, 0, 0, 0, 0];
|
|
||||||
let o2 = &mut [0, 0, 0, 0, 0];
|
|
||||||
|
|
||||||
let mut stream = Rewind::new(MockStream::new(bs));
|
#[tokio::test]
|
||||||
let mut o1_cursor = Cursor::new(o1);
|
async fn full_rewind() {
|
||||||
match stream.read_buf(&mut o1_cursor).unwrap() {
|
let underlying = [104, 101, 108, 108, 111];
|
||||||
Async::NotReady => panic!("should be ready"),
|
|
||||||
Async::Ready(cnt) => assert_eq!(5, cnt),
|
|
||||||
}
|
|
||||||
|
|
||||||
let read_buf = Bytes::from(&o1_cursor.into_inner()[..]);
|
let mock = tokio_test::io::Builder::new()
|
||||||
stream.rewind(read_buf);
|
.read(&underlying)
|
||||||
|
.build();
|
||||||
|
|
||||||
let mut o2_cursor = Cursor::new(o2);
|
let mut stream = Rewind::new(mock);
|
||||||
stream.read_buf(&mut o2_cursor).unwrap();
|
|
||||||
stream.read_buf(&mut o2_cursor).unwrap();
|
|
||||||
let o2_final = o2_cursor.into_inner();
|
|
||||||
|
|
||||||
assert_eq!(&o2_final, &bs);
|
let mut buf = [0; 5];
|
||||||
|
stream
|
||||||
|
.read_exact(&mut buf)
|
||||||
|
.await
|
||||||
|
.expect("read1");
|
||||||
|
|
||||||
|
|
||||||
|
// Rewind the stream so that it is as if we never read in the first place.
|
||||||
|
stream.rewind(Bytes::from(&buf[..]));
|
||||||
|
|
||||||
|
let mut buf = [0; 5];
|
||||||
|
stream
|
||||||
|
.read_exact(&mut buf)
|
||||||
|
.await
|
||||||
|
.expect("read1");
|
||||||
}
|
}
|
||||||
#[test]
|
|
||||||
fn partial_rewind() {
|
|
||||||
let bs = &mut [104, 101, 108, 108, 111];
|
|
||||||
let o1 = &mut [0, 0];
|
|
||||||
let o2 = &mut [0, 0, 0, 0, 0];
|
|
||||||
|
|
||||||
let mut stream = Rewind::new(MockStream::new(bs));
|
|
||||||
stream.read(o1).unwrap();
|
|
||||||
|
|
||||||
let read_buf = Bytes::from(&o1[..]);
|
|
||||||
stream.rewind(read_buf);
|
|
||||||
let cnt = stream.read(o2).unwrap();
|
|
||||||
stream.read(&mut o2[cnt..]).unwrap();
|
|
||||||
assert_eq!(&o2, &bs);
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn full_rewind() {
|
|
||||||
let bs = &mut [104, 101, 108, 108, 111];
|
|
||||||
let o1 = &mut [0, 0, 0, 0, 0];
|
|
||||||
let o2 = &mut [0, 0, 0, 0, 0];
|
|
||||||
|
|
||||||
let mut stream = Rewind::new(MockStream::new(bs));
|
|
||||||
stream.read(o1).unwrap();
|
|
||||||
|
|
||||||
let read_buf = Bytes::from(&o1[..]);
|
|
||||||
stream.rewind(read_buf);
|
|
||||||
let cnt = stream.read(o2).unwrap();
|
|
||||||
stream.read(&mut o2[cnt..]).unwrap();
|
|
||||||
assert_eq!(&o2, &bs);
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user