| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| |
|
| | 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().with_memory_addr_external().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().with_memory_addr_external().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"); |
| | } |
| |
|