pub trait SwarmExt {
type NB: NetworkBehaviour;
// Required methods
fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self
where Self: Sized;
fn connect<'life0, 'life1, 'async_trait, T>(
&'life0 mut self,
other: &'life1 mut Swarm<T>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where T: NetworkBehaviour + Send + 'async_trait,
<T as NetworkBehaviour>::ToSwarm: Debug,
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait;
fn dial_and_wait<'life0, 'async_trait>(
&'life0 mut self,
addr: Multiaddr,
) -> Pin<Box<dyn Future<Output = PeerId> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn wait<'life0, 'async_trait, E, P>(
&'life0 mut self,
predicate: P,
) -> Pin<Box<dyn Future<Output = E> + Send + 'async_trait>>
where P: Fn(SwarmEvent<<Self::NB as NetworkBehaviour>::ToSwarm>) -> Option<E> + Send + 'async_trait,
E: 'async_trait,
Self: 'async_trait,
'life0: 'async_trait;
fn listen(&mut self) -> ListenFuture<&mut Self>;
fn next_swarm_event<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = SwarmEvent<<Self::NB as NetworkBehaviour>::ToSwarm>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn next_behaviour_event<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = <Self::NB as NetworkBehaviour>::ToSwarm> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn loop_on_next<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait;
}
Expand description
An extension trait for [Swarm
] that makes it
easier to set up a network of [Swarm
]s for tests.
Required Associated Types§
Required Methods§
Sourcefn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Selfwhere
Self: Sized,
fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Selfwhere
Self: Sized,
Create a new [Swarm
] with an ephemeral identity and the async-std
runtime.
The swarm will use a [libp2p_core::transport::MemoryTransport
] together with a
[libp2p_plaintext::Config
] authentication layer and [libp2p_yamux::Config
] as the
multiplexer. However, these details should not be relied
upon by the test and may change at any time.
Sourcefn connect<'life0, 'life1, 'async_trait, T>(
&'life0 mut self,
other: &'life1 mut Swarm<T>,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
fn connect<'life0, 'life1, 'async_trait, T>( &'life0 mut self, other: &'life1 mut Swarm<T>, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
Establishes a connection to the given [Swarm
], polling both of them until the connection
is established.
This will take addresses from the other
[Swarm
] via [Swarm::external_addresses
].
By default, this iterator will not yield any addresses.
To add listen addresses as external addresses, use
ListenFuture::with_memory_addr_external
or ListenFuture::with_tcp_addr_external
.
Sourcefn dial_and_wait<'life0, 'async_trait>(
&'life0 mut self,
addr: Multiaddr,
) -> Pin<Box<dyn Future<Output = PeerId> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn dial_and_wait<'life0, 'async_trait>(
&'life0 mut self,
addr: Multiaddr,
) -> Pin<Box<dyn Future<Output = PeerId> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Dial the provided address and wait until a connection has been established.
In a normal test scenario, you should prefer SwarmExt::connect
but that is not always
possible. This function only abstracts away the “dial and wait for
ConnectionEstablished
event” part.
Because we don’t have access to the other [Swarm
],
we can’t guarantee that it makes progress.
Sourcefn wait<'life0, 'async_trait, E, P>(
&'life0 mut self,
predicate: P,
) -> Pin<Box<dyn Future<Output = E> + Send + 'async_trait>>
fn wait<'life0, 'async_trait, E, P>( &'life0 mut self, predicate: P, ) -> Pin<Box<dyn Future<Output = E> + Send + 'async_trait>>
Wait for specified condition to return Some
.
Sourcefn listen(&mut self) -> ListenFuture<&mut Self>
fn listen(&mut self) -> ListenFuture<&mut Self>
Listens for incoming connections, polling the [Swarm
] until the
transport is ready to accept connections.
The first address is for the memory transport, the second one for the TCP transport.
Sourcefn next_swarm_event<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = SwarmEvent<<Self::NB as NetworkBehaviour>::ToSwarm>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn next_swarm_event<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = SwarmEvent<<Self::NB as NetworkBehaviour>::ToSwarm>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Returns the next [SwarmEvent
] or times out after 10 seconds.
If the 10s timeout does not fit your usecase, please fall back to StreamExt::next
.
Sourcefn next_behaviour_event<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = <Self::NB as NetworkBehaviour>::ToSwarm> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn next_behaviour_event<'life0, 'async_trait>(
&'life0 mut self,
) -> Pin<Box<dyn Future<Output = <Self::NB as NetworkBehaviour>::ToSwarm> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Returns the next behaviour event or times out after 10 seconds.
If the 10s timeout does not fit your usecase, please fall back to StreamExt::next
.
fn loop_on_next<'async_trait>(
self,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.