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

is_pending_outbound fixes #1928

Merged
merged 8 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions protocols/request-response/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,16 @@ where
/// [`PeerId`] initiated by [`RequestResponse::send_request`] is still
/// pending, i.e. waiting for a response.
pub fn is_pending_outbound(&self, peer: &PeerId, request_id: &RequestId) -> bool {
self.connected.get(peer)
// Check if request is already sent on established connection.
let est_conn = self.connected.get(peer)
.map(|cs| cs.iter().any(|c| c.pending_inbound_responses.contains(request_id)))
.unwrap_or(false)
.unwrap_or(false);
// Check if request is still pending to be sent.
let pen_conn = self.pending_outbound_requests.get(peer)
.map(|rps| rps.iter().any(|rp| {rp.request_id == *request_id}))
.unwrap_or(false);

est_conn || pen_conn
}

/// Checks whether an inbound request from the peer with the provided
Expand Down
8 changes: 7 additions & 1 deletion protocols/request-response/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ fn ping_protocol() {
let addr = rx.next().await.unwrap();
swarm2.add_address(&peer1_id, addr.clone());
let mut req_id = swarm2.send_request(&peer1_id, ping.clone());
assert!(swarm2.is_pending_outbound(&peer1_id, &req_id));

loop {
match swarm2.next().await {
Expand All @@ -105,14 +106,19 @@ fn ping_protocol() {
assert_eq!(&peer, &peer1_id);
assert_eq!(req_id, request_id);
if count >= num_pings {
return
break
} else {
req_id = swarm2.send_request(&peer1_id, ping.clone());
}
},
e => panic!("Peer2: Unexpected event: {:?}", e)
}
}

let req_id2 = swarm2.send_request(&peer1_id, ping.clone());
Copy link
Author

Choose a reason for hiding this comment

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

hi @mxinden, could you give me some pointers on how to come up with a test for this scenario?

// check if is_pending_outbound returns false for a finished request when we have already issued a new one
assert!(swarm2.is_pending_outbound(&peer1_id, &req_id) == false);
assert!(swarm2.is_pending_outbound(&peer1_id, &req_id2));
};

async_std::task::spawn(Box::pin(peer1));
Expand Down