Skip to content

Commit

Permalink
chore: refactor dcutr and gossipsub tests to use tokio instead
Browse files Browse the repository at this point in the history
ref #4449

Refactored dcutr and gossipsub tests to use `tokio` instead of `async-std`.

Pull-Request: #5662.
  • Loading branch information
kamuik16 authored Nov 21, 2024
1 parent 059742f commit 13b9ea2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 12 deletions.
4 changes: 2 additions & 2 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 protocols/dcutr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ lru = "0.12.3"
futures-bounded = { workspace = true }

[dev-dependencies]
async-std = { version = "1.12.0", features = ["attributes"] }
clap = { version = "4.5.6", features = ["derive"] }
libp2p-dns = { workspace = true, features = ["async-std"] }
libp2p-identify = { workspace = true }
Expand All @@ -41,6 +40,7 @@ libp2p-tcp = { workspace = true, features = ["async-io"] }
libp2p-yamux = { workspace = true }
rand = "0.8"
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tokio = { workspace = true, features = ["rt", "macros"] }

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
6 changes: 3 additions & 3 deletions protocols/dcutr/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use libp2p_swarm_test::SwarmExt as _;
use std::time::Duration;
use tracing_subscriber::EnvFilter;

#[async_std::test]
#[tokio::test]
async fn connect() {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
Expand All @@ -53,7 +53,7 @@ async fn connect() {
let relay_peer_id = *relay.local_peer_id();
let dst_peer_id = *dst.local_peer_id();

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

let dst_relayed_addr = relay_tcp_addr
.with(Protocol::P2p(relay_peer_id))
Expand All @@ -68,7 +68,7 @@ async fn connect() {
false, // No renewal.
)
.await;
async_std::task::spawn(dst.loop_on_next());
tokio::spawn(dst.loop_on_next());

src.dial_and_wait(dst_relayed_addr.clone()).await;

Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ tracing = { workspace = true }
prometheus-client = { workspace = true }

[dev-dependencies]
async-std = { version = "1.6.3", features = ["unstable"] }
hex = "0.4.2"
libp2p-core = { workspace = true }
libp2p-yamux = { workspace = true }
libp2p-noise = { workspace = true }
libp2p-swarm-test = { path = "../../swarm-test" }
quickcheck = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "time"] }

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
Expand Down
2 changes: 1 addition & 1 deletion protocols/gossipsub/src/behaviour/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
use super::*;
use crate::subscription_filter::WhitelistSubscriptionFilter;
use crate::{config::ConfigBuilder, types::Rpc, IdentTopic as Topic};
use async_std::net::Ipv4Addr;
use byteorder::{BigEndian, ByteOrder};
use libp2p_core::ConnectedPoint;
use rand::Rng;
use std::net::Ipv4Addr;
use std::thread::sleep;

#[derive(Default, Debug)]
Expand Down
11 changes: 7 additions & 4 deletions protocols/gossipsub/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use async_std::prelude::FutureExt;
use futures::stream::{FuturesUnordered, SelectAll};
use futures::StreamExt;
use libp2p_gossipsub as gossipsub;
Expand All @@ -28,7 +27,9 @@ use libp2p_swarm_test::SwarmExt as _;
use quickcheck::{QuickCheck, TestResult};
use rand::{seq::SliceRandom, SeedableRng};
use std::{task::Poll, time::Duration};
use tokio::{runtime::Runtime, time};
use tracing_subscriber::EnvFilter;

struct Graph {
nodes: SelectAll<Swarm<gossipsub::Behaviour>>,
}
Expand Down Expand Up @@ -84,7 +85,7 @@ impl Graph {
}
};

match condition.timeout(Duration::from_secs(10)).await {
match time::timeout(Duration::from_secs(10), condition).await {
Ok(()) => true,
Err(_) => false,
}
Expand All @@ -98,7 +99,7 @@ impl Graph {
Poll::Pending => return Poll::Ready(()),
}
});
fut.timeout(Duration::from_secs(10)).await.unwrap();
time::timeout(Duration::from_secs(10), fut).await.unwrap();
}
}

Expand Down Expand Up @@ -139,7 +140,9 @@ fn multi_hop_propagation() {

tracing::debug!(number_of_nodes=%num_nodes, seed=%seed);

async_std::task::block_on(async move {
let rt = Runtime::new().unwrap();

rt.block_on(async move {
let mut graph = Graph::new_connected(num_nodes as usize, seed).await;
let number_nodes = graph.nodes.len();

Expand Down

0 comments on commit 13b9ea2

Please sign in to comment.