socket_server_mocker

Struct UdpServerMocker

source
pub struct UdpServerMocker { /* private fields */ }
Expand description

A UDP server mocker

Can be used to mock a UDP server if the application you want to test uses UDP sockets to connect to a server.

It’s preferable that only 1 client sends messages to the mocked server. When the object is dropped or a stop instruction is received, the mocked server will stop. The server will also stop in case no more instructions are available.

Implementations§

source§

impl UdpServerMocker

source

pub fn new() -> Result<Self, ServerMockerError>

Create a new instance of the UDP server mocker on a random free port. The port can be retrieved with the ServerMocker::port method.

source

pub fn new_with_port(port: u16) -> Result<Self, ServerMockerError>

Create a new instance of the UDP server mocker on the given port. If the port is already in use, the method will return an error.

source

pub fn new_with_opts( options: UdpMockerOptions, ) -> Result<Self, ServerMockerError>

Create a new instance of the TCP server mocker with the given options.

§Panics

It is assumed that threads can use messages channels without panicking.

source

pub fn options(&self) -> &UdpMockerOptions

Get the options used to create the server mocker

Trait Implementations§

source§

impl ServerMocker for UdpServerMocker

UdpServerMocker implementation

§Example

use std::net::{SocketAddr, UdpSocket};
use socket_server_mocker::ServerMocker;
use socket_server_mocker::Instruction::{ReceiveMessage, SendMessage, StopExchange};
use socket_server_mocker::UdpServerMocker;

let server = UdpServerMocker::new().unwrap();
// 0 = random port
let mut client = UdpSocket::bind("127.0.0.1:0").unwrap();
server.add_mock_instructions(vec![
   ReceiveMessage,
   SendMessage(vec![4, 5, 6]),
   StopExchange,
]).unwrap();

client.send_to(&[1, 2, 3], server.socket_address()).unwrap();
let mut buffer = [0; 3];
client.recv_from(&mut buffer).unwrap();
assert_eq!([4, 5, 6], buffer);
assert_eq!(Some(vec![1, 2, 3]), server.pop_received_message());
assert!(server.pop_server_error().is_none());
source§

fn socket_address(&self) -> SocketAddr

Returns the socket address on which the mock server is listening
source§

fn add_mock_instructions( &self, instructions: Vec<Instruction>, ) -> Result<(), ServerMockerError>

Adds a slice of instructions to the server mocker Read more
source§

fn pop_received_message(&self) -> Option<Vec<u8>>

Return first message received by the mock server on the messages queue Read more
source§

fn pop_server_error(&self) -> Option<ServerMockerError>

Return first error received by the mock server on the errors queue Read more
source§

fn port(&self) -> u16

Returns the port on which the mock server is listening 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, 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.