mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-01 05:32:19 +00:00
Previously, a connection would be shut down immediately as soon as its `ConnectionHandler` reports `KeepAlive::No`. As we have gained experience with libp2p, it turned out that this isn't ideal. For one, tests often need to keep connections alive longer than the configured protocols require. Plus, some usecases require connections to be kept alive in general. Both of these needs are currently served by the `keep_alive::Behaviour`. That one does essentially nothing other than statically returning `KeepAlive::Yes` from its `ConnectionHandler`. It makes much more sense to deprecate `keep_alive::Behaviour` and instead allow users to globally configure an `idle_conncetion_timeout` on the `Swarm`. This timeout comes into effect once a `ConnectionHandler` reports `KeepAlive::No`. To start with, this timeout is 0. Together with https://github.com/libp2p/rust-libp2p/issues/3844, this will allow us to move towards a much more aggressive closing of idle connections, together with a more ergonomic way of opting out of this behaviour. Fixes #4121. Pull-Request: #4161.
96 lines
3.5 KiB
Rust
96 lines
3.5 KiB
Rust
// Copyright 2019 Parity Technologies (UK) Ltd.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
// to deal in the Software without restriction, including without limitation
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//! Integration tests for the `Ping` network behaviour.
|
|
|
|
use libp2p_ping as ping;
|
|
use libp2p_swarm::dummy;
|
|
use libp2p_swarm::{Swarm, SwarmEvent};
|
|
use libp2p_swarm_test::SwarmExt;
|
|
use quickcheck::*;
|
|
use std::{num::NonZeroU8, time::Duration};
|
|
|
|
#[test]
|
|
fn ping_pong() {
|
|
fn prop(count: NonZeroU8) {
|
|
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));
|
|
|
|
let mut swarm1 = Swarm::new_ephemeral(|_| ping::Behaviour::new(cfg.clone()));
|
|
let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(cfg.clone()));
|
|
|
|
async_std::task::block_on(async {
|
|
swarm1.listen().await;
|
|
swarm2.connect(&mut swarm1).await;
|
|
|
|
for _ in 0..count.get() {
|
|
let ([e1], [e2]): ([ping::Event; 1], [ping::Event; 1]) =
|
|
libp2p_swarm_test::drive(&mut swarm1, &mut swarm2).await;
|
|
|
|
assert_eq!(&e1.peer, swarm2.local_peer_id());
|
|
assert_eq!(&e2.peer, swarm1.local_peer_id());
|
|
|
|
assert_ping_rtt_less_than_50ms(e1);
|
|
assert_ping_rtt_less_than_50ms(e2);
|
|
}
|
|
});
|
|
}
|
|
|
|
QuickCheck::new().tests(10).quickcheck(prop as fn(_))
|
|
}
|
|
|
|
fn assert_ping_rtt_less_than_50ms(e: ping::Event) {
|
|
let rtt = e.result.expect("a ping success");
|
|
|
|
assert!(rtt < Duration::from_millis(50))
|
|
}
|
|
|
|
#[test]
|
|
fn unsupported_doesnt_fail() {
|
|
let mut swarm1 = Swarm::new_ephemeral(|_| dummy::Behaviour);
|
|
let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(ping::Config::new()));
|
|
|
|
let result = async_std::task::block_on(async {
|
|
swarm1.listen().await;
|
|
swarm2.connect(&mut swarm1).await;
|
|
let swarm1_peer_id = *swarm1.local_peer_id();
|
|
async_std::task::spawn(swarm1.loop_on_next());
|
|
|
|
loop {
|
|
match swarm2.next_swarm_event().await {
|
|
SwarmEvent::Behaviour(ping::Event {
|
|
result: Err(ping::Failure::Unsupported),
|
|
..
|
|
}) => {
|
|
swarm2.disconnect_peer_id(swarm1_peer_id).unwrap();
|
|
}
|
|
SwarmEvent::ConnectionClosed { cause: Some(e), .. } => {
|
|
break Err(e);
|
|
}
|
|
SwarmEvent::ConnectionClosed { cause: None, .. } => {
|
|
break Ok(());
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
});
|
|
|
|
result.expect("node with ping should not fail connection due to unsupported protocol");
|
|
}
|