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::Endpoint;
use quickcheck::{quickcheck, Arbitrary, Gen, QuickCheck};
use rand::prelude::SliceRandom;
use rand::Rng;
// Test execution state.
@ -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]