Skip to content

Commit 80a9e76

Browse files
committed
protocols/relay/tests/v2: Use wait_for_dial to ensure connection reuse
1 parent b6dbf82 commit 80a9e76

File tree

1 file changed

+14
-10
lines changed
  • protocols/relay/tests

1 file changed

+14
-10
lines changed

protocols/relay/tests/v2.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ fn reservation() {
5959

6060
client.listen_on(client_addr.clone()).unwrap();
6161

62+
// Wait for connection to relay.
63+
assert!(pool.run_until(wait_for_dial(&mut client, relay_peer_id)));
64+
6265
// Wait for initial reservation.
6366
pool.run_until(wait_for_reservation(
6467
&mut client,
@@ -103,6 +106,9 @@ fn new_reservation_to_same_relay_replaces_old() {
103106

104107
let old_listener = client.listen_on(client_addr.clone()).unwrap();
105108

109+
// Wait for connection to relay.
110+
assert!(pool.run_until(wait_for_dial(&mut client, relay_peer_id)));
111+
106112
// Wait for first (old) reservation.
107113
pool.run_until(wait_for_reservation(
108114
&mut client,
@@ -192,6 +198,8 @@ fn connect() {
192198

193199
dst.listen_on(dst_addr.clone()).unwrap();
194200

201+
assert!(pool.run_until(wait_for_dial(&mut dst, relay_peer_id)));
202+
195203
pool.run_until(wait_for_reservation(
196204
&mut dst,
197205
dst_addr.clone(),
@@ -272,6 +280,7 @@ fn reuse_connection() {
272280
assert!(pool.run_until(wait_for_dial(&mut client, relay_peer_id)));
273281

274282
client.listen_on(client_addr.clone()).unwrap();
283+
275284
pool.run_until(wait_for_reservation(
276285
&mut client,
277286
client_addr.with(Protocol::P2p(client_peer_id.into())),
@@ -422,25 +431,20 @@ async fn wait_for_reservation(
422431
}
423432
}
424433
SwarmEvent::Behaviour(ClientEvent::Ping(_)) => {}
425-
SwarmEvent::Dialing(peer_id) if peer_id == relay_peer_id => {}
426-
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == relay_peer_id => {}
427434
e => panic!("{:?}", e),
428435
}
429436
}
430437
}
431438

432-
async fn wait_for_dial(client: &mut Swarm<Client>, relay_peer_id: PeerId) -> bool {
439+
async fn wait_for_dial(client: &mut Swarm<Client>, remote: PeerId) -> bool {
433440
loop {
434441
match client.select_next_some().await {
435-
SwarmEvent::Dialing(peer_id) if peer_id == relay_peer_id => {}
436-
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == relay_peer_id => {
437-
return true
438-
}
439-
SwarmEvent::OutgoingConnectionError { peer_id, .. }
440-
if peer_id == Some(relay_peer_id) =>
441-
{
442+
SwarmEvent::Dialing(peer_id) if peer_id == remote => {}
443+
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == remote => return true,
444+
SwarmEvent::OutgoingConnectionError { peer_id, .. } if peer_id == Some(remote) => {
442445
return false
443446
}
447+
SwarmEvent::Behaviour(ClientEvent::Ping(_)) => {}
444448
e => panic!("{:?}", e),
445449
}
446450
}

0 commit comments

Comments
 (0)