Skip to content

Commit 8387749

Browse files
authored
chore: refactor ping tests (#5655)
## Description ref #4449 Refactored ping tests to use `tokio` instead of `async-std`. ## Change checklist - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [x] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates
1 parent 9586071 commit 8387749

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protocols/ping/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ tracing = { workspace = true }
2323
void = "1.0"
2424

2525
[dev-dependencies]
26-
async-std = "1.6.2"
2726
libp2p-swarm = { workspace = true, features = ["macros"] }
2827
libp2p-swarm-test = { path = "../../swarm-test" }
2928
quickcheck = { workspace = true }
3029
tracing-subscriber = { workspace = true, features = ["env-filter"] }
30+
tokio = {workspace = true, features = ["rt", "macros"]}
3131

3232
# Passing arguments to the docsrs builder in order to properly document cfg's.
3333
# More information: https://docs.rs/about/builds#cross-compiling

protocols/ping/src/protocol.rs

+16-18
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ mod tests {
8989
Endpoint,
9090
};
9191

92-
#[test]
93-
fn ping_pong() {
92+
#[tokio::test]
93+
async fn ping_pong() {
9494
let mem_addr = multiaddr![Memory(thread_rng().gen::<u64>())];
9595
let mut transport = MemoryTransport::new().boxed();
9696
transport.listen_on(ListenerId::next(), mem_addr).unwrap();
@@ -101,27 +101,25 @@ mod tests {
101101
.and_then(|ev| ev.into_new_address())
102102
.expect("MemoryTransport not listening on an address!");
103103

104-
async_std::task::spawn(async move {
104+
tokio::spawn(async move {
105105
let transport_event = transport.next().await.unwrap();
106106
let (listener_upgrade, _) = transport_event.into_incoming().unwrap();
107107
let conn = listener_upgrade.await.unwrap();
108108
recv_ping(conn).await.unwrap();
109109
});
110110

111-
async_std::task::block_on(async move {
112-
let c = MemoryTransport::new()
113-
.dial(
114-
listener_addr,
115-
DialOpts {
116-
role: Endpoint::Dialer,
117-
port_use: PortUse::Reuse,
118-
},
119-
)
120-
.unwrap()
121-
.await
122-
.unwrap();
123-
let (_, rtt) = send_ping(c).await.unwrap();
124-
assert!(rtt > Duration::from_secs(0));
125-
});
111+
let c = MemoryTransport::new()
112+
.dial(
113+
listener_addr,
114+
DialOpts {
115+
role: Endpoint::Dialer,
116+
port_use: PortUse::Reuse,
117+
},
118+
)
119+
.unwrap()
120+
.await
121+
.unwrap();
122+
let (_, rtt) = send_ping(c).await.unwrap();
123+
assert!(rtt > Duration::from_secs(0));
126124
}
127125
}

protocols/ping/tests/ping.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ use libp2p_swarm_test::SwarmExt;
2727
use quickcheck::*;
2828
use std::{num::NonZeroU8, time::Duration};
2929

30-
#[test]
31-
fn ping_pong() {
30+
#[tokio::test]
31+
async fn ping_pong() {
3232
fn prop(count: NonZeroU8) {
3333
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));
3434

3535
let mut swarm1 = Swarm::new_ephemeral(|_| ping::Behaviour::new(cfg.clone()));
3636
let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(cfg.clone()));
3737

38-
async_std::task::block_on(async {
38+
tokio::spawn(async move {
3939
swarm1.listen().with_memory_addr_external().await;
4040
swarm2.connect(&mut swarm1).await;
4141

@@ -61,16 +61,16 @@ fn assert_ping_rtt_less_than_50ms(e: ping::Event) {
6161
assert!(rtt < Duration::from_millis(50))
6262
}
6363

64-
#[test]
65-
fn unsupported_doesnt_fail() {
64+
#[tokio::test]
65+
async fn unsupported_doesnt_fail() {
6666
let mut swarm1 = Swarm::new_ephemeral(|_| dummy::Behaviour);
6767
let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(ping::Config::new()));
6868

69-
let result = async_std::task::block_on(async {
69+
let result = {
7070
swarm1.listen().with_memory_addr_external().await;
7171
swarm2.connect(&mut swarm1).await;
7272
let swarm1_peer_id = *swarm1.local_peer_id();
73-
async_std::task::spawn(swarm1.loop_on_next());
73+
tokio::spawn(swarm1.loop_on_next());
7474

7575
loop {
7676
match swarm2.next_swarm_event().await {
@@ -89,7 +89,7 @@ fn unsupported_doesnt_fail() {
8989
_ => {}
9090
}
9191
}
92-
});
92+
};
9393

9494
result.expect("node with ping should not fail connection due to unsupported protocol");
9595
}

0 commit comments

Comments
 (0)