pub struct MockStream { /* private fields */ }mock and net only.Expand description
A test-only Read + Write byte stream backed by in-memory buffers — exercise MessageStream
code in unit tests with no real socket. Enabled by the mock feature (put it in your
[dev-dependencies]). Queue inbound bytes with push_inbound and inspect
what was written with written.
Unlike std::io::Cursor it keeps separate read and write buffers (so it handles duplex
request/reply cleanly), and it can deliver inbound bytes a few at a time
(with_chunk_size) — to exercise read_message’s buffer-more-and-retry
loop, i.e. a message split across reads, which Cursor (one read = everything) cannot.
use bnb::{bin, MessageStream, MockStream};
#[bin(big)]
#[derive(Debug, PartialEq, Eq)]
struct Ping {
seq: u16,
}
// deliver the 2-byte Ping one byte per read — forces the read-more loop
let mut conn = MessageStream::new(MockStream::with_chunk_size(1));
conn.get_mut().push_inbound(&Ping { seq: 7 }.to_bytes().unwrap());
let ping: Ping = conn.read_message().unwrap();
assert_eq!(ping, Ping { seq: 7 });
conn.write_message(&Ping { seq: 8 }).unwrap();
assert_eq!(conn.get_mut().written(), &Ping { seq: 8 }.to_bytes().unwrap()[..]);Implementations§
Source§impl MockStream
impl MockStream
Sourcepub fn with_chunk_size(n: usize) -> Self
pub fn with_chunk_size(n: usize) -> Self
Like new, but each read returns at most n bytes (n > 0) — to simulate a
stream that dribbles one message across several reads (the Incomplete / read-more path).
Sourcepub fn push_inbound(&mut self, bytes: &[u8])
pub fn push_inbound(&mut self, bytes: &[u8])
Queue bytes to be returned by future reads.
Sourcepub fn fail_after(self, n: usize) -> Self
pub fn fail_after(self, n: usize) -> Self
After n inbound bytes have been read, every further read fails with ConnectionReset
— to test a connection that drops mid-message (the error surfaces through read_message).
Trait Implementations§
Source§impl Clone for MockStream
impl Clone for MockStream
Source§impl Debug for MockStream
impl Debug for MockStream
Source§impl Default for MockStream
impl Default for MockStream
Source§impl Read for MockStream
impl Read for MockStream
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(
&mut self,
cursor: BorrowedCursor<'_, u8>,
) -> Result<(), Error>
fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read more1.0.0 · Source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
1.0.0 · Source§fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
fn take(self, limit: u64) -> Take<Self>where
Self: Sized,
limit bytes from it. Read moreSource§fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>where
Self: Sized,
read_array)Source§impl Write for MockStream
impl Write for MockStream
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)