From 8387749048f9c922dc8a30e18a709eb82ec76503 Mon Sep 17 00:00:00 2001 From: Krishang Shah <93703995+kamuik16@users.noreply.github.com> Date: Wed, 30 Oct 2024 20:17:50 +0530 Subject: [PATCH] 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 --- Cargo.lock | 2 +- protocols/ping/Cargo.toml | 2 +- protocols/ping/src/protocol.rs | 34 ++++++++++++++++------------------ protocols/ping/tests/ping.rs | 16 ++++++++-------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d3e2fc9fa47..c0ada652504 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3023,7 +3023,6 @@ dependencies = [ name = "libp2p-ping" version = "0.45.0" dependencies = [ - "async-std", "either", "futures", "futures-timer", @@ -3033,6 +3032,7 @@ dependencies = [ "libp2p-swarm-test", "quickcheck-ext", "rand 0.8.5", + "tokio", "tracing", "tracing-subscriber", "void", diff --git a/protocols/ping/Cargo.toml b/protocols/ping/Cargo.toml index 66775d3ba8d..794ab54ba42 100644 --- a/protocols/ping/Cargo.toml +++ b/protocols/ping/Cargo.toml @@ -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 diff --git a/protocols/ping/src/protocol.rs b/protocols/ping/src/protocol.rs index 6e3f06d0498..101c219aac4 100644 --- a/protocols/ping/src/protocol.rs +++ b/protocols/ping/src/protocol.rs @@ -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::())]; let mut transport = MemoryTransport::new().boxed(); transport.listen_on(ListenerId::next(), mem_addr).unwrap(); @@ -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)); } } diff --git a/protocols/ping/tests/ping.rs b/protocols/ping/tests/ping.rs index 3ca469f16a8..0752b1fced9 100644 --- a/protocols/ping/tests/ping.rs +++ b/protocols/ping/tests/ping.rs @@ -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; @@ -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 { @@ -89,7 +89,7 @@ fn unsupported_doesnt_fail() { _ => {} } } - }); + }; result.expect("node with ping should not fail connection due to unsupported protocol"); }