diff options
author | Carl Lerche <me@carllerche.com> | 2018-06-05 15:31:01 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-06-05 15:31:01 -0700 |
commit | c07a7b26d31cce4ef40c51cca4048a7b5230250d (patch) | |
tree | 6668d44a888b6cfd03a8c9758ebb58d4e7d3d549 /tokio-codec/tests | |
parent | f723d100871e025e4bdd2f47397c9b089e666ce0 (diff) |
Cleanup FramedParts in new tokio-codec (#394)
Diffstat (limited to 'tokio-codec/tests')
-rw-r--r-- | tokio-codec/tests/framed.rs | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/tokio-codec/tests/framed.rs b/tokio-codec/tests/framed.rs index fdedd560..f7dd9cdf 100644 --- a/tokio-codec/tests/framed.rs +++ b/tokio-codec/tests/framed.rs @@ -7,10 +7,11 @@ use futures::{Stream, Future}; use std::io::{self, Read}; use tokio_codec::{Framed, FramedParts, Decoder, Encoder}; use tokio_io::AsyncRead; -use bytes::{BytesMut, Buf, BufMut, IntoBuf, BigEndian}; +use bytes::{BytesMut, Buf, BufMut, IntoBuf}; const INITIAL_CAPACITY: usize = 8 * 1024; +/// Encode and decode u32 values. struct U32Codec; impl Decoder for U32Codec { @@ -39,6 +40,7 @@ impl Encoder for U32Codec { } } +/// This value should never be used struct DontReadIntoThis; impl Read for DontReadIntoThis { @@ -52,12 +54,10 @@ impl AsyncRead for DontReadIntoThis {} #[test] fn can_read_from_existing_buf() { - let parts = FramedParts { - inner: DontReadIntoThis, - readbuf: vec![0, 0, 0, 42].into(), - writebuf: BytesMut::with_capacity(0), - }; - let framed = Framed::from_parts(parts, U32Codec); + let mut parts = FramedParts::new(DontReadIntoThis, U32Codec); + parts.read_buf = vec![0, 0, 0, 42].into(); + + let framed = Framed::from_parts(parts); let num = framed .into_future() @@ -67,31 +67,28 @@ fn can_read_from_existing_buf() { .wait() .map_err(|e| e.0) .unwrap(); + assert_eq!(num, 42); } #[test] fn external_buf_grows_to_init() { - let parts = FramedParts { - inner: DontReadIntoThis, - readbuf: vec![0, 0, 0, 42].into(), - writebuf: BytesMut::with_capacity(0), - }; - let framed = Framed::from_parts(parts, U32Codec); - let FramedParts { readbuf, .. } = framed.into_parts(); - - assert_eq!(readbuf.capacity(), INITIAL_CAPACITY); + let mut parts = FramedParts::new(DontReadIntoThis, U32Codec); + parts.read_buf = vec![0, 0, 0, 42].into(); + + let framed = Framed::from_parts(parts); + let FramedParts { read_buf, .. } = framed.into_parts(); + + assert_eq!(read_buf.capacity(), INITIAL_CAPACITY); } #[test] fn external_buf_does_not_shrink() { - let parts = FramedParts { - inner: DontReadIntoThis, - readbuf: vec![0; INITIAL_CAPACITY * 2].into(), - writebuf: BytesMut::with_capacity(0), - }; - let framed = Framed::from_parts(parts, U32Codec); - let FramedParts { readbuf, .. } = framed.into_parts(); - - assert_eq!(readbuf.capacity(), INITIAL_CAPACITY * 2); + let mut parts = FramedParts::new(DontReadIntoThis, U32Codec); + parts.read_buf = vec![0; INITIAL_CAPACITY * 2].into(); + + let framed = Framed::from_parts(parts); + let FramedParts { read_buf, .. } = framed.into_parts(); + + assert_eq!(read_buf.capacity(), INITIAL_CAPACITY * 2); } |