pub struct TcpServerMocker { /* private fields */ }Expand description
A TCP server mocker
Can be used to mock a TCP server if the application you want to test uses TCP sockets to connect to a server.
Only 1 client can be connected to the mocked server. When the connection is closed, the mocked server will stop.
Implementations§
source§impl TcpServerMocker
impl TcpServerMocker
sourcepub fn new() -> Result<Self, ServerMockerError>
pub fn new() -> Result<Self, ServerMockerError>
Create a new instance of the TCP 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 TCP 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 TcpServerMocker
impl ServerMocker for TcpServerMocker
TcpServerMocker implementation
§Example
use std::io::Write;
use std::net::TcpStream;
use socket_server_mocker::server_mocker::ServerMocker;
use socket_server_mocker::server_mocker_instruction::Instruction;
use socket_server_mocker::server_mocker_instruction::Instruction::{ReceiveMessage, StopExchange};
use socket_server_mocker::tcp_server_mocker::TcpServerMocker;
let tcp_server_mocker = TcpServerMocker::new_with_port(1234).unwrap();
let mut client = TcpStream::connect("127.0.0.1:1234").unwrap();
tcp_server_mocker.add_mock_instructions(vec![
ReceiveMessage,
StopExchange,
]).unwrap();
client.write_all(&[1, 2, 3]).unwrap();
let mock_server_received_message = tcp_server_mocker.pop_received_message();
assert_eq!(Some(vec![1, 2, 3]), mock_server_received_message);
assert!(tcp_server_mocker.pop_server_error().is_none());
assert!(tcp_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<BinaryMessage>
fn pop_received_message(&self) -> Option<BinaryMessage>
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_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.
source§const DEFAULT_THREAD_RECEIVER_TIMEOUT_MS: u64 = 100u64
const DEFAULT_THREAD_RECEIVER_TIMEOUT_MS: u64 = 100u64
Timeout if no more instruction is available and
Instruction::StopExchange hasn’t been sentAuto Trait Implementations§
impl Freeze for TcpServerMocker
impl RefUnwindSafe for TcpServerMocker
impl Send for TcpServerMocker
impl !Sync for TcpServerMocker
impl Unpin for TcpServerMocker
impl UnwindSafe for TcpServerMocker
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