Skip to content

Commit

Permalink
protocols/autonat: Fix flaky test (libp2p#2480)
Browse files Browse the repository at this point in the history
  • Loading branch information
elenaf9 committed Feb 15, 2022
1 parent 7a5e87e commit 262f0ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
26 changes: 21 additions & 5 deletions tests/test_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ async fn test_auto_probe() {
other => panic!("Unexpected behaviour event: {:?}.", other),
};

let mut has_received_response = false;
// Expect inbound dial from server.
loop {
match client.select_next_some().await {
Expand All @@ -198,19 +199,34 @@ async fn test_auto_probe() {
assert_eq!(peer_id, server_id);
break;
}
SwarmEvent::Behaviour(Event::OutboundProbe(OutboundProbeEvent::Response {
probe_id,
peer,
..
})) => {
assert_eq!(peer, server_id);
assert_eq!(probe_id, id);
has_received_response = true;
}
SwarmEvent::IncomingConnection { .. }
| SwarmEvent::NewListenAddr { .. }
| SwarmEvent::ExpiredListenAddr { .. } => {}
other => panic!("Unexpected swarm event: {:?}.", other),
}
}

match next_event(&mut client).await {
Event::OutboundProbe(OutboundProbeEvent::Response { probe_id, peer, .. }) => {
assert_eq!(peer, server_id);
assert_eq!(probe_id, id);
if !has_received_response {
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);
}
other => panic!("Unexpected swarm event: {:?}.", other),
}
other => panic!("Unexpected behaviour event: {:?}.", other),
}

// Expect to flip status to public
Expand Down
7 changes: 3 additions & 4 deletions tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ async fn spawn_client(
let (tx, rx) = oneshot::channel();
async_std::task::spawn(async move {
let mut client = init_swarm(Config {
boot_delay: Duration::from_millis(100),
refresh_interval: Duration::from_millis(100),
retry_interval: Duration::from_millis(200),
boot_delay: Duration::from_secs(1),
retry_interval: Duration::from_secs(1),
throttle_server_period: Duration::ZERO,
..Default::default()
})
Expand All @@ -98,7 +97,7 @@ async fn spawn_client(
}
}
if add_dummy_external_addr {
let dummy_addr: Multiaddr = "/ip4/127.0.0.1/tcp/42".parse().unwrap();
let dummy_addr: Multiaddr = "/ip4/127.0.0.1/tcp/12345".parse().unwrap();
client.add_external_address(dummy_addr, AddressScore::Infinite);
}
tx.send((peer_id, addr)).unwrap();
Expand Down

0 comments on commit 262f0ac

Please sign in to comment.