pub struct Network<PI, R> { /* private fields */ }
test-utils
only.Expand description
Test network implementation.
A discrete event simulation of a gossip swarm.
Implementations§
Source§impl<PI, R> Network<PI, R>
impl<PI, R> Network<PI, R>
Sourcepub fn new(config: NetworkConfig, rng: R) -> Self
pub fn new(config: NetworkConfig, rng: R) -> Self
Creates a new network.
Source§impl<PI: PeerIdentity + Display, R: Rng + SeedableRng + Clone> Network<PI, R>
impl<PI: PeerIdentity + Display, R: Rng + SeedableRng + Clone> Network<PI, R>
Sourcepub fn insert_with_config(&mut self, peer_id: PI, config: Config)
pub fn insert_with_config(&mut self, peer_id: PI, config: Config)
Inserts a new peer with the specified protocol config.
Panics if the peer already exists.
Sourcepub fn insert_and_join(
&mut self,
peer_id: PI,
topic: TopicId,
bootstrap: Vec<PI>,
)
pub fn insert_and_join( &mut self, peer_id: PI, topic: TopicId, bootstrap: Vec<PI>, )
Inserts a new peer and joins a topic with a set of bootstrap nodes.
Panics if the peer already exists.
Source§impl<PI: PeerIdentity + Display, R: Rng + Clone> Network<PI, R>
impl<PI: PeerIdentity + Display, R: Rng + Clone> Network<PI, R>
Sourcepub fn events(&mut self) -> impl Iterator<Item = (PI, TopicId, Event<PI>)> + '_
pub fn events(&mut self) -> impl Iterator<Item = (PI, TopicId, Event<PI>)> + '_
Drains all queued events.
Sourcepub fn events_sorted(&mut self) -> Vec<(PI, TopicId, Event<PI>)>
pub fn events_sorted(&mut self) -> Vec<(PI, TopicId, Event<PI>)>
Drains all queued events and returns them in a sorted vector.
Sourcepub fn command(&mut self, peer: PI, topic: TopicId, command: Command<PI>)
pub fn command(&mut self, peer: PI, topic: TopicId, command: Command<PI>)
Queues and performs a command.
Sourcepub fn peer_states(&self) -> impl Iterator<Item = &State<PI, R>>
pub fn peer_states(&self) -> impl Iterator<Item = &State<PI, R>>
Returns an iterator over the State
for each peer.
Sourcepub fn peer_ids(&self) -> impl Iterator<Item = PI> + '_
pub fn peer_ids(&self) -> impl Iterator<Item = PI> + '_
Returns an iterator over the node ids of all peers.
Sourcepub fn peer(&self, peer: &PI) -> Option<&State<PI, R>>
pub fn peer(&self, peer: &PI) -> Option<&State<PI, R>>
Returns the State
for a peer, if it exists.
Sourcepub fn neighbors(&self, peer: &PI, topic: &TopicId) -> Option<Vec<PI>>
pub fn neighbors(&self, peer: &PI, topic: &TopicId) -> Option<Vec<PI>>
Returns the neighbors a peer has on the swarm membership layer.
Sourcepub fn elapsed_fmt(&self) -> String
pub fn elapsed_fmt(&self) -> String
Returns the time elapsed since starting the network, formatted as seconds with limited decimals.
Sourcepub fn run_trips(&mut self, n: usize)
pub fn run_trips(&mut self, n: usize)
Runs the simulation for n
times the maximum latency between peers.
Sourcepub fn run_duration(&mut self, timeout: Duration)
pub fn run_duration(&mut self, timeout: Duration)
Runs the simulation for timeout
.
Sourcepub fn run_while(&mut self, f: impl FnMut(PI, TopicId, Event<PI>) -> bool)
pub fn run_while(&mut self, f: impl FnMut(PI, TopicId, Event<PI>) -> bool)
Runs the simulation while f
returns true
.
The callback will be called for each emitted event.
Sourcepub fn run_while_with_timeout(
&mut self,
timeout: Duration,
f: impl FnMut(PI, TopicId, Event<PI>) -> bool,
)
pub fn run_while_with_timeout( &mut self, timeout: Duration, f: impl FnMut(PI, TopicId, Event<PI>) -> bool, )
Runs the simulation while f
returns true
, aborting after timeout
.
The callback will be called for each emitted event.
Sourcepub fn check_synchronicity(&self) -> bool
pub fn check_synchronicity(&self) -> bool
Checks if all neighbor and eager relations are synchronous.
Iterates over all peers, and checks for each peer X:
- that all active view members (neighbors) have X listed as a neighbor as well
- that all eager peers have X listed as eager as well
Returns true
if this is holds, otherwise returns false
.
Logs, at debug level, the cases where the above doesn’t hold.
Sourcepub fn report(&self) -> NetworkReport<PI>
pub fn report(&self) -> NetworkReport<PI>
Returns a report with histograms on active, passive, eager and lazy counts.