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 Swarms for tests.

Required Associated Types§

Required Methods§

source

fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self
where Self: Sized,

Create a new Swarm with an ephemeral identity.

The swarm will use a MemoryTransport together with a plaintext::Config authentication layer and yamux::Config as the multiplexer. However, these details should not be relied upon by the test and may change at any time.

source

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,

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.

source

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.

source

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,

Wait for specified condition to return Some.

source

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.

source

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.

source

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.

source

fn loop_on_next<'async_trait>( self ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<B> SwarmExt for Swarm<B>

§

type NB = B

source§

fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self
where Self: Sized,

source§

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,

source§

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,

source§

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<<B as NetworkBehaviour>::ToSwarm>) -> Option<E> + Send + 'async_trait, E: 'async_trait, Self: 'async_trait, 'life0: 'async_trait,

source§

fn listen(&mut self) -> ListenFuture<&mut Self>

source§

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,

source§

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,

source§

fn loop_on_next<'async_trait>( self ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait,

Implementors§