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.
Trait Implementations
sourceimpl ServerMocker for UdpServerMocker
impl ServerMocker for UdpServerMocker
UdpServerMocker implementation
Example
use std::net::{SocketAddr, UdpSocket};
use socket_server_mocker::server_mocker::ServerMocker;
use socket_server_mocker::server_mocker_instruction::{ServerMockerInstructionsList, ServerMockerInstruction};
use socket_server_mocker::udp_server_mocker::UdpServerMocker;
// 0 = random port
let udp_server_mocker = UdpServerMocker::new(0).unwrap();
let mut client = UdpSocket::bind("127.0.0.1:0").unwrap();
let server_addr = SocketAddr::from(([127, 0, 0, 1], udp_server_mocker.listening_port()));
udp_server_mocker.add_mock_instructions_list(ServerMockerInstructionsList::new_with_instructions([
ServerMockerInstruction::ReceiveMessage,
ServerMockerInstruction::SendMessage(vec![4, 5, 6]),
ServerMockerInstruction::StopExchange,
].as_slice()));
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());
sourcefn listening_port(&self) -> u16
fn listening_port(&self) -> u16
Returns the port on which the mock server is listening Read more
sourcefn add_mock_instructions_list(
&self,
instructions_list: ServerMockerInstructionsList
) -> Result<(), ServerMockerError>
fn add_mock_instructions_list(
&self,
instructions_list: ServerMockerInstructionsList
) -> Result<(), ServerMockerError>
Adds a list of instructions to the server mocker Read more
sourcefn pop_received_message(&self) -> Option<BinaryMessage>
fn pop_received_message(&self) -> Option<BinaryMessage>
Return first message received by the mock server on the messages queue Read more
sourcefn pop_server_error(&self) -> Option<ServerMockerError>
fn pop_server_error(&self) -> Option<ServerMockerError>
sourceconst DEFAULT_NET_TIMEOUT_MS: u64 = 100u64
const DEFAULT_NET_TIMEOUT_MS: u64 = 100u64
Default timeout in milliseconds for the server to wait for a message from the client.
sourceconst DEFAULT_THREAD_RECEIVER_TIMEOUT_MS: u64 = 100u64
const DEFAULT_THREAD_RECEIVER_TIMEOUT_MS: u64 = 100u64
Timeout if no more instruction is available and ServerMockerInstruction::StopExchange hasn’t been sent
sourcefn add_mock_instructions(
&self,
instructions: &[ServerMockerInstruction]
) -> Result<(), ServerMockerError>
fn add_mock_instructions(
&self,
instructions: &[ServerMockerInstruction]
) -> Result<(), ServerMockerError>
Adds a slice of instructions to the server mocker Read more
Auto Trait Implementations
impl !RefUnwindSafe for UdpServerMocker
impl Send for UdpServerMocker
impl !Sync for UdpServerMocker
impl Unpin for UdpServerMocker
impl !UnwindSafe for UdpServerMocker
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more