fix(test): only listen on 127.0.0.1 for ephemeral swarm

This resolves an issue where our tests were depending on the number of network interfaces available on the local machine.

Related #4110.

Pull-Request: #4122.


  
Co-Authored-By: Thomas Eizinger <thomas@eizinger.io>
This commit is contained in:
Thomas Eizinger 2023-06-27 03:21:47 +02:00 committed by GitHub
parent e32775d6fb
commit d5475fccde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 6 deletions

View File

@ -418,7 +418,7 @@ mod tests {
let ( let (
[SwarmEvent::OutgoingConnectionError { error: DialError::Denied { cause: outgoing_cause }, .. }], [SwarmEvent::OutgoingConnectionError { error: DialError::Denied { cause: outgoing_cause }, .. }],
[_, _, _, SwarmEvent::IncomingConnectionError { error: ListenError::Denied { cause: incoming_cause }, .. }], [_, SwarmEvent::IncomingConnectionError { error: ListenError::Denied { cause: incoming_cause }, .. }],
) = libp2p_swarm_test::drive(&mut dialer, &mut listener).await else { ) = libp2p_swarm_test::drive(&mut dialer, &mut listener).await else {
panic!("unexpected events") panic!("unexpected events")
}; };

View File

@ -59,7 +59,7 @@ async fn two_servers_add_each_other_to_routing_table() {
match libp2p_swarm_test::drive(&mut server2, &mut server1).await { match libp2p_swarm_test::drive(&mut server2, &mut server1).await {
( (
[Identify(_), Kad(RoutingUpdated { peer: peer2, .. }), Identify(_)], [Identify(_), Kad(UnroutablePeer { .. }), Identify(_), Kad(RoutingUpdated { peer: peer2, .. }), Identify(_)],
[Identify(_), Identify(_)], [Identify(_), Identify(_)],
) => { ) => {
assert_eq!(peer2, server1_peer_id); assert_eq!(peer2, server1_peer_id);

View File

@ -148,7 +148,20 @@ async fn run_discovery_test(config: Config) {
async fn create_swarm(config: Config) -> Swarm<Behaviour> { async fn create_swarm(config: Config) -> Swarm<Behaviour> {
let mut swarm = let mut swarm =
Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap()); Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap());
swarm.listen().await;
// Manually listen on all interfaces because mDNS only works for non-loopback addresses.
let expected_listener_id = swarm
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
swarm
.wait(|e| match e {
SwarmEvent::NewListenAddr { listener_id, .. } => {
(listener_id == expected_listener_id).then_some(())
}
_ => None,
})
.await;
swarm swarm
} }

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.use futures::StreamExt; // DEALINGS IN THE SOFTWARE.use futures::StreamExt;
use futures::future::Either; use futures::future::Either;
use libp2p_mdns::{tokio::Behaviour, Config, Event}; use libp2p_mdns::{tokio::Behaviour, Config, Event};
use libp2p_swarm::Swarm; use libp2p_swarm::{Swarm, SwarmEvent};
use libp2p_swarm_test::SwarmExt as _; use libp2p_swarm_test::SwarmExt as _;
use std::time::Duration; use std::time::Duration;
@ -104,7 +104,20 @@ async fn run_discovery_test(config: Config) {
async fn create_swarm(config: Config) -> Swarm<Behaviour> { async fn create_swarm(config: Config) -> Swarm<Behaviour> {
let mut swarm = let mut swarm =
Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap()); Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap());
swarm.listen().await;
// Manually listen on all interfaces because mDNS only works for non-loopback addresses.
let expected_listener_id = swarm
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
.unwrap();
swarm
.wait(|e| match e {
SwarmEvent::NewListenAddr { listener_id, .. } => {
(listener_id == expected_listener_id).then_some(())
}
_ => None,
})
.await;
swarm swarm
} }

View File

@ -312,7 +312,7 @@ where
self.add_external_address(memory_multiaddr.clone()); self.add_external_address(memory_multiaddr.clone());
let tcp_addr_listener_id = self let tcp_addr_listener_id = self
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()) .listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
.unwrap(); .unwrap();
let tcp_multiaddr = self let tcp_multiaddr = self