Skip to content

Commit

Permalink
chore: refactor ping tests (libp2p#5655)
Browse files Browse the repository at this point in the history
## Description

ref libp2p#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
  • Loading branch information
kamuik16 authored Oct 30, 2024
1 parent 9586071 commit 8387749
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protocols/ping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ tracing = { workspace = true }
void = "1.0"

[dev-dependencies]
async-std = "1.6.2"
libp2p-swarm = { workspace = true, features = ["macros"] }
libp2p-swarm-test = { path = "../../swarm-test" }
quickcheck = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tokio = {workspace = true, features = ["rt", "macros"]}

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
34 changes: 16 additions & 18 deletions protocols/ping/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ mod tests {
Endpoint,
};

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

async_std::task::spawn(async move {
tokio::spawn(async move {
let transport_event = transport.next().await.unwrap();
let (listener_upgrade, _) = transport_event.into_incoming().unwrap();
let conn = listener_upgrade.await.unwrap();
recv_ping(conn).await.unwrap();
});

async_std::task::block_on(async move {
let c = MemoryTransport::new()
.dial(
listener_addr,
DialOpts {
role: Endpoint::Dialer,
port_use: PortUse::Reuse,
},
)
.unwrap()
.await
.unwrap();
let (_, rtt) = send_ping(c).await.unwrap();
assert!(rtt > Duration::from_secs(0));
});
let c = MemoryTransport::new()
.dial(
listener_addr,
DialOpts {
role: Endpoint::Dialer,
port_use: PortUse::Reuse,
},
)
.unwrap()
.await
.unwrap();
let (_, rtt) = send_ping(c).await.unwrap();
assert!(rtt > Duration::from_secs(0));
}
}
16 changes: 8 additions & 8 deletions protocols/ping/tests/ping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ use libp2p_swarm_test::SwarmExt;
use quickcheck::*;
use std::{num::NonZeroU8, time::Duration};

#[test]
fn ping_pong() {
#[tokio::test]
async fn ping_pong() {
fn prop(count: NonZeroU8) {
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));

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

async_std::task::block_on(async {
tokio::spawn(async move {
swarm1.listen().with_memory_addr_external().await;
swarm2.connect(&mut swarm1).await;

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

#[test]
fn unsupported_doesnt_fail() {
#[tokio::test]
async fn unsupported_doesnt_fail() {
let mut swarm1 = Swarm::new_ephemeral(|_| dummy::Behaviour);
let mut swarm2 = Swarm::new_ephemeral(|_| ping::Behaviour::new(ping::Config::new()));

let result = async_std::task::block_on(async {
let result = {
swarm1.listen().with_memory_addr_external().await;
swarm2.connect(&mut swarm1).await;
let swarm1_peer_id = *swarm1.local_peer_id();
async_std::task::spawn(swarm1.loop_on_next());
tokio::spawn(swarm1.loop_on_next());

loop {
match swarm2.next_swarm_event().await {
Expand All @@ -89,7 +89,7 @@ fn unsupported_doesnt_fail() {
_ => {}
}
}
});
};

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

0 comments on commit 8387749

Please sign in to comment.