summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio-codec/tests/framed.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 14:29:10 +0000
commit2aa4a82499d4becd2284cdb482213d541b8804dd (patch)
treeb80bf8bf13c3766139fbacc530efd0dd9d54394c /third_party/rust/tokio-codec/tests/framed.rs
parentInitial commit. (diff)
downloadfirefox-2aa4a82499d4becd2284cdb482213d541b8804dd.tar.xz
firefox-2aa4a82499d4becd2284cdb482213d541b8804dd.zip
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/tokio-codec/tests/framed.rs')
-rw-r--r--third_party/rust/tokio-codec/tests/framed.rs94
1 files changed, 94 insertions, 0 deletions
diff --git a/third_party/rust/tokio-codec/tests/framed.rs b/third_party/rust/tokio-codec/tests/framed.rs
new file mode 100644
index 0000000000..f7dd9cdf70
--- /dev/null
+++ b/third_party/rust/tokio-codec/tests/framed.rs
@@ -0,0 +1,94 @@
+extern crate tokio_codec;
+extern crate tokio_io;
+extern crate bytes;
+extern crate futures;
+
+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};
+
+const INITIAL_CAPACITY: usize = 8 * 1024;
+
+/// Encode and decode u32 values.
+struct U32Codec;
+
+impl Decoder for U32Codec {
+ type Item = u32;
+ type Error = io::Error;
+
+ fn decode(&mut self, buf: &mut BytesMut) -> io::Result<Option<u32>> {
+ if buf.len() < 4 {
+ return Ok(None);
+ }
+
+ let n = buf.split_to(4).into_buf().get_u32_be();
+ Ok(Some(n))
+ }
+}
+
+impl Encoder for U32Codec {
+ type Item = u32;
+ type Error = io::Error;
+
+ fn encode(&mut self, item: u32, dst: &mut BytesMut) -> io::Result<()> {
+ // Reserve space
+ dst.reserve(4);
+ dst.put_u32_be(item);
+ Ok(())
+ }
+}
+
+/// This value should never be used
+struct DontReadIntoThis;
+
+impl Read for DontReadIntoThis {
+ fn read(&mut self, _: &mut [u8]) -> io::Result<usize> {
+ Err(io::Error::new(io::ErrorKind::Other,
+ "Read into something you weren't supposed to."))
+ }
+}
+
+impl AsyncRead for DontReadIntoThis {}
+
+#[test]
+fn can_read_from_existing_buf() {
+ 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()
+ .map(|(first_num, _)| {
+ first_num.unwrap()
+ })
+ .wait()
+ .map_err(|e| e.0)
+ .unwrap();
+
+ assert_eq!(num, 42);
+}
+
+#[test]
+fn external_buf_grows_to_init() {
+ 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 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);
+}