2019-04-16 15:57:29 +02:00
|
|
|
// 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.
|
|
|
|
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_ping as ping;
|
2022-10-06 03:50:11 +11:00
|
|
|
use libp2p_swarm::keep_alive;
|
2022-12-13 07:58:01 +11:00
|
|
|
use libp2p_swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
2023-03-08 20:36:35 +11:00
|
|
|
use libp2p_swarm_test::SwarmExt;
|
2020-08-10 12:54:55 +02:00
|
|
|
use quickcheck::*;
|
2020-10-16 16:53:02 +02:00
|
|
|
use std::{num::NonZeroU8, time::Duration};
|
2019-04-16 15:57:29 +02:00
|
|
|
|
|
|
|
#[test]
|
2020-08-10 12:54:55 +02:00
|
|
|
fn ping_pong() {
|
2023-03-08 20:36:35 +11:00
|
|
|
fn prop(count: NonZeroU8) {
|
2022-10-06 03:50:11 +11:00
|
|
|
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));
|
2019-04-25 10:33:57 +02:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
let mut swarm1 = Swarm::new_ephemeral(|_| Behaviour::new(cfg.clone()));
|
|
|
|
let mut swarm2 = Swarm::new_ephemeral(|_| Behaviour::new(cfg.clone()));
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
async_std::task::block_on(async {
|
|
|
|
swarm1.listen().await;
|
|
|
|
swarm2.connect(&mut swarm1).await;
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
for _ in 0..count.get() {
|
|
|
|
let (e1, e2) = match libp2p_swarm_test::drive(&mut swarm1, &mut swarm2).await {
|
|
|
|
([BehaviourEvent::Ping(e1)], [BehaviourEvent::Ping(e2)]) => (e1, e2),
|
|
|
|
events => panic!("Unexpected events: {events:?}"),
|
|
|
|
};
|
2019-04-16 15:57:29 +02:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
assert_eq!(&e1.peer, swarm2.local_peer_id());
|
|
|
|
assert_eq!(&e2.peer, swarm1.local_peer_id());
|
2019-11-25 17:33:59 +01:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
assert_ping_rtt_less_than_50ms(e1);
|
|
|
|
assert_ping_rtt_less_than_50ms(e2);
|
2020-08-10 12:54:55 +02:00
|
|
|
}
|
2023-03-08 20:36:35 +11:00
|
|
|
});
|
|
|
|
}
|
2020-08-10 12:54:55 +02:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
QuickCheck::new().tests(10).quickcheck(prop as fn(_))
|
|
|
|
}
|
2020-08-10 12:54:55 +02:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
fn assert_ping_rtt_less_than_50ms(e: ping::Event) {
|
2023-05-24 14:33:18 +02:00
|
|
|
let rtt = e.result.expect("a ping success");
|
2020-08-10 12:54:55 +02:00
|
|
|
|
2023-05-24 14:33:18 +02:00
|
|
|
assert!(rtt < Duration::from_millis(50))
|
2019-04-16 15:57:29 +02:00
|
|
|
}
|
|
|
|
|
2021-07-31 06:21:21 +10:00
|
|
|
#[test]
|
|
|
|
fn unsupported_doesnt_fail() {
|
2023-03-08 20:36:35 +11:00
|
|
|
let mut swarm1 = Swarm::new_ephemeral(|_| keep_alive::Behaviour);
|
|
|
|
let mut swarm2 = Swarm::new_ephemeral(|_| Behaviour::new(ping::Config::new()));
|
2021-07-31 06:21:21 +10:00
|
|
|
|
2023-03-08 20:36:35 +11:00
|
|
|
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());
|
2021-07-31 06:21:21 +10:00
|
|
|
|
|
|
|
loop {
|
2023-03-08 20:36:35 +11:00
|
|
|
match swarm2.next_swarm_event().await {
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping::Event {
|
2021-09-07 00:10:48 +10:00
|
|
|
result: Err(ping::Failure::Unsupported),
|
2021-07-31 06:21:21 +10:00
|
|
|
..
|
2022-10-06 03:50:11 +11:00
|
|
|
})) => {
|
2023-03-08 20:36:35 +11:00
|
|
|
swarm2.disconnect_peer_id(swarm1_peer_id).unwrap();
|
2021-07-31 06:21:21 +10:00
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2022-10-06 03:50:11 +11:00
|
|
|
#[derive(NetworkBehaviour, Default)]
|
2022-12-13 07:58:01 +11:00
|
|
|
#[behaviour(prelude = "libp2p_swarm::derive_prelude")]
|
2022-10-06 03:50:11 +11:00
|
|
|
struct Behaviour {
|
|
|
|
keep_alive: keep_alive::Behaviour,
|
|
|
|
ping: ping::Behaviour,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Behaviour {
|
|
|
|
fn new(config: ping::Config) -> Self {
|
|
|
|
Self {
|
|
|
|
keep_alive: keep_alive::Behaviour,
|
|
|
|
ping: ping::Behaviour::new(config),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|