From ddca74ca51b5e460a148ef472596fa6f9bb774a7 Mon Sep 17 00:00:00 2001 From: shiyasmohd Date: Mon, 10 Feb 2025 19:38:21 +0530 Subject: [PATCH] chore(request-response): migrate tests from async-std to tokio --- Cargo.lock | 2 +- protocols/request-response/Cargo.toml | 9 ++++++--- protocols/request-response/src/cbor.rs | 2 +- protocols/request-response/src/json.rs | 2 +- .../request-response/tests/error_reporting.rs | 16 ++++++++-------- protocols/request-response/tests/peer_address.rs | 4 ++-- protocols/request-response/tests/ping.rs | 10 +++++----- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e1fedcba41b..4e2658b9f8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3247,7 +3247,6 @@ name = "libp2p-request-response" version = "0.28.1" dependencies = [ "anyhow", - "async-std", "async-trait", "cbor4ii", "futures", @@ -3261,6 +3260,7 @@ dependencies = [ "serde", "serde_json", "smallvec", + "tokio", "tracing", "tracing-subscriber", ] diff --git a/protocols/request-response/Cargo.toml b/protocols/request-response/Cargo.toml index cf9e6563848..496fdf3171f 100644 --- a/protocols/request-response/Cargo.toml +++ b/protocols/request-response/Cargo.toml @@ -12,13 +12,16 @@ categories = ["network-programming", "asynchronous"] [dependencies] async-trait = "0.1" -cbor4ii = { version = "0.3.2", features = ["serde1", "use_std"], optional = true } +cbor4ii = { version = "0.3.2", features = [ + "serde1", + "use_std", +], optional = true } futures = { workspace = true } libp2p-core = { workspace = true } libp2p-swarm = { workspace = true } libp2p-identity = { workspace = true } rand = "0.8" -serde = { version = "1.0", optional = true} +serde = { version = "1.0", optional = true } serde_json = { version = "1.0.117", optional = true } smallvec = "1.13.2" tracing = { workspace = true } @@ -30,7 +33,7 @@ cbor = ["dep:serde", "dep:cbor4ii", "libp2p-swarm/macros"] [dev-dependencies] anyhow = "1.0.86" -async-std = { version = "1.6.2", features = ["attributes"] } +tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] } rand = "0.8" libp2p-swarm-test = { path = "../../swarm-test" } futures_ringbuf = "0.4.0" diff --git a/protocols/request-response/src/cbor.rs b/protocols/request-response/src/cbor.rs index 9dea02ef6f1..ef2de4f384f 100644 --- a/protocols/request-response/src/cbor.rs +++ b/protocols/request-response/src/cbor.rs @@ -202,7 +202,7 @@ mod tests { use crate::{cbor::codec::Codec, Codec as _}; - #[async_std::test] + #[tokio::test] async fn test_codec() { let expected_request = TestRequest { payload: "test_payload".to_string(), diff --git a/protocols/request-response/src/json.rs b/protocols/request-response/src/json.rs index 5832c2b3508..bb5dc2a3a3c 100644 --- a/protocols/request-response/src/json.rs +++ b/protocols/request-response/src/json.rs @@ -175,7 +175,7 @@ mod tests { use crate::Codec; - #[async_std::test] + #[tokio::test] async fn test_codec() { let expected_request = TestRequest { payload: "test_payload".to_string(), diff --git a/protocols/request-response/tests/error_reporting.rs b/protocols/request-response/tests/error_reporting.rs index 2108b6006c5..c3894f39e2e 100644 --- a/protocols/request-response/tests/error_reporting.rs +++ b/protocols/request-response/tests/error_reporting.rs @@ -1,7 +1,6 @@ use std::{io, iter, pin::pin, time::Duration}; use anyhow::{bail, Result}; -use async_std::task::sleep; use async_trait::async_trait; use futures::prelude::*; use libp2p_identity::PeerId; @@ -12,9 +11,10 @@ use libp2p_swarm_test::SwarmExt; use request_response::{ Codec, InboundFailure, InboundRequestId, OutboundFailure, OutboundRequestId, ResponseChannel, }; +use tokio::time::sleep; use tracing_subscriber::EnvFilter; -#[async_std::test] +#[tokio::test] async fn report_outbound_failure_on_read_response() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) @@ -70,7 +70,7 @@ async fn report_outbound_failure_on_read_response() { futures::future::select(server_task, client_task).await; } -#[async_std::test] +#[tokio::test] async fn report_outbound_failure_on_write_request() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) @@ -113,7 +113,7 @@ async fn report_outbound_failure_on_write_request() { futures::future::select(server_task, client_task).await; } -#[async_std::test] +#[tokio::test] async fn report_outbound_timeout_on_read_response() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) @@ -160,7 +160,7 @@ async fn report_outbound_timeout_on_read_response() { futures::future::select(server_task, client_task).await; } -#[async_std::test] +#[tokio::test] async fn report_outbound_failure_on_max_streams() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) @@ -212,7 +212,7 @@ async fn report_outbound_failure_on_max_streams() { futures::future::select(swarm1_task, swarm2_task).await; } -#[async_std::test] +#[tokio::test] async fn report_inbound_failure_on_read_request() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) @@ -249,7 +249,7 @@ async fn report_inbound_failure_on_read_request() { futures::future::select(server_task, client_task).await; } -#[async_std::test] +#[tokio::test] async fn report_inbound_failure_on_write_response() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) @@ -315,7 +315,7 @@ async fn report_inbound_failure_on_write_response() { futures::future::select(server_task, client_task).await; } -#[async_std::test] +#[tokio::test] async fn report_inbound_timeout_on_write_response() { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) diff --git a/protocols/request-response/tests/peer_address.rs b/protocols/request-response/tests/peer_address.rs index 603e2d09dc0..cbdef79b9df 100644 --- a/protocols/request-response/tests/peer_address.rs +++ b/protocols/request-response/tests/peer_address.rs @@ -8,7 +8,7 @@ use libp2p_swarm_test::SwarmExt; use serde::{Deserialize, Serialize}; use tracing_subscriber::EnvFilter; -#[async_std::test] +#[tokio::test] #[cfg(feature = "cbor")] async fn dial_succeeds_after_adding_peers_address() { let _ = tracing_subscriber::fmt() @@ -34,7 +34,7 @@ async fn dial_succeeds_after_adding_peers_address() { swarm.dial(peer_id2).unwrap(); - async_std::task::spawn(swarm2.loop_on_next()); + tokio::spawn(swarm2.loop_on_next()); let (connected_peer_id, connected_address) = swarm .wait(|event| match event { diff --git a/protocols/request-response/tests/ping.rs b/protocols/request-response/tests/ping.rs index 94adedac2d7..4cb36891b53 100644 --- a/protocols/request-response/tests/ping.rs +++ b/protocols/request-response/tests/ping.rs @@ -32,7 +32,7 @@ use rand::Rng; use serde::{Deserialize, Serialize}; use tracing_subscriber::EnvFilter; -#[async_std::test] +#[tokio::test] #[cfg(feature = "cbor")] async fn is_response_outbound() { let _ = tracing_subscriber::fmt() @@ -84,7 +84,7 @@ async fn is_response_outbound() { } /// Exercises a simple ping protocol. -#[async_std::test] +#[tokio::test] #[cfg(feature = "cbor")] async fn ping_protocol() { let ping = Ping("ping".to_string().into_bytes()); @@ -176,11 +176,11 @@ async fn ping_protocol() { } }; - async_std::task::spawn(Box::pin(peer1)); + tokio::spawn(Box::pin(peer1)); peer2.await; } -#[async_std::test] +#[tokio::test] #[cfg(feature = "cbor")] async fn emits_inbound_connection_closed_failure() { let ping = Ping("ping".to_string().into_bytes()); @@ -246,7 +246,7 @@ async fn emits_inbound_connection_closed_failure() { /// early close as a protocol violation which results in the connection being closed. /// If the substream were not properly closed when dropped, the sender would instead /// run into a timeout waiting for the response. -#[async_std::test] +#[tokio::test] #[cfg(feature = "cbor")] async fn emits_inbound_connection_closed_if_channel_is_dropped() { let ping = Ping("ping".to_string().into_bytes());