Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for is_pending_outbound #1938

Merged
merged 9 commits into from
Feb 10, 2021
24 changes: 24 additions & 0 deletions protocols/request-response/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@ use rand::{self, Rng};
use std::{io, iter};
use std::{collections::HashSet, num::NonZeroU16};

/// Check if is_response_outbound works properly
#[test]
fn pending_connections() {
let ping = Ping("ping".to_string().into_bytes());
let offline_peer = PeerId::random();

let protocols = iter::once((PingProtocol(), ProtocolSupport::Full));
let cfg = RequestResponseConfig::default();

let (peer1_id, trans) = mk_transport();
let ping_proto1 = RequestResponse::throttled(PingCodec(), protocols.clone(), cfg.clone());
let mut swarm1 = Swarm::new(trans, ping_proto1, peer1_id.clone());

let request_id_1 = swarm1.send_request(&offline_peer, ping.clone()).unwrap();

// Poll swarm until request 1 is reported as failed.
swarm1.network.poll();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polling the swarm a single time likely won't do it here.

In case you want to read up on the concept of asynchronous programming in Rust, or the concept of a Future and its poll function, I recommend the async-book.

Below is a concrete example of polling a swarm until some event X happens:

loop {
match swarm2.next().await {
RequestResponseEvent::Message {
peer,
message: RequestResponseMessage::Response { request_id, response }
} => {
count += 1;
assert_eq!(&response, &expected_pong);
assert_eq!(&peer, &peer1_id);
assert_eq!(req_id, request_id);
if count >= num_pings {
return
} else {
req_id = swarm2.send_request(&peer1_id, ping.clone());
}
},
e => panic!("Peer2: Unexpected event: {:?}", e)
}
}


let request_id_2 = swarm1.send_request(&offline_peer, ping.clone()).unwrap();

assert!(!swarm1.is_pending_outbound(&offline_peer, &request_id_1));
assert!(swarm1.is_pending_outbound(&offline_peer, &request_id_2));
}

/// Exercises a simple ping protocol.
#[test]
fn ping_protocol() {
Expand Down