Skip to content

chore(libp2p-uds): use tokio instead of async_std in tests #6031

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions transports/uds/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ tracing = { workspace = true }

[dev-dependencies]
tempfile = "3.10"
tokio = { workspace = true, features = ["rt", "macros", "io-util"] }

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
25 changes: 14 additions & 11 deletions transports/uds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ fn multiaddr_to_path(addr: &Multiaddr) -> Result<PathBuf, ()> {
}
}

#[cfg(all(test, feature = "async-std"))]
#[cfg(all(test, feature = "tokio"))]
mod tests {
use std::{borrow::Cow, path::Path};

Expand All @@ -252,8 +252,9 @@ mod tests {
transport::{DialOpts, ListenerId, PortUse},
Endpoint, Transport,
};
use tokio::io::{AsyncReadExt, AsyncWriteExt};

use super::{multiaddr_to_path, UdsConfig};
use super::{multiaddr_to_path, TokioUdsConfig};

#[test]
fn multiaddr_to_path_conversion() {
Expand All @@ -271,8 +272,8 @@ mod tests {
);
}

#[test]
fn communicating_between_dialer_and_listener() {
#[tokio::test]
async fn communicating_between_dialer_and_listener() {
let temp_dir = tempfile::tempdir().unwrap();
let socket = temp_dir.path().join("socket");
let addr = Multiaddr::from(Protocol::Unix(Cow::Owned(
Expand All @@ -281,8 +282,8 @@ mod tests {

let (tx, rx) = oneshot::channel();

async_std::task::spawn(async move {
let mut transport = UdsConfig::new().boxed();
let listener = async move {
let mut transport = TokioUdsConfig::new().boxed();
transport.listen_on(ListenerId::next(), addr).unwrap();

let listen_addr = transport
Expand All @@ -303,10 +304,10 @@ mod tests {
let mut buf = [0u8; 3];
sock.read_exact(&mut buf).await.unwrap();
assert_eq!(buf, [1, 2, 3]);
});
};

async_std::task::block_on(async move {
let mut uds = UdsConfig::new();
let dialer = async move {
let mut uds = TokioUdsConfig::new();
let addr = rx.await.unwrap();
let mut socket = uds
.dial(
Expand All @@ -320,13 +321,15 @@ mod tests {
.await
.unwrap();
let _ = socket.write(&[1, 2, 3]).await.unwrap();
});
};

tokio::join!(listener, dialer);
}

#[test]
#[ignore] // TODO: for the moment unix addresses fail to parse
fn larger_addr_denied() {
let mut uds = UdsConfig::new();
let mut uds = TokioUdsConfig::new();

let addr = "/unix//foo/bar".parse::<Multiaddr>().unwrap();
assert!(uds.listen_on(ListenerId::next(), addr).is_err());
Expand Down
Loading