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

protocols/autonat: Fix flaky test #2660

Merged
merged 2 commits into from May 22, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 17 additions & 22 deletions protocols/autonat/tests/test_client.rs
Expand Up @@ -189,15 +189,14 @@ async fn test_auto_probe() {
other => panic!("Unexpected behaviour event: {:?}.", other),
};

let mut has_received_response = false;
// Expect inbound dial from server.
let mut had_connection_event = false;
loop {
match client.select_next_some().await {
SwarmEvent::ConnectionEstablished {
endpoint, peer_id, ..
} if endpoint.is_listener() => {
assert_eq!(peer_id, server_id);
break;
had_connection_event = true;
}
SwarmEvent::Behaviour(Event::OutboundProbe(OutboundProbeEvent::Response {
probe_id,
Expand All @@ -206,7 +205,13 @@ async fn test_auto_probe() {
})) => {
assert_eq!(peer, server_id);
assert_eq!(probe_id, id);
has_received_response = true;
}
SwarmEvent::Behaviour(Event::StatusChanged { old, new }) => {
// Expect to flip status to public
assert_eq!(old, NatStatus::Private);
assert!(matches!(new, NatStatus::Public(_)));
assert!(new.is_public());
break;
}
SwarmEvent::IncomingConnection { .. }
| SwarmEvent::NewListenAddr { .. }
Expand All @@ -215,30 +220,20 @@ async fn test_auto_probe() {
}
}

if !has_received_response {
// It can happen that the server observed the established connection and
// returned a response before the inbound established connection was reported at the client.
// In this (rare) case the `ConnectionEstablished` event occurs after the `OutboundProbeEvent::Response`.
if !had_connection_event {
match client.select_next_some().await {
SwarmEvent::Behaviour(Event::OutboundProbe(OutboundProbeEvent::Response {
probe_id,
peer,
..
})) => {
assert_eq!(peer, server_id);
assert_eq!(probe_id, id);
SwarmEvent::ConnectionEstablished {
endpoint, peer_id, ..
} if endpoint.is_listener() => {
assert_eq!(peer_id, server_id);
}
other => panic!("Unexpected swarm event: {:?}.", other),
}
}

// Expect to flip status to public
match next_event(&mut client).await {
Event::StatusChanged { old, new } => {
assert_eq!(old, NatStatus::Private);
assert!(matches!(new, NatStatus::Public(_)));
assert!(new.is_public());
}
other => panic!("Unexpected behaviour event: {:?}.", other),
}

assert_eq!(client.behaviour().confidence(), 0);
assert!(client.behaviour().nat_status().is_public());
assert!(client.behaviour().public_address().is_some());
Expand Down