Skip to content

Commit d5475fc

Browse files
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 <[email protected]>
1 parent e32775d commit d5475fc

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

misc/allow-block-list/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ mod tests {
418418

419419
let (
420420
[SwarmEvent::OutgoingConnectionError { error: DialError::Denied { cause: outgoing_cause }, .. }],
421-
[_, _, _, SwarmEvent::IncomingConnectionError { error: ListenError::Denied { cause: incoming_cause }, .. }],
421+
[_, SwarmEvent::IncomingConnectionError { error: ListenError::Denied { cause: incoming_cause }, .. }],
422422
) = libp2p_swarm_test::drive(&mut dialer, &mut listener).await else {
423423
panic!("unexpected events")
424424
};

protocols/kad/tests/client_mode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ async fn two_servers_add_each_other_to_routing_table() {
5959

6060
match libp2p_swarm_test::drive(&mut server2, &mut server1).await {
6161
(
62-
[Identify(_), Kad(RoutingUpdated { peer: peer2, .. }), Identify(_)],
62+
[Identify(_), Kad(UnroutablePeer { .. }), Identify(_), Kad(RoutingUpdated { peer: peer2, .. }), Identify(_)],
6363
[Identify(_), Identify(_)],
6464
) => {
6565
assert_eq!(peer2, server1_peer_id);

protocols/mdns/tests/use-async-std.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,20 @@ async fn run_discovery_test(config: Config) {
148148
async fn create_swarm(config: Config) -> Swarm<Behaviour> {
149149
let mut swarm =
150150
Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap());
151-
swarm.listen().await;
151+
152+
// Manually listen on all interfaces because mDNS only works for non-loopback addresses.
153+
let expected_listener_id = swarm
154+
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
155+
.unwrap();
156+
157+
swarm
158+
.wait(|e| match e {
159+
SwarmEvent::NewListenAddr { listener_id, .. } => {
160+
(listener_id == expected_listener_id).then_some(())
161+
}
162+
_ => None,
163+
})
164+
.await;
152165

153166
swarm
154167
}

protocols/mdns/tests/use-tokio.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// DEALINGS IN THE SOFTWARE.use futures::StreamExt;
2020
use futures::future::Either;
2121
use libp2p_mdns::{tokio::Behaviour, Config, Event};
22-
use libp2p_swarm::Swarm;
22+
use libp2p_swarm::{Swarm, SwarmEvent};
2323
use libp2p_swarm_test::SwarmExt as _;
2424
use std::time::Duration;
2525

@@ -104,7 +104,20 @@ async fn run_discovery_test(config: Config) {
104104
async fn create_swarm(config: Config) -> Swarm<Behaviour> {
105105
let mut swarm =
106106
Swarm::new_ephemeral(|key| Behaviour::new(config, key.public().to_peer_id()).unwrap());
107-
swarm.listen().await;
107+
108+
// Manually listen on all interfaces because mDNS only works for non-loopback addresses.
109+
let expected_listener_id = swarm
110+
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
111+
.unwrap();
112+
113+
swarm
114+
.wait(|e| match e {
115+
SwarmEvent::NewListenAddr { listener_id, .. } => {
116+
(listener_id == expected_listener_id).then_some(())
117+
}
118+
_ => None,
119+
})
120+
.await;
108121

109122
swarm
110123
}

swarm-test/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ where
312312
self.add_external_address(memory_multiaddr.clone());
313313

314314
let tcp_addr_listener_id = self
315-
.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap())
315+
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
316316
.unwrap();
317317

318318
let tcp_multiaddr = self

0 commit comments

Comments
 (0)