Skip to content

Commit 8199dac

Browse files
authored
chore(libp2p-websocket): use tokio instead of async-std
ref libp2p#4449 Pull-Request: libp2p#6028.
1 parent b5928ba commit 8199dac

File tree

4 files changed

+63
-70
lines changed

4 files changed

+63
-70
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.

transports/websocket/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ webpki-roots = "0.26"
2727

2828
[dev-dependencies]
2929
libp2p-tcp = { workspace = true, features = ["async-io"] }
30-
libp2p-dns = { workspace = true, features = ["async-std"] }
30+
libp2p-dns = { workspace = true, features = ["tokio"] }
3131
libp2p-identity = { workspace = true, features = ["rand"] }
32-
async-std = { version = "1.6.5", features = ["attributes"] }
32+
tokio = { workspace = true, features = ["rt", "macros", "io-std", "io-util"] }
3333
rcgen = { workspace = true }
3434

3535
# Passing arguments to the docsrs builder in order to properly document cfg's.

transports/websocket/src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,11 @@ use rw_stream_sink::RwStreamSink;
7272
/// # use libp2p_websocket as websocket;
7373
/// # use std::pin::Pin;
7474
/// #
75-
/// # #[async_std::main]
75+
/// # #[tokio::main]
7676
/// # async fn main() {
7777
///
7878
/// let mut transport = websocket::Config::new(
79-
/// dns::async_std::Transport::system(tcp::async_io::Transport::new(tcp::Config::default()))
80-
/// .await
79+
/// dns::tokio::Transport::system(tcp::async_io::Transport::new(tcp::Config::default()))
8180
/// .unwrap(),
8281
/// );
8382
///
@@ -115,7 +114,7 @@ use rw_stream_sink::RwStreamSink;
115114
/// # use libp2p_websocket as websocket;
116115
/// # use std::pin::Pin;
117116
/// #
118-
/// # #[async_std::main]
117+
/// # #[tokio::main]
119118
/// # async fn main() {
120119
///
121120
/// let mut transport =

transports/websocket/src/quicksink.rs

Lines changed: 57 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -293,87 +293,81 @@ where
293293

294294
#[cfg(test)]
295295
mod tests {
296-
use async_std::{io, task};
297296
use futures::{channel::mpsc, prelude::*};
297+
use tokio::io::{self, AsyncWriteExt};
298298

299299
use crate::quicksink::{make_sink, Action};
300300

301-
#[test]
302-
fn smoke_test() {
303-
task::block_on(async {
304-
let sink = make_sink(io::stdout(), |mut stdout, action| async move {
305-
match action {
306-
Action::Send(x) => stdout.write_all(x).await?,
307-
Action::Flush => stdout.flush().await?,
308-
Action::Close => stdout.close().await?,
309-
}
310-
Ok::<_, io::Error>(stdout)
311-
});
301+
#[tokio::test]
302+
async fn smoke_test() {
303+
let sink = make_sink(io::stdout(), |mut stdout, action| async move {
304+
match action {
305+
Action::Send(x) => stdout.write_all(x).await?,
306+
Action::Flush => stdout.flush().await?,
307+
Action::Close => stdout.shutdown().await?,
308+
}
309+
Ok::<_, io::Error>(stdout)
310+
});
312311

313-
let values = vec![Ok(&b"hello\n"[..]), Ok(&b"world\n"[..])];
314-
assert!(stream::iter(values).forward(sink).await.is_ok())
315-
})
312+
let values = vec![Ok(&b"hello\n"[..]), Ok(&b"world\n"[..])];
313+
assert!(stream::iter(values).forward(sink).await.is_ok())
316314
}
317315

318-
#[test]
319-
fn replay() {
320-
task::block_on(async {
321-
let (tx, rx) = mpsc::channel(5);
316+
#[tokio::test]
317+
async fn replay() {
318+
let (tx, rx) = mpsc::channel(5);
322319

323-
let sink = make_sink(tx, |mut tx, action| async move {
324-
tx.send(action.clone()).await?;
325-
if action == Action::Close {
326-
tx.close().await?
327-
}
328-
Ok::<_, mpsc::SendError>(tx)
329-
});
320+
let sink = make_sink(tx, |mut tx, action| async move {
321+
tx.send(action.clone()).await?;
322+
if action == Action::Close {
323+
tx.close().await?
324+
}
325+
Ok::<_, mpsc::SendError>(tx)
326+
});
330327

331-
futures::pin_mut!(sink);
328+
futures::pin_mut!(sink);
332329

333-
let expected = [
334-
Action::Send("hello\n"),
335-
Action::Flush,
336-
Action::Send("world\n"),
337-
Action::Flush,
338-
Action::Close,
339-
];
330+
let expected = [
331+
Action::Send("hello\n"),
332+
Action::Flush,
333+
Action::Send("world\n"),
334+
Action::Flush,
335+
Action::Close,
336+
];
340337

341-
for &item in &["hello\n", "world\n"] {
342-
sink.send(item).await.unwrap()
343-
}
338+
for &item in &["hello\n", "world\n"] {
339+
sink.send(item).await.unwrap()
340+
}
344341

345-
sink.close().await.unwrap();
342+
sink.close().await.unwrap();
346343

347-
let actual = rx.collect::<Vec<_>>().await;
344+
let actual = rx.collect::<Vec<_>>().await;
348345

349-
assert_eq!(&expected[..], &actual[..])
350-
});
346+
assert_eq!(&expected[..], &actual[..])
351347
}
352348

353-
#[test]
354-
fn error_does_not_panic() {
355-
task::block_on(async {
356-
let sink = make_sink(io::stdout(), |mut _stdout, _action| async move {
357-
Err(io::Error::other("oh no"))
358-
});
349+
#[tokio::test]
350+
async fn error_does_not_panic() {
351+
let sink = make_sink(io::stdout(), |mut _stdout, _action| async move {
352+
Err(io::Error::other("oh no"))
353+
});
359354

360-
futures::pin_mut!(sink);
355+
futures::pin_mut!(sink);
361356

362-
let result = sink.send("hello").await;
363-
match result {
364-
Err(crate::quicksink::Error::Send(e)) => {
365-
assert_eq!(e.kind(), io::ErrorKind::Other);
366-
assert_eq!(e.to_string(), "oh no")
367-
}
368-
_ => panic!("unexpected result: {result:?}"),
369-
};
357+
let result = sink.send("hello").await;
358+
match result {
359+
Err(crate::quicksink::Error::Send(e)) => {
360+
assert_eq!(e.kind(), io::ErrorKind::Other);
361+
assert_eq!(e.to_string(), "oh no")
362+
}
363+
_ => panic!("unexpected result: {result:?}"),
364+
};
370365

371-
// Call send again, expect not to panic.
372-
let result = sink.send("hello").await;
373-
match result {
374-
Err(crate::quicksink::Error::Closed) => {}
375-
_ => panic!("unexpected result: {result:?}"),
376-
};
377-
})
366+
// Call send again, expect not to panic.
367+
let result = sink.send("hello").await;
368+
match result {
369+
Err(crate::quicksink::Error::Closed) => {}
370+
_ => panic!("unexpected result: {result:?}"),
371+
};
378372
}
379373
}

0 commit comments

Comments
 (0)