Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swarm: Fix rare test failure of multiple_addresses_err #2882

Merged
merged 6 commits into from
Sep 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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