swarm/: Fix rare test failure of multiple_addresses_err (#2882)

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.
This commit is contained in:
Thomas Eizinger
2022-09-11 16:55:26 +10:00
committed by GitHub
parent 457fb51ee0
commit 66c275520d

View File

@ -1624,7 +1624,6 @@ mod tests {
use libp2p_core::transport::TransportEvent; use libp2p_core::transport::TransportEvent;
use libp2p_core::Endpoint; use libp2p_core::Endpoint;
use quickcheck::{quickcheck, Arbitrary, Gen, QuickCheck}; use quickcheck::{quickcheck, Arbitrary, Gen, QuickCheck};
use rand::prelude::SliceRandom;
use rand::Rng; use rand::Rng;
// Test execution state. // Test execution state.
@ -2425,60 +2424,51 @@ mod tests {
assert!(!swarm.is_connected(&peer_id)); assert!(!swarm.is_connected(&peer_id));
} }
#[test] #[async_std::test]
fn multiple_addresses_err() { async fn multiple_addresses_err() {
// Tries dialing multiple addresses, and makes sure there's one dialing error per address. // Tries dialing multiple addresses, and makes sure there's one dialing error per address.
let target = PeerId::random(); let target = PeerId::random();
let mut swarm = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build(); let mut swarm = new_test_swarm::<_, ()>(DummyConnectionHandler::default()).build();
let mut addresses = Vec::new(); let addresses = HashSet::from([
for _ in 0..3 { multiaddr![Ip4([0, 0, 0, 0]), Tcp(rand::random::<u16>())],
addresses.push(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>())],
for _ in 0..5 { multiaddr![Udp(rand::random::<u16>())],
addresses.push(multiaddr![Udp(rand::random::<u16>())]); multiaddr![Udp(rand::random::<u16>())],
} multiaddr![Udp(rand::random::<u16>())],
addresses.shuffle(&mut rand::thread_rng()); multiaddr![Udp(rand::random::<u16>())],
multiaddr![Udp(rand::random::<u16>())],
]);
swarm swarm
.dial( .dial(
DialOpts::peer_id(target) DialOpts::peer_id(target)
.addresses(addresses.clone()) .addresses(addresses.iter().cloned().collect())
.build(), .build(),
) )
.unwrap(); .unwrap();
futures::executor::block_on(future::poll_fn(|cx| -> Poll<Result<(), io::Error>> { match swarm.next().await.unwrap() {
loop { SwarmEvent::OutgoingConnectionError {
match swarm.poll_next_unpin(cx) { peer_id,
Poll::Ready(Some(SwarmEvent::OutgoingConnectionError { // multiaddr,
peer_id, error: DialError::Transport(errors),
// multiaddr, } => {
error: DialError::Transport(errors), assert_eq!(target, peer_id.unwrap());
})) => {
assert_eq!(peer_id.unwrap(), target);
let failed_addresses = let failed_addresses = errors.into_iter().map(|(addr, _)| addr).collect::<Vec<_>>();
errors.into_iter().map(|(addr, _)| addr).collect::<Vec<_>>(); let expected_addresses = addresses
assert_eq!( .into_iter()
failed_addresses, .map(|addr| addr.with(Protocol::P2p(target.into())))
addresses .collect::<Vec<_>>();
.clone()
.into_iter()
.map(|addr| addr.with(Protocol::P2p(target.into())))
.collect::<Vec<_>>()
);
return Poll::Ready(Ok(())); assert_eq!(expected_addresses, failed_addresses);
}
Poll::Ready(_) => unreachable!(),
Poll::Pending => break Poll::Pending,
}
} }
})) e => panic!("Unexpected event: {e:?}"),
.unwrap(); }
} }
#[test] #[test]