pub struct FakeNetwork<Spec: FakeNetworkSpec, CtxId, Links> { /* private fields */ }
Expand description
A fake network, composed of many FakeCoreCtx
s.
Provides a utility to have many contexts keyed by CtxId
that can
exchange frames.
Implementations§
Source§impl<Spec, CtxId, Links> FakeNetwork<Spec, CtxId, Links>
impl<Spec, CtxId, Links> FakeNetwork<Spec, CtxId, Links>
Source§impl<Spec, CtxId, Links> FakeNetwork<Spec, CtxId, Links>where
Spec: FakeNetworkSpec,
CtxId: Eq + Hash + Copy + Debug,
Links: FakeNetworkLinks<Spec::SendMeta, Spec::RecvMeta, CtxId>,
impl<Spec, CtxId, Links> FakeNetwork<Spec, CtxId, Links>where
Spec: FakeNetworkSpec,
CtxId: Eq + Hash + Copy + Debug,
Links: FakeNetworkLinks<Spec::SendMeta, Spec::RecvMeta, CtxId>,
Sourcepub fn new<I: IntoIterator<Item = (CtxId, Spec::Context)>>(
contexts: I,
links: Links,
) -> Self
pub fn new<I: IntoIterator<Item = (CtxId, Spec::Context)>>( contexts: I, links: Links, ) -> Self
Creates a new FakeNetwork
.
Creates a new FakeNetwork
with the collection of FakeCoreCtx
s in
contexts
. Ctx
s are named by type parameter CtxId
.
§Panics
Calls to new
will panic if given a FakeCoreCtx
with timer events.
FakeCoreCtx
s given to FakeNetwork
must not have any timer
events already attached to them, because FakeNetwork
maintains
all the internal timers in dispatchers in sync to enable synchronous
simulation steps.
Sourcepub fn iter_pending_frames(
&self,
) -> impl Iterator<Item = &PendingFrame<CtxId, Spec::RecvMeta>>
pub fn iter_pending_frames( &self, ) -> impl Iterator<Item = &PendingFrame<CtxId, Spec::RecvMeta>>
Iterates over pending frames in an arbitrary order.
Sourcepub fn assert_no_pending_frames(&self)
pub fn assert_no_pending_frames(&self)
Asserts no pending frames exist.
Sourcepub fn drop_pending_frames(&mut self)
pub fn drop_pending_frames(&mut self)
Drops all pending frames; they will not be delivered.
Sourcepub fn step(&mut self) -> StepResult
pub fn step(&mut self) -> StepResult
Performs a single step in network simulation.
step
performs a single logical step in the collection of Ctx
s
held by this FakeNetwork
. A single step consists of the following
operations:
- All pending frames, kept in each
FakeCoreCtx
, are mapped to their destination context/device pairs and moved to an internal collection of pending frames. - The collection of pending timers and scheduled frames is inspected and a simulation time step is retrieved, which will cause a next event to trigger. The simulation time is updated to the new time.
- All scheduled frames whose deadline is less than or equal to the
new simulation time are sent to their destinations, handled using
handle_frame
. - All timer events whose deadline is less than or equal to the new
simulation time are fired, handled using
handle_timer
.
If any new events are created during the operation of frames or
timers, they will not be taken into account in the current
step
. That is, step
collects all the pending events before
dispatching them, ensuring that an infinite loop can’t be created as
a side effect of calling step
.
The return value of step
indicates which of the operations were
performed.
§Panics
If FakeNetwork
was set up with a bad links
, calls to step
may
panic when trying to route frames to their context/device
destinations.
Sourcepub fn step_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>(
&mut self,
filter_map_frame: F,
) -> StepResult
pub fn step_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>( &mut self, filter_map_frame: F, ) -> StepResult
Like FakeNetwork::step
, but receives a function
filter_map_frame
that can modify the an inbound frame before
delivery or drop it altogether by returning None
.
Sourcepub fn step_deliver_frames(&mut self) -> StepResult
pub fn step_deliver_frames(&mut self) -> StepResult
Collects and dispatches all pending frames without advancing time or triggering any timers.
§Panics
Panics under the same conditions as [dispatch_pending_frames
].
Sourcepub fn step_deliver_frames_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>(
&mut self,
filter_map_frame: F,
) -> StepResult
pub fn step_deliver_frames_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>( &mut self, filter_map_frame: F, ) -> StepResult
Like FakeNetwork::step_deliver_frames
, but receives a function
filter_map_frame
that can modify frames, or drop them if it returns
None
.
Sourcepub fn run_until_idle(&mut self)
pub fn run_until_idle(&mut self)
Runs the network until it is starved of events.
§Panics
Panics if 1,000,000 steps are performed without becoming idle.
Also panics under the same conditions as [step
].
Sourcepub fn run_until_idle_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>(
&mut self,
filter_map_frame: F,
)
pub fn run_until_idle_with<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>( &mut self, filter_map_frame: F, )
Like FakeNetwork::run_until_idle
but receives a function
filter_map_frame
that can modify the an inbound frame before
delivery or drop it altogether by returning None
.
Sourcepub fn collect_frames(&mut self)
pub fn collect_frames(&mut self)
Collects all queued frames.
Collects all pending frames and schedules them for delivery to the
destination context/device based on the result of links
. The
collected frames are queued for dispatching in the FakeNetwork
,
ordered by their scheduled delivery time given by the latency result
provided by links
.
Sourcepub fn dispatch_pending_frames<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>(
&mut self,
filter_map_frame: F,
) -> usize
pub fn dispatch_pending_frames<F: FnMut(&mut Spec::Context, Spec::RecvMeta, Buf<Vec<u8>>) -> Option<(Spec::RecvMeta, Buf<Vec<u8>>)>>( &mut self, filter_map_frame: F, ) -> usize
Dispatches scheduled frames that were previously collected with
[collect_frames
].
Only frames for which the current deadline is less than or equal to the current simulation time are delivered.
§Panics
If FakeNetwork
was set up with a bad links
, calls may panic when
trying to route frames to their context/device destinations.
Sourcepub fn next_step(&self) -> Option<FakeInstant>
pub fn next_step(&self) -> Option<FakeInstant>
Calculates the next FakeInstant
when events are available.
Returns the smallest FakeInstant
greater than or equal to the
current time for which an event is available. If no events are
available, returns None
.
Auto Trait Implementations§
impl<Spec, CtxId, Links> Freeze for FakeNetwork<Spec, CtxId, Links>where
Links: Freeze,
impl<Spec, CtxId, Links> RefUnwindSafe for FakeNetwork<Spec, CtxId, Links>where
Links: RefUnwindSafe,
CtxId: RefUnwindSafe,
<Spec as FakeNetworkSpec>::Context: RefUnwindSafe,
<Spec as FakeNetworkSpec>::RecvMeta: RefUnwindSafe,
impl<Spec, CtxId, Links> Send for FakeNetwork<Spec, CtxId, Links>where
Links: Send,
CtxId: Send,
<Spec as FakeNetworkSpec>::Context: Send,
<Spec as FakeNetworkSpec>::RecvMeta: Send,
impl<Spec, CtxId, Links> Sync for FakeNetwork<Spec, CtxId, Links>where
Links: Sync,
CtxId: Sync,
<Spec as FakeNetworkSpec>::Context: Sync,
<Spec as FakeNetworkSpec>::RecvMeta: Sync,
impl<Spec, CtxId, Links> Unpin for FakeNetwork<Spec, CtxId, Links>where
Links: Unpin,
CtxId: Unpin,
<Spec as FakeNetworkSpec>::Context: Unpin,
<Spec as FakeNetworkSpec>::RecvMeta: Unpin,
impl<Spec, CtxId, Links> UnwindSafe for FakeNetwork<Spec, CtxId, Links>where
Links: UnwindSafe,
CtxId: UnwindSafe,
<Spec as FakeNetworkSpec>::Context: UnwindSafe,
<Spec as FakeNetworkSpec>::RecvMeta: UnwindSafe,
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, D> Encode<Ambiguous1, D> for Twhere
D: ResourceDialect,
impl<T, D> Encode<Ambiguous1, D> for Twhere
D: ResourceDialect,
§impl<T, D> Encode<Ambiguous2, D> for Twhere
D: ResourceDialect,
impl<T, D> Encode<Ambiguous2, D> for Twhere
D: ResourceDialect,
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