Skip to content

Commit 547fc37

Browse files
authored
chore(request-response): migrate tests from async-std to tokio
Migrating tests in protocols/request-response from async_std to tokio as per #4449 Pull-Request: #5857.
1 parent d3e88cf commit 547fc37

File tree

7 files changed

+32
-31
lines changed

7 files changed

+32
-31
lines changed

Cargo.lock

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

protocols/request-response/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ libp2p-core = { workspace = true }
1818
libp2p-swarm = { workspace = true }
1919
libp2p-identity = { workspace = true }
2020
rand = "0.8"
21-
serde = { version = "1.0", optional = true}
21+
serde = { version = "1.0", optional = true }
2222
serde_json = { version = "1.0.117", optional = true }
2323
smallvec = "1.13.2"
2424
tracing = { workspace = true }
@@ -30,7 +30,7 @@ cbor = ["dep:serde", "dep:cbor4ii", "libp2p-swarm/macros"]
3030

3131
[dev-dependencies]
3232
anyhow = "1.0.86"
33-
async-std = { version = "1.6.2", features = ["attributes"] }
33+
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }
3434
rand = "0.8"
3535
libp2p-swarm-test = { path = "../../swarm-test", features = ["async-std"]}
3636
futures_ringbuf = "0.4.0"

protocols/request-response/src/cbor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ mod tests {
205205

206206
use crate::{cbor::codec::Codec, Codec as _};
207207

208-
#[async_std::test]
208+
#[tokio::test]
209209
async fn test_codec() {
210210
let expected_request = TestRequest {
211211
payload: "test_payload".to_string(),

protocols/request-response/src/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ mod tests {
184184

185185
use crate::Codec;
186186

187-
#[async_std::test]
187+
#[tokio::test]
188188
async fn test_codec() {
189189
let expected_request = TestRequest {
190190
payload: "test_payload".to_string(),

protocols/request-response/tests/error_reporting.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use std::{io, iter, pin::pin, time::Duration};
22

33
use anyhow::{bail, Result};
4-
use async_std::task::sleep;
54
use async_trait::async_trait;
6-
use futures::prelude::*;
5+
use futures::{future::pending, prelude::*};
76
use libp2p_identity::PeerId;
87
use libp2p_request_response as request_response;
98
use libp2p_request_response::ProtocolSupport;
@@ -14,7 +13,7 @@ use request_response::{
1413
};
1514
use tracing_subscriber::EnvFilter;
1615

17-
#[async_std::test]
16+
#[tokio::test]
1817
async fn report_outbound_failure_on_read_response() {
1918
let _ = tracing_subscriber::fmt()
2019
.with_env_filter(EnvFilter::from_default_env())
@@ -70,7 +69,7 @@ async fn report_outbound_failure_on_read_response() {
7069
futures::future::select(server_task, client_task).await;
7170
}
7271

73-
#[async_std::test]
72+
#[tokio::test]
7473
async fn report_outbound_failure_on_write_request() {
7574
let _ = tracing_subscriber::fmt()
7675
.with_env_filter(EnvFilter::from_default_env())
@@ -113,7 +112,7 @@ async fn report_outbound_failure_on_write_request() {
113112
futures::future::select(server_task, client_task).await;
114113
}
115114

116-
#[async_std::test]
115+
#[tokio::test]
117116
async fn report_outbound_timeout_on_read_response() {
118117
let _ = tracing_subscriber::fmt()
119118
.with_env_filter(EnvFilter::from_default_env())
@@ -160,7 +159,7 @@ async fn report_outbound_timeout_on_read_response() {
160159
futures::future::select(server_task, client_task).await;
161160
}
162161

163-
#[async_std::test]
162+
#[tokio::test]
164163
async fn report_outbound_failure_on_max_streams() {
165164
let _ = tracing_subscriber::fmt()
166165
.with_env_filter(EnvFilter::from_default_env())
@@ -212,7 +211,7 @@ async fn report_outbound_failure_on_max_streams() {
212211
futures::future::select(swarm1_task, swarm2_task).await;
213212
}
214213

215-
#[async_std::test]
214+
#[tokio::test]
216215
async fn report_inbound_failure_on_read_request() {
217216
let _ = tracing_subscriber::fmt()
218217
.with_env_filter(EnvFilter::from_default_env())
@@ -249,7 +248,7 @@ async fn report_inbound_failure_on_read_request() {
249248
futures::future::select(server_task, client_task).await;
250249
}
251250

252-
#[async_std::test]
251+
#[tokio::test]
253252
async fn report_inbound_failure_on_write_response() {
254253
let _ = tracing_subscriber::fmt()
255254
.with_env_filter(EnvFilter::from_default_env())
@@ -315,7 +314,7 @@ async fn report_inbound_failure_on_write_response() {
315314
futures::future::select(server_task, client_task).await;
316315
}
317316

318-
#[async_std::test]
317+
#[tokio::test]
319318
async fn report_inbound_timeout_on_write_response() {
320319
let _ = tracing_subscriber::fmt()
321320
.with_env_filter(EnvFilter::from_default_env())
@@ -465,9 +464,10 @@ impl Codec for TestCodec {
465464

466465
match buf[0].try_into()? {
467466
Action::FailOnReadResponse => Err(io::Error::other("FailOnReadResponse")),
468-
Action::TimeoutOnReadResponse => loop {
469-
sleep(Duration::MAX).await;
470-
},
467+
Action::TimeoutOnReadResponse => {
468+
pending::<()>().await;
469+
Err(io::Error::other("FailOnReadResponse"))
470+
}
471471
action => Ok(action),
472472
}
473473
}
@@ -502,9 +502,10 @@ impl Codec for TestCodec {
502502
{
503503
match res {
504504
Action::FailOnWriteResponse => Err(io::Error::other("FailOnWriteResponse")),
505-
Action::TimeoutOnWriteResponse => loop {
506-
sleep(Duration::MAX).await;
507-
},
505+
Action::TimeoutOnWriteResponse => {
506+
pending::<()>().await;
507+
Err(io::Error::other("FailOnWriteResponse"))
508+
}
508509
action => {
509510
let bytes = [action.into()];
510511
io.write_all(&bytes).await?;

protocols/request-response/tests/peer_address.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use libp2p_swarm_test::SwarmExt;
88
use serde::{Deserialize, Serialize};
99
use tracing_subscriber::EnvFilter;
1010

11-
#[async_std::test]
11+
#[tokio::test]
1212
#[cfg(feature = "cbor")]
1313
async fn dial_succeeds_after_adding_peers_address() {
1414
let _ = tracing_subscriber::fmt()
@@ -34,7 +34,7 @@ async fn dial_succeeds_after_adding_peers_address() {
3434

3535
swarm.dial(peer_id2).unwrap();
3636

37-
async_std::task::spawn(swarm2.loop_on_next());
37+
tokio::spawn(swarm2.loop_on_next());
3838

3939
let (connected_peer_id, connected_address) = swarm
4040
.wait(|event| match event {

protocols/request-response/tests/ping.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rand::Rng;
3232
use serde::{Deserialize, Serialize};
3333
use tracing_subscriber::EnvFilter;
3434

35-
#[async_std::test]
35+
#[tokio::test]
3636
#[cfg(feature = "cbor")]
3737
async fn is_response_outbound() {
3838
let _ = tracing_subscriber::fmt()
@@ -81,7 +81,7 @@ async fn is_response_outbound() {
8181
}
8282

8383
/// Exercises a simple ping protocol.
84-
#[async_std::test]
84+
#[tokio::test]
8585
#[cfg(feature = "cbor")]
8686
async fn ping_protocol() {
8787
let ping = Ping("ping".to_string().into_bytes());
@@ -173,12 +173,12 @@ async fn ping_protocol() {
173173
}
174174
};
175175

176-
async_std::task::spawn(Box::pin(peer1));
176+
tokio::spawn(Box::pin(peer1));
177177
peer2.await;
178178
}
179179

180180
/// Exercises a simple ping protocol where peers are not connected prior to request sending.
181-
#[async_std::test]
181+
#[tokio::test]
182182
#[cfg(feature = "cbor")]
183183
async fn ping_protocol_explicit_address() {
184184
let ping = Ping("ping".to_string().into_bytes());
@@ -293,11 +293,11 @@ async fn ping_protocol_explicit_address() {
293293
}
294294
};
295295

296-
async_std::task::spawn(Box::pin(peer1));
296+
tokio::spawn(Box::pin(peer1));
297297
peer2.await;
298298
}
299299

300-
#[async_std::test]
300+
#[tokio::test]
301301
#[cfg(feature = "cbor")]
302302
async fn emits_inbound_connection_closed_failure() {
303303
let ping = Ping("ping".to_string().into_bytes());
@@ -363,7 +363,7 @@ async fn emits_inbound_connection_closed_failure() {
363363
/// early close as a protocol violation which results in the connection being closed.
364364
/// If the substream were not properly closed when dropped, the sender would instead
365365
/// run into a timeout waiting for the response.
366-
#[async_std::test]
366+
#[tokio::test]
367367
#[cfg(feature = "cbor")]
368368
async fn emits_inbound_connection_closed_if_channel_is_dropped() {
369369
let ping = Ping("ping".to_string().into_bytes());
@@ -421,7 +421,7 @@ async fn emits_inbound_connection_closed_if_channel_is_dropped() {
421421
}
422422

423423
/// Send multiple requests concurrently.
424-
#[async_std::test]
424+
#[tokio::test]
425425
#[cfg(feature = "cbor")]
426426
async fn concurrent_ping_protocol() {
427427
use std::{collections::HashMap, num::NonZero};
@@ -537,7 +537,7 @@ async fn concurrent_ping_protocol() {
537537
assert_eq!(count, num_pings);
538538
};
539539

540-
async_std::task::spawn(Box::pin(peer1));
540+
tokio::spawn(Box::pin(peer1));
541541
peer2.await;
542542
}
543543

0 commit comments

Comments
 (0)