Skip to content
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

chore: use tokio instead of async-std #5828

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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tracing = { workspace = true }
unsigned-varint = { workspace = true }

[dev-dependencies]
async-std = { version = "1.6.2", features = ["attributes"] }
tokio = { workspace = true, features = ["rt-multi-thread"] }
libp2p-mplex = { path = "../muxers/mplex" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing.
libp2p-noise = { path = "../transports/noise" } # Using `path` here because this is a cyclic dev-dependency which otherwise breaks releasing.
multihash = { workspace = true, features = ["arb"] }
Expand Down
6 changes: 4 additions & 2 deletions core/tests/transport_upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use libp2p_mplex::MplexConfig;
use libp2p_noise as noise;
use multiaddr::{Multiaddr, Protocol};
use rand::random;
use tokio::runtime::Runtime;

#[derive(Clone)]
struct HelloUpgrade {}
Expand Down Expand Up @@ -137,6 +138,7 @@ fn upgrade_pipeline() {
assert_eq!(peer, listener_id);
};

async_std::task::spawn(server);
async_std::task::block_on(client);
let rt = Runtime::new().unwrap();
rt.spawn(server);
rt.block_on(client);
Comment on lines +141 to +143
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could use the macros feature to add tokio::test to the function and spawn the task for the server future while awaiting on the client without needing to call block_on.

Dont believe there would be any impact but we could probably poll both futures concurrently in the test to drive them to completion, or at least until the client future is completed, no? Whats your thoughts on that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I tried without block_on and polling both the futures together using the join! but it gets stuck in a deadlock. I don't know how we can do this any other way. Let me know if you have something in mind.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Join! macro would await until both futures are completed and since the server future will continue to await on events, it will never return, so instead we would probably want to use select future or the select! macro to poll both of them concurrently and after the client future is complete we could then end the test. Additionally, if we client.await with rt.spawn(server) (or tokio::spawn if tokio::test is used) should only await until it (client) is completed and would then end the test.

We could always just hold off on concurrently polling them both and stick with replacing the Runtime with direct functions from tokio and see about polling the client task.

}
2 changes: 1 addition & 1 deletion misc/allow-block-list/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true, features = ["peerid"] }

[dev-dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
tokio = { workspace = true, features = ["rt", "macros"] }
libp2p-swarm-derive = { path = "../../swarm-derive" }
libp2p-swarm-test = { path = "../../swarm-test" }

Expand Down
18 changes: 9 additions & 9 deletions misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ mod tests {

use super::*;

#[async_std::test]
#[tokio::test]
async fn cannot_dial_blocked_peer() {
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
Expand All @@ -319,7 +319,7 @@ mod tests {
assert!(cause.downcast::<Blocked>().is_ok());
}

#[async_std::test]
#[tokio::test]
async fn can_dial_unblocked_peer() {
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
Expand All @@ -333,15 +333,15 @@ mod tests {
dial(&mut dialer, &listener).unwrap();
}

#[async_std::test]
#[tokio::test]
async fn blocked_peer_cannot_dial_us() {
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
listener.listen().with_memory_addr_external().await;

listener.behaviour_mut().block_peer(*dialer.local_peer_id());
dial(&mut dialer, &listener).unwrap();
async_std::task::spawn(dialer.loop_on_next());
tokio::spawn(dialer.loop_on_next());

let cause = listener
.wait(|e| match e {
Expand All @@ -355,7 +355,7 @@ mod tests {
assert!(cause.downcast::<Blocked>().is_ok());
}

#[async_std::test]
#[tokio::test]
async fn connections_get_closed_upon_blocked() {
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<BlockedPeers>::default());
Expand All @@ -381,7 +381,7 @@ mod tests {
assert_eq!(closed_listener_peer, *dialer.local_peer_id());
}

#[async_std::test]
#[tokio::test]
async fn cannot_dial_peer_unless_allowed() {
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
Expand All @@ -396,7 +396,7 @@ mod tests {
assert!(dial(&mut dialer, &listener).is_ok());
}

#[async_std::test]
#[tokio::test]
async fn cannot_dial_disallowed_peer() {
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
Expand All @@ -413,7 +413,7 @@ mod tests {
assert!(cause.downcast::<NotAllowed>().is_ok());
}

#[async_std::test]
#[tokio::test]
async fn not_allowed_peer_cannot_dial_us() {
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
Expand Down Expand Up @@ -450,7 +450,7 @@ mod tests {
assert!(incoming_cause.downcast::<NotAllowed>().is_ok());
}

#[async_std::test]
#[tokio::test]
async fn connections_get_closed_upon_disallow() {
let mut dialer = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
let mut listener = Swarm::new_ephemeral(|_| Behaviour::<AllowedPeers>::default());
Expand Down
2 changes: 1 addition & 1 deletion misc/connection-limits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ libp2p-swarm = { workspace = true }
libp2p-identity = { workspace = true, features = ["peerid"] }

[dev-dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
tokio = { workspace = true, features = ["rt-multi-thread"] }
libp2p-identify = { workspace = true }
libp2p-ping = { workspace = true }
libp2p-swarm-derive = { path = "../../swarm-derive" }
Expand Down
11 changes: 7 additions & 4 deletions misc/connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ mod tests {
};
use libp2p_swarm_test::SwarmExt;
use quickcheck::*;
use tokio::runtime::Runtime;

use super::*;

Expand Down Expand Up @@ -452,7 +453,8 @@ mod tests {
)
});

async_std::task::block_on(async {
let rt = Runtime::new().unwrap();
rt.block_on(async {
let (listen_addr, _) = swarm1.listen().with_memory_addr_external().await;

for _ in 0..limit {
Expand All @@ -461,7 +463,7 @@ mod tests {

swarm2.dial(listen_addr).unwrap();

async_std::task::spawn(swarm2.loop_on_next());
tokio::spawn(swarm2.loop_on_next());

let cause = swarm1
.wait(|event| match event {
Expand Down Expand Up @@ -503,11 +505,12 @@ mod tests {
});
let mut swarm2 = Swarm::new_ephemeral(|_| Behaviour::new(ConnectionLimits::default()));

async_std::task::block_on(async {
let rt = Runtime::new().unwrap();
rt.block_on(async {
// Have swarm2 dial swarm1.
let (listen_addr, _) = swarm1.listen().await;
swarm2.dial(listen_addr).unwrap();
async_std::task::spawn(swarm2.loop_on_next());
tokio::spawn(swarm2.loop_on_next());

// Wait for the ConnectionDenier of swarm1 to deny the established connection.
let cause = swarm1
Expand Down
Loading