Skip to content

Commit 4dc9431

Browse files
chore(multistream-select): replace async-std with tokio in tests
Fixes #4449 Pull-Request: #6057.
1 parent a4220eb commit 4dc9431

File tree

5 files changed

+47
-33
lines changed

5 files changed

+47
-33
lines changed

Cargo.lock

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

misc/multistream-select/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ smallvec = "1.13.2"
1919
unsigned-varint = { workspace = true }
2020

2121
[dev-dependencies]
22-
async-std = { version = "1.6.2", features = ["attributes"] }
2322
futures_ringbuf = "0.4.0"
2423
quickcheck = { workspace = true }
2524
rw-stream-sink = { workspace = true }
2625
tracing-subscriber = { workspace = true, features = ["env-filter"] }
26+
tokio = { workspace = true, features = ["full"] }
27+
tokio-util = { version = "0.7", features = ["compat"] }
2728

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

misc/multistream-select/src/dialer_select.rs

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,16 @@ where
208208

209209
#[cfg(test)]
210210
mod tests {
211+
211212
use std::time::Duration;
212213

213-
use async_std::{
214-
future::timeout,
214+
use quickcheck::{Arbitrary, Gen, GenRange};
215+
use tokio::{
215216
net::{TcpListener, TcpStream},
217+
runtime::Runtime,
218+
time::timeout,
216219
};
217-
use quickcheck::{Arbitrary, Gen, GenRange};
220+
use tokio_util::compat::TokioAsyncReadCompatExt;
218221
use tracing::metadata::LevelFilter;
219222
use tracing_subscriber::EnvFilter;
220223

@@ -226,7 +229,7 @@ mod tests {
226229
async fn run(version: Version) {
227230
let (client_connection, server_connection) = futures_ringbuf::Endpoint::pair(100, 100);
228231

229-
let server = async_std::task::spawn(async move {
232+
let server = tokio::task::spawn(async move {
230233
let protos = vec!["/proto1", "/proto2"];
231234
let (proto, mut io) = listener_select_proto(server_connection, protos)
232235
.await
@@ -242,7 +245,7 @@ mod tests {
242245
io.flush().await.unwrap();
243246
});
244247

245-
let client = async_std::task::spawn(async move {
248+
let client = tokio::task::spawn(async move {
246249
let protos = vec!["/proto3", "/proto2"];
247250
let (proto, mut io) = dialer_select_proto(client_connection, protos, version)
248251
.await
@@ -258,12 +261,13 @@ mod tests {
258261
assert_eq!(out, b"pong");
259262
});
260263

261-
server.await;
262-
client.await;
264+
server.await.unwrap();
265+
client.await.unwrap();
263266
}
264267

265-
async_std::task::block_on(run(Version::V1));
266-
async_std::task::block_on(run(Version::V1Lazy));
268+
let rt = Runtime::new().unwrap();
269+
rt.block_on(run(Version::V1));
270+
rt.block_on(run(Version::V1Lazy));
267271
}
268272

269273
/// Tests the expected behaviour of failed negotiations.
@@ -283,12 +287,13 @@ mod tests {
283287
)
284288
.try_init();
285289

286-
async_std::task::block_on(async move {
290+
let rt = Runtime::new().unwrap();
291+
rt.block_on(async move {
287292
let listener = TcpListener::bind("0.0.0.0:0").await.unwrap();
288293
let addr = listener.local_addr().unwrap();
289294

290-
let server = async_std::task::spawn(async move {
291-
let server_connection = listener.accept().await.unwrap().0;
295+
let server = tokio::task::spawn(async move {
296+
let server_connection = listener.accept().await.unwrap().0.compat();
292297

293298
let io = match timeout(
294299
Duration::from_secs(2),
@@ -309,8 +314,8 @@ mod tests {
309314
}
310315
});
311316

312-
let client = async_std::task::spawn(async move {
313-
let client_connection = TcpStream::connect(addr).await.unwrap();
317+
let client = tokio::task::spawn(async move {
318+
let client_connection = TcpStream::connect(addr).await.unwrap().compat();
314319

315320
let mut io = match timeout(
316321
Duration::from_secs(2),
@@ -336,8 +341,8 @@ mod tests {
336341
}
337342
});
338343

339-
server.await;
340-
client.await;
344+
server.await.unwrap();
345+
client.await.unwrap();
341346

342347
tracing::info!("---------------------------------------")
343348
});
@@ -348,12 +353,12 @@ mod tests {
348353
.quickcheck(prop as fn(_, _, _, _));
349354
}
350355

351-
#[async_std::test]
356+
#[tokio::test]
352357
async fn v1_lazy_do_not_wait_for_negotiation_on_poll_close() {
353358
let (client_connection, _server_connection) =
354359
futures_ringbuf::Endpoint::pair(1024 * 1024, 1);
355360

356-
let client = async_std::task::spawn(async move {
361+
let client = tokio::task::spawn(async move {
357362
// Single protocol to allow for lazy (or optimistic) protocol negotiation.
358363
let protos = vec!["/proto1"];
359364
let (proto, mut io) = dialer_select_proto(client_connection, protos, Version::V1Lazy)
@@ -366,9 +371,12 @@ mod tests {
366371
io.close().await.unwrap();
367372
});
368373

369-
async_std::future::timeout(Duration::from_secs(10), client)
370-
.await
371-
.unwrap();
374+
match tokio::time::timeout(Duration::from_secs(10), client).await {
375+
Ok(join_result) => join_result.expect("Client task should complete successfully"),
376+
Err(_elapsed) => {
377+
panic!("Expected the client task to complete before timeout");
378+
}
379+
}
372380
}
373381

374382
#[derive(Clone, Debug)]

misc/multistream-select/src/length_delimited.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,7 @@ mod tests {
388388

389389
use futures::{io::Cursor, prelude::*};
390390
use quickcheck::*;
391+
use tokio::runtime::Runtime;
391392

392393
use crate::length_delimited::LengthDelimited;
393394

@@ -491,9 +492,10 @@ mod tests {
491492
fn prop(frames: Vec<Vec<u8>>) -> TestResult {
492493
let (client_connection, server_connection) = futures_ringbuf::Endpoint::pair(100, 100);
493494

494-
async_std::task::block_on(async move {
495+
let rt = Runtime::new().unwrap();
496+
rt.block_on(async move {
495497
let expected_frames = frames.clone();
496-
let server = async_std::task::spawn(async move {
498+
let server = tokio::task::spawn(async move {
497499
let mut connec =
498500
rw_stream_sink::RwStreamSink::new(LengthDelimited::new(server_connection));
499501

@@ -510,15 +512,15 @@ mod tests {
510512
}
511513
});
512514

513-
let client = async_std::task::spawn(async move {
515+
let client = tokio::task::spawn(async move {
514516
let mut connec = LengthDelimited::new(client_connection);
515517
for frame in frames {
516518
connec.send(From::from(frame)).await.unwrap();
517519
}
518520
});
519521

520-
server.await;
521-
client.await;
522+
server.await.unwrap();
523+
client.await.unwrap();
522524
});
523525

524526
TestResult::passed()

misc/multistream-select/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,19 @@
6969
//! For a dialer:
7070
//!
7171
//! ```no_run
72-
//! use async_std::net::TcpStream;
7372
//! use futures::prelude::*;
7473
//! use multistream_select::{dialer_select_proto, Version};
74+
//! use tokio::{net::TcpStream, runtime::Runtime};
75+
//! use tokio_util::compat::TokioAsyncReadCompatExt;
7576
//!
76-
//! async_std::task::block_on(async move {
77+
//! let rt = Runtime::new().unwrap();
78+
//! rt.block_on(async move {
7779
//! let socket = TcpStream::connect("127.0.0.1:10333").await.unwrap();
80+
//! let compat_socket = socket.compat();
7881
//!
7982
//! let protos = vec!["/echo/1.0.0", "/echo/2.5.0"];
80-
//! let (protocol, _io) = dialer_select_proto(socket, protos, Version::V1)
81-
//! .await
82-
//! .unwrap();
83+
//! let result = dialer_select_proto(compat_socket, protos, Version::V1).await;
84+
//! let (protocol, _io) = result.unwrap();
8385
//!
8486
//! println!("Negotiated protocol: {:?}", protocol);
8587
//! // You can now use `_io` to communicate with the remote.

0 commit comments

Comments
 (0)