Skip to content

Commit cd215dc

Browse files
authored
chore: swarm executor should match runtime in tests
We've changed most of our tests to use the `tokio` runtime: libp2p#4449. All of these tests, however, were using `swarm_test::Swarm::new_ephemeral`, which actually creates a `Swarm` with `async_std` executor. This PR changes all test that are using tokio as runtime to use `swarm_test::Swarm::new_ephemeral_tokio`, so that the executor used by the swarm matches the test's executor. It also defaults `swarm-test` to the `tokio` feature. Pull-Request: libp2p#6024.
1 parent 0e6924c commit cd215dc

File tree

25 files changed

+113
-101
lines changed

25 files changed

+113
-101
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ libp2p-server = { version = "0.12.7", path = "misc/server" }
104104
libp2p-stream = { version = "0.3.0-alpha.1", path = "protocols/stream" }
105105
libp2p-swarm = { version = "0.47.0", path = "swarm" }
106106
libp2p-swarm-derive = { version = "=0.35.1", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
107-
libp2p-swarm-test = { version = "0.5.0", path = "swarm-test" }
107+
libp2p-swarm-test = { version = "0.6.0", path = "swarm-test" }
108108
libp2p-tcp = { version = "0.43.0", path = "transports/tcp" }
109109
libp2p-tls = { version = "0.6.2", path = "transports/tls" }
110110
libp2p-uds = { version = "0.42.0", path = "transports/uds" }

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ mod tests {
305305

306306
#[tokio::test]
307307
async fn cannot_dial_blocked_peer() {
308-
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
309-
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
308+
let mut dialer = Swarm::new_ephemeral_tokio(|_| Behaviour::<BlockedPeers>::default());
309+
let mut listener = Swarm::new_ephemeral_tokio(|_| Behaviour::<BlockedPeers>::default());
310310
listener.listen().with_memory_addr_external().await;
311311

312312
dialer.behaviour_mut().block_peer(*listener.local_peer_id());
@@ -319,8 +319,8 @@ mod tests {
319319

320320
#[tokio::test]
321321
async fn can_dial_unblocked_peer() {
322-
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
323-
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
322+
let mut dialer = Swarm::new_ephemeral_tokio(|_| Behaviour::<BlockedPeers>::default());
323+
let mut listener = Swarm::new_ephemeral_tokio(|_| Behaviour::<BlockedPeers>::default());
324324
listener.listen().with_memory_addr_external().await;
325325

326326
dialer.behaviour_mut().block_peer(*listener.local_peer_id());
@@ -333,8 +333,8 @@ mod tests {
333333

334334
#[tokio::test]
335335
async fn blocked_peer_cannot_dial_us() {
336-
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
337-
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
336+
let mut dialer = Swarm::new_ephemeral_tokio(|_| Behaviour::<BlockedPeers>::default());
337+
let mut listener = Swarm::new_ephemeral_tokio(|_| Behaviour::<BlockedPeers>::default());
338338
listener.listen().with_memory_addr_external().await;
339339

340340
listener.behaviour_mut().block_peer(*dialer.local_peer_id());
@@ -355,8 +355,8 @@ mod tests {
355355

356356
#[tokio::test]
357357
async fn connections_get_closed_upon_blocked() {
358-
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
359-
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
358+
let mut dialer = Swarm::new_ephemeral_tokio(|_| Behaviour::<BlockedPeers>::default());
359+
let mut listener = Swarm::new_ephemeral_tokio(|_| Behaviour::<BlockedPeers>::default());
360360
listener.listen().with_memory_addr_external().await;
361361
dialer.connect(&mut listener).await;
362362

@@ -381,8 +381,8 @@ mod tests {
381381

382382
#[tokio::test]
383383
async fn cannot_dial_peer_unless_allowed() {
384-
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
385-
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
384+
let mut dialer = Swarm::new_ephemeral_tokio(|_| Behaviour::<AllowedPeers>::default());
385+
let mut listener = Swarm::new_ephemeral_tokio(|_| Behaviour::<AllowedPeers>::default());
386386
listener.listen().with_memory_addr_external().await;
387387

388388
let DialError::Denied { cause } = dial(&mut dialer, &listener).unwrap_err() else {
@@ -396,8 +396,8 @@ mod tests {
396396

397397
#[tokio::test]
398398
async fn cannot_dial_disallowed_peer() {
399-
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
400-
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
399+
let mut dialer = Swarm::new_ephemeral_tokio(|_| Behaviour::<AllowedPeers>::default());
400+
let mut listener = Swarm::new_ephemeral_tokio(|_| Behaviour::<AllowedPeers>::default());
401401
listener.listen().with_memory_addr_external().await;
402402

403403
dialer.behaviour_mut().allow_peer(*listener.local_peer_id());
@@ -413,8 +413,8 @@ mod tests {
413413

414414
#[tokio::test]
415415
async fn not_allowed_peer_cannot_dial_us() {
416-
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
417-
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
416+
let mut dialer = Swarm::new_ephemeral_tokio(|_| Behaviour::<AllowedPeers>::default());
417+
let mut listener = Swarm::new_ephemeral_tokio(|_| Behaviour::<AllowedPeers>::default());
418418
listener.listen().with_memory_addr_external().await;
419419

420420
dialer
@@ -450,8 +450,8 @@ mod tests {
450450

451451
#[tokio::test]
452452
async fn connections_get_closed_upon_disallow() {
453-
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
454-
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
453+
let mut dialer = Swarm::new_ephemeral_tokio(|_| Behaviour::<AllowedPeers>::default());
454+
let mut listener = Swarm::new_ephemeral_tokio(|_| Behaviour::<AllowedPeers>::default());
455455
listener.listen().with_memory_addr_external().await;
456456
dialer.behaviour_mut().allow_peer(*listener.local_peer_id());
457457
listener.behaviour_mut().allow_peer(*dialer.local_peer_id());

misc/connection-limits/src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ mod tests {
416416

417417
let outgoing_limit = rand::thread_rng().gen_range(1..10);
418418

419-
let mut network = Swarm::new_ephemeral(|_| {
419+
let mut network = Swarm::new_ephemeral_tokio(|_| {
420420
Behaviour::new(
421421
ConnectionLimits::default().with_max_pending_outgoing(Some(outgoing_limit)),
422422
)
@@ -439,8 +439,8 @@ mod tests {
439439
(network, addr, outgoing_limit)
440440
}
441441

442-
#[test]
443-
fn max_outgoing() {
442+
#[tokio::test]
443+
async fn max_outgoing() {
444444
let (mut network, addr, outgoing_limit) = fill_outgoing();
445445
match network
446446
.dial(
@@ -469,8 +469,8 @@ mod tests {
469469
);
470470
}
471471

472-
#[test]
473-
fn outgoing_limit_bypass() {
472+
#[tokio::test]
473+
async fn outgoing_limit_bypass() {
474474
let (mut network, addr, _) = fill_outgoing();
475475
let bypassed_peer = PeerId::random();
476476
network
@@ -513,12 +513,12 @@ mod tests {
513513
#[test]
514514
fn max_established_incoming() {
515515
fn prop(Limit(limit): Limit) {
516-
let mut swarm1 = Swarm::new_ephemeral(|_| {
516+
let mut swarm1 = Swarm::new_ephemeral_tokio(|_| {
517517
Behaviour::new(
518518
ConnectionLimits::default().with_max_established_incoming(Some(limit)),
519519
)
520520
});
521-
let mut swarm2 = Swarm::new_ephemeral(|_| {
521+
let mut swarm2 = Swarm::new_ephemeral_tokio(|_| {
522522
Behaviour::new(
523523
ConnectionLimits::default().with_max_established_incoming(Some(limit)),
524524
)
@@ -556,17 +556,17 @@ mod tests {
556556
#[test]
557557
fn bypass_established_incoming() {
558558
fn prop(Limit(limit): Limit) {
559-
let mut swarm1 = Swarm::new_ephemeral(|_| {
559+
let mut swarm1 = Swarm::new_ephemeral_tokio(|_| {
560560
Behaviour::new(
561561
ConnectionLimits::default().with_max_established_incoming(Some(limit)),
562562
)
563563
});
564-
let mut swarm2 = Swarm::new_ephemeral(|_| {
564+
let mut swarm2 = Swarm::new_ephemeral_tokio(|_| {
565565
Behaviour::new(
566566
ConnectionLimits::default().with_max_established_incoming(Some(limit)),
567567
)
568568
});
569-
let mut swarm3 = Swarm::new_ephemeral(|_| {
569+
let mut swarm3 = Swarm::new_ephemeral_tokio(|_| {
570570
Behaviour::new(
571571
ConnectionLimits::default().with_max_established_incoming(Some(limit)),
572572
)
@@ -623,10 +623,11 @@ mod tests {
623623
/// ([`SwarmEvent::ConnectionEstablished`]) can the connection be seen as established.
624624
#[tokio::test]
625625
async fn support_other_behaviour_denying_connection() {
626-
let mut swarm1 = Swarm::new_ephemeral(|_| {
626+
let mut swarm1 = Swarm::new_ephemeral_tokio(|_| {
627627
Behaviour::new_with_connection_denier(ConnectionLimits::default())
628628
});
629-
let mut swarm2 = Swarm::new_ephemeral(|_| Behaviour::new(ConnectionLimits::default()));
629+
let mut swarm2 =
630+
Swarm::new_ephemeral_tokio(|_| Behaviour::new(ConnectionLimits::default()));
630631

631632
// Have swarm2 dial swarm1.
632633
let (listen_addr, _) = swarm1.listen().await;

misc/memory-connection-limits/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ sysinfo = "0.33"
1818
tracing = { workspace = true }
1919

2020
[dev-dependencies]
21+
tokio = { workspace = true, features = ["macros", "time"] }
2122
libp2p-identify = { workspace = true }
2223
libp2p-swarm-derive = { path = "../../swarm-derive" }
2324
libp2p-swarm-test = { path = "../../swarm-test" }

misc/memory-connection-limits/tests/max_bytes.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ use libp2p_swarm::{dial_opts::DialOpts, DialError, Swarm};
2929
use libp2p_swarm_test::SwarmExt;
3030
use util::*;
3131

32-
#[test]
33-
fn max_bytes() {
32+
#[tokio::test]
33+
async fn max_bytes() {
3434
const CONNECTION_LIMIT: usize = 20;
3535
let max_allowed_bytes = CONNECTION_LIMIT * 1024 * 1024;
3636

37-
let mut network = Swarm::new_ephemeral(|_| TestBehaviour {
37+
let mut network = Swarm::new_ephemeral_tokio(|_| TestBehaviour {
3838
connection_limits: Behaviour::with_max_bytes(max_allowed_bytes),
3939
mem_consumer: ConsumeMemoryBehaviour1MBPending0Established::default(),
4040
});
@@ -69,8 +69,9 @@ fn max_bytes() {
6969
.expect("Unexpected connection limit.");
7070
}
7171

72-
std::thread::sleep(Duration::from_millis(100)); // Memory stats are only updated every 100ms internally, ensure they are up-to-date when we try
73-
// to exceed it.
72+
// Memory stats are only updated every 100ms internally,
73+
// ensure they are up-to-date when we try to exceed it.
74+
tokio::time::sleep(Duration::from_millis(100)).await;
7475

7576
match network
7677
.dial(

misc/memory-connection-limits/tests/max_percentage.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ use libp2p_swarm_test::SwarmExt;
3333
use sysinfo::{MemoryRefreshKind, RefreshKind};
3434
use util::*;
3535

36-
#[test]
37-
fn max_percentage() {
36+
#[tokio::test]
37+
async fn max_percentage() {
3838
const CONNECTION_LIMIT: usize = 20;
3939
let system_info = sysinfo::System::new_with_specifics(
4040
RefreshKind::default().with_memory(MemoryRefreshKind::default().with_ram()),
4141
);
4242

43-
let mut network = Swarm::new_ephemeral(|_| TestBehaviour {
43+
let mut network = Swarm::new_ephemeral_tokio(|_| TestBehaviour {
4444
connection_limits: Behaviour::with_max_percentage(0.1),
4545
mem_consumer: ConsumeMemoryBehaviour1MBPending0Established::default(),
4646
});
@@ -78,7 +78,7 @@ fn max_percentage() {
7878

7979
// Memory stats are only updated every 100ms internally,
8080
// ensure they are up-to-date when we try to exceed it.
81-
std::thread::sleep(Duration::from_millis(100));
81+
tokio::time::sleep(Duration::from_millis(100)).await;
8282

8383
match network
8484
.dial(

protocols/autonat/tests/autonatv2.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ async fn dial_back_to_not_supporting() {
414414
}
415415

416416
async fn new_server() -> Swarm<CombinedServer> {
417-
let mut node = Swarm::new_ephemeral(|identity| CombinedServer {
417+
let mut node = Swarm::new_ephemeral_tokio(|identity| CombinedServer {
418418
autonat: libp2p_autonat::v2::server::Behaviour::default(),
419419
identify: libp2p_identify::Behaviour::new(libp2p_identify::Config::new(
420420
"/libp2p-test/1.0.0".into(),
@@ -427,7 +427,7 @@ async fn new_server() -> Swarm<CombinedServer> {
427427
}
428428

429429
async fn new_client() -> Swarm<CombinedClient> {
430-
let mut node = Swarm::new_ephemeral(|identity| CombinedClient {
430+
let mut node = Swarm::new_ephemeral_tokio(|identity| CombinedClient {
431431
autonat: libp2p_autonat::v2::client::Behaviour::new(
432432
OsRng,
433433
Config::default().with_probe_interval(Duration::from_millis(100)),
@@ -456,7 +456,7 @@ struct CombinedClient {
456456
}
457457

458458
async fn new_dummy() -> Swarm<libp2p_identify::Behaviour> {
459-
let mut node = Swarm::new_ephemeral(|identity| {
459+
let mut node = Swarm::new_ephemeral_tokio(|identity| {
460460
libp2p_identify::Behaviour::new(libp2p_identify::Config::new(
461461
"/libp2p-test/1.0.0".into(),
462462
identity.public().clone(),

protocols/autonat/tests/test_client.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const TEST_REFRESH_INTERVAL: Duration = Duration::from_secs(2);
3535

3636
#[tokio::test]
3737
async fn test_auto_probe() {
38-
let mut client = Swarm::new_ephemeral(|key| {
38+
let mut client = Swarm::new_ephemeral_tokio(|key| {
3939
Behaviour::new(
4040
key.public().to_peer_id(),
4141
Config {
@@ -137,7 +137,7 @@ async fn test_auto_probe() {
137137

138138
#[tokio::test]
139139
async fn test_confidence() {
140-
let mut client = Swarm::new_ephemeral(|key| {
140+
let mut client = Swarm::new_ephemeral_tokio(|key| {
141141
Behaviour::new(
142142
key.public().to_peer_id(),
143143
Config {
@@ -221,7 +221,7 @@ async fn test_confidence() {
221221

222222
#[tokio::test]
223223
async fn test_throttle_server_period() {
224-
let mut client = Swarm::new_ephemeral(|key| {
224+
let mut client = Swarm::new_ephemeral_tokio(|key| {
225225
Behaviour::new(
226226
key.public().to_peer_id(),
227227
Config {
@@ -272,7 +272,7 @@ async fn test_throttle_server_period() {
272272

273273
#[tokio::test]
274274
async fn test_use_connected_as_server() {
275-
let mut client = Swarm::new_ephemeral(|key| {
275+
let mut client = Swarm::new_ephemeral_tokio(|key| {
276276
Behaviour::new(
277277
key.public().to_peer_id(),
278278
Config {
@@ -310,7 +310,7 @@ async fn test_use_connected_as_server() {
310310

311311
#[tokio::test]
312312
async fn test_outbound_failure() {
313-
let mut client = Swarm::new_ephemeral(|key| {
313+
let mut client = Swarm::new_ephemeral_tokio(|key| {
314314
Behaviour::new(
315315
key.public().to_peer_id(),
316316
Config {
@@ -379,7 +379,7 @@ async fn test_outbound_failure() {
379379

380380
#[tokio::test]
381381
async fn test_global_ips_config() {
382-
let mut client = Swarm::new_ephemeral(|key| {
382+
let mut client = Swarm::new_ephemeral_tokio(|key| {
383383
Behaviour::new(
384384
key.public().to_peer_id(),
385385
Config {
@@ -413,7 +413,7 @@ async fn test_global_ips_config() {
413413
}
414414

415415
async fn new_server_swarm() -> (PeerId, Multiaddr, JoinHandle<()>) {
416-
let mut swarm = Swarm::new_ephemeral(|key| {
416+
let mut swarm = Swarm::new_ephemeral_tokio(|key| {
417417
Behaviour::new(
418418
key.public().to_peer_id(),
419419
Config {

protocols/autonat/tests/test_server.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,16 @@ async fn new_server_swarm(config: Option<Config>) -> (Swarm<Behaviour>, PeerId,
359359
// Don't do any outbound probes.
360360
config.boot_delay = Duration::from_secs(60);
361361

362-
let mut server = Swarm::new_ephemeral(|key| Behaviour::new(key.public().to_peer_id(), config));
362+
let mut server =
363+
Swarm::new_ephemeral_tokio(|key| Behaviour::new(key.public().to_peer_id(), config));
363364
let peer_id = *server.local_peer_id();
364365
let (_, addr) = server.listen().await;
365366

366367
(server, peer_id, addr)
367368
}
368369

369370
async fn new_client_swarm(server_id: PeerId, server_addr: Multiaddr) -> (Swarm<Behaviour>, PeerId) {
370-
let mut client = Swarm::new_ephemeral(|key| {
371+
let mut client = Swarm::new_ephemeral_tokio(|key| {
371372
Behaviour::new(
372373
key.public().to_peer_id(),
373374
Config {

0 commit comments

Comments
 (0)