pub struct MockDatagramSocket { /* private fields */ }Available on crate features
mock and net only.Expand description
A test-only DatagramSocket backed by in-memory queues — exchange datagrams with a
MessageDatagram in unit tests, no real socket bound. Enabled by the mock feature
(put it in your [dev-dependencies]). Queue inbound datagrams with
push_inbound (each is one recv_from) and inspect what was sent with
sent.
use bnb::{bin, MessageDatagram, MockDatagramSocket};
#[bin(big)]
#[derive(Debug, PartialEq, Eq)]
struct Ping {
seq: u16,
}
let mut peer = MessageDatagram::new(MockDatagramSocket::new());
let from = "127.0.0.1:5000".parse().unwrap();
peer.get_ref().push_inbound(&Ping { seq: 7 }.to_bytes().unwrap(), from); // as if it arrived
let (ping, who): (Ping, _) = peer.recv_message().unwrap();
assert_eq!(ping, Ping { seq: 7 });
peer.send_message(&Ping { seq: 8 }, &who).unwrap(); // reply to the sender
assert_eq!(peer.get_ref().sent()[0].0, Ping { seq: 8 }.to_bytes().unwrap());Implementations§
Source§impl MockDatagramSocket
impl MockDatagramSocket
Sourcepub fn push_inbound(&self, bytes: &[u8], from: SocketAddr)
pub fn push_inbound(&self, bytes: &[u8], from: SocketAddr)
Queue one datagram (bytes, from from) to be returned by the next recv_from.
Sourcepub fn sent(&self) -> Vec<(Vec<u8>, SocketAddr)>
pub fn sent(&self) -> Vec<(Vec<u8>, SocketAddr)>
Every datagram sent so far, as (bytes, destination), in send order.
Sourcepub fn fail_next_recv(self) -> Self
pub fn fail_next_recv(self) -> Self
Make the next recv_from fail with ConnectionReset instead of returning a datagram — to
test recv-error handling. One-shot: later recvs behave normally.
Trait Implementations§
Source§impl DatagramSocket for MockDatagramSocket
impl DatagramSocket for MockDatagramSocket
Source§impl Debug for MockDatagramSocket
impl Debug for MockDatagramSocket
Source§impl Default for MockDatagramSocket
impl Default for MockDatagramSocket
Source§fn default() -> MockDatagramSocket
fn default() -> MockDatagramSocket
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl !Freeze for MockDatagramSocket
impl !RefUnwindSafe for MockDatagramSocket
impl !Sync for MockDatagramSocket
impl Send for MockDatagramSocket
impl Unpin for MockDatagramSocket
impl UnsafeUnpin for MockDatagramSocket
impl UnwindSafe for MockDatagramSocket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more