Skip to main content

MockDatagramSocket

Struct MockDatagramSocket 

Source
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

Source

pub fn new() -> Self

An empty mock with no queued datagrams.

Source

pub fn push_inbound(&self, bytes: &[u8], from: SocketAddr)

Queue one datagram (bytes, from from) to be returned by the next recv_from.

Source

pub fn sent(&self) -> Vec<(Vec<u8>, SocketAddr)>

Every datagram sent so far, as (bytes, destination), in send order.

Source

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

Source§

type Addr = SocketAddr

The peer-address type (SocketAddr for UDP; std::os::unix::net::SocketAddr for Unix).
Source§

fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>

Receive one datagram into buf, returning how many bytes it held and who sent it. Read more
Source§

fn send_to(&self, buf: &[u8], addr: &SocketAddr) -> Result<usize>

Send buf as one datagram to addr, returning the bytes sent. Read more
Source§

impl Debug for MockDatagramSocket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for MockDatagramSocket

Source§

fn default() -> MockDatagramSocket

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more