Skip to content

Commit

Permalink
swarm/: Fix rare test failure of multiple_addresses_err (#2882)
Browse files Browse the repository at this point in the history
In case we accidentally generate the same port twice, we will try to
issue two dial attempts to the same address but also expect two dial
errors which is exactly what this test is trying to catch.
Unfortunately, the assertion is badly written and does not catch
duplicate inputs.
  • Loading branch information
thomaseizinger authored Sep 11, 2022
1 parent 457fb51 commit 66c2755
Showing 1 changed file with 28 additions and 38 deletions.
66 changes: 28 additions & 38 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1624,7 +1624,6 @@ mod tests {
use libp2p_core::transport::TransportEvent;
use libp2p_core::Endpoint;
use quickcheck::{quickcheck, Arbitrary, Gen, QuickCheck};
use rand::prelude::SliceRandom;
use rand::Rng;

// Test execution state.
Expand Down Expand Up @@ -2425,60 +2424,51 @@ mod tests {
assert!(!swarm.is_connected(&peer_id));
}

#[test]
fn multiple_addresses_err() {
#[async_std::test]
async fn multiple_addresses_err() {
// Tries dialing multiple addresses, and makes sure there's one dialing error per address.

let target = PeerId::random();

let mut swarm = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();

let mut addresses = Vec::new();
for _ in 0..3 {
addresses.push(multiaddr![Ip4([0, 0, 0, 0]), Tcp(rand::random::<u16>())]);
}
for _ in 0..5 {
addresses.push(multiaddr![Udp(rand::random::<u16>())]);
}
addresses.shuffle(&mut rand::thread_rng());
let addresses = HashSet::from([
multiaddr![Ip4([0, 0, 0, 0]), Tcp(rand::random::<u16>())],
multiaddr![Ip4([0, 0, 0, 0]), Tcp(rand::random::<u16>())],
multiaddr![Ip4([0, 0, 0, 0]), Tcp(rand::random::<u16>())],
multiaddr![Udp(rand::random::<u16>())],
multiaddr![Udp(rand::random::<u16>())],
multiaddr![Udp(rand::random::<u16>())],
multiaddr![Udp(rand::random::<u16>())],
multiaddr![Udp(rand::random::<u16>())],
]);

swarm
.dial(
DialOpts::peer_id(target)
.addresses(addresses.clone())
.addresses(addresses.iter().cloned().collect())
.build(),
)
.unwrap();

futures::executor::block_on(future::poll_fn(|cx| -> Poll<Result<(), io::Error>> {
loop {
match swarm.poll_next_unpin(cx) {
Poll::Ready(Some(SwarmEvent::OutgoingConnectionError {
peer_id,
// multiaddr,
error: DialError::Transport(errors),
})) => {
assert_eq!(peer_id.unwrap(), target);
match swarm.next().await.unwrap() {
SwarmEvent::OutgoingConnectionError {
peer_id,
// multiaddr,
error: DialError::Transport(errors),
} => {
assert_eq!(target, peer_id.unwrap());

let failed_addresses =
errors.into_iter().map(|(addr, _)| addr).collect::<Vec<_>>();
assert_eq!(
failed_addresses,
addresses
.clone()
.into_iter()
.map(|addr| addr.with(Protocol::P2p(target.into())))
.collect::<Vec<_>>()
);
let failed_addresses = errors.into_iter().map(|(addr, _)| addr).collect::<Vec<_>>();
let expected_addresses = addresses
.into_iter()
.map(|addr| addr.with(Protocol::P2p(target.into())))
.collect::<Vec<_>>();

return Poll::Ready(Ok(()));
}
Poll::Ready(_) => unreachable!(),
Poll::Pending => break Poll::Pending,
}
assert_eq!(expected_addresses, failed_addresses);
}
}))
.unwrap();
e => panic!("Unexpected event: {e:?}"),
}
}

#[test]
Expand Down

0 comments on commit 66c2755

Please sign in to comment.