pub struct MockNetwork {
listening: Mutex<HashMap<SocketAddr, AddrBehavior>>,
}
Expand description
A simulated Internet, for testing.
We simulate TCP streams only, and skip all the details. Connection
are implemented using LocalStream
. The MockNetwork object is
shared by a large set of MockNetworkProviders, each of which has
its own view of its address(es) on the network.
Fields§
§listening: Mutex<HashMap<SocketAddr, AddrBehavior>>
A map from address to the entries about listeners there.
Implementations§
Source§impl MockNetwork
impl MockNetwork
Sourcepub fn builder(self: &Arc<Self>) -> ProviderBuilder
pub fn builder(self: &Arc<Self>) -> ProviderBuilder
Return a ProviderBuilder
for creating a MockNetProvider
§Examples
let client_net = mock_network.builder()
.add_address("198.51.100.6".parse().unwrap())
.add_address("2001:db8::7".parse().unwrap())
.provider();
Sourcepub fn add_blackhole(&self, address: SocketAddr) -> IoResult<()>
pub fn add_blackhole(&self, address: SocketAddr) -> IoResult<()>
Add a “black hole” at the given address, where all traffic will time out.
Sourceasync fn send_connection(
&self,
source_addr: SocketAddr,
target_addr: SocketAddr,
peer_stream: LocalStream,
) -> IoResult<Option<Vec<u8>>>
async fn send_connection( &self, source_addr: SocketAddr, target_addr: SocketAddr, peer_stream: LocalStream, ) -> IoResult<Option<Vec<u8>>>
Tell the listener at target_addr
(if any) about an incoming
connection from source_addr
at peer_stream
.
If the listener is a TLS listener, returns its certificate. Note: Callers should check whether the presence or absence of a certificate matches their expectations.
Returns an error if there isn’t any such listener.
Sourcefn add_listener(
&self,
addr: SocketAddr,
tls_cert: Option<Vec<u8>>,
) -> IoResult<Receiver<(LocalStream, SocketAddr)>>
fn add_listener( &self, addr: SocketAddr, tls_cert: Option<Vec<u8>>, ) -> IoResult<Receiver<(LocalStream, SocketAddr)>>
Register a listener at addr
and return the ConnReceiver
that it should use for connections.
If tls_cert is provided, then the listener is a TLS listener and any only TLS connection attempts should succeed.
Returns an error if the address is already in use.
Trait Implementations§
Source§impl Default for MockNetwork
impl Default for MockNetwork
Source§fn default() -> MockNetwork
fn default() -> MockNetwork
Auto Trait Implementations§
impl !Freeze for MockNetwork
impl RefUnwindSafe for MockNetwork
impl Send for MockNetwork
impl Sync for MockNetwork
impl Unpin for MockNetwork
impl UnwindSafe for MockNetwork
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more