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
impl UdpServerMocker
sourcepub fn new() -> Result<Self, ServerMockerError>
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.
sourcepub fn new_with_port(port: u16) -> Result<Self, ServerMockerError>
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.
Trait Implementations§
source§impl ServerMocker for UdpServerMocker
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 udp_server_mocker = UdpServerMocker::new().unwrap();
// 0 = random port
let mut client = UdpSocket::bind("127.0.0.1:0").unwrap();
let server_addr = SocketAddr::from(([127, 0, 0, 1], udp_server_mocker.port()));
udp_server_mocker.add_mock_instructions(vec![
ReceiveMessage,
SendMessage(vec![4, 5, 6]),
StopExchange,
]).unwrap();
client.send_to(&[1, 2, 3], server_addr).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]), udp_server_mocker.pop_received_message());
assert!(udp_server_mocker.pop_server_error().is_none());
source§fn socket_address(&self) -> SocketAddr
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>
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>>
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>
fn pop_server_error(&self) -> Option<ServerMockerError>
source§const DEFAULT_NET_TIMEOUT: Duration = _
const DEFAULT_NET_TIMEOUT: Duration = _
Default timeout for the server to wait for a message from the client.
source§const DEFAULT_THREAD_RECEIVER_TIMEOUT: Duration = _
const DEFAULT_THREAD_RECEIVER_TIMEOUT: Duration = _
Timeout if no more instruction is available and
Instruction::StopExchange
hasn’t been sentAuto Trait Implementations§
impl Freeze for UdpServerMocker
impl RefUnwindSafe for UdpServerMocker
impl Send for UdpServerMocker
impl !Sync for UdpServerMocker
impl Unpin for UdpServerMocker
impl UnwindSafe for UdpServerMocker
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