test: introduce libp2p-swarm-test

This patch-set introduces `libp2p-swarm-test`. It provides utilities for quick and safe bootstrapping of tests for `NetworkBehaviour`s. The main design features are:

- Everything has timeouts
- APIs don't get in your way
- Minimal boilerplate

Closes #2884.

Pull-Request: #2888.
This commit is contained in:
Thomas Eizinger
2023-03-08 20:36:35 +11:00
committed by GitHub
parent 8a27375f96
commit 7069d78ee3
24 changed files with 1821 additions and 2134 deletions

View File

@ -296,6 +296,17 @@ pub enum SwarmEvent<TBehaviourOutEvent, THandlerErr> {
Dialing(PeerId),
}
impl<TBehaviourOutEvent, THandlerErr> SwarmEvent<TBehaviourOutEvent, THandlerErr> {
/// Extract the `TBehaviourOutEvent` from this [`SwarmEvent`] in case it is the `Behaviour` variant, otherwise fail.
#[allow(clippy::result_large_err)]
pub fn try_into_behaviour_event(self) -> Result<TBehaviourOutEvent, Self> {
match self {
SwarmEvent::Behaviour(inner) => Ok(inner),
other => Err(other),
}
}
}
/// Contains the state of the network, plus the way it should behave.
///
/// Note: Needs to be polled via `<Swarm as Stream>` in order to make