Skip to content

Commit ef24b6f

Browse files
authored
chore(kad): deprecate async-std for tokio on the tests
Pull-Request: #6072.
1 parent e4cbc1a commit ef24b6f

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

protocols/kad/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }
3737
futures-timer = "3.0"
3838
libp2p-identify = { path = "../identify" }
3939
libp2p-noise = { workspace = true }
40-
libp2p-swarm = { path = "../../swarm", features = ["macros", "async-std"] }
40+
libp2p-swarm = { path = "../../swarm", features = ["macros", "tokio"] }
4141
libp2p-swarm-test = { path = "../../swarm-test" }
4242
libp2p-yamux = { workspace = true }
4343
quickcheck = { workspace = true }

protocols/kad/src/behaviour/test.rs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
#![cfg(test)]
2222

23-
use futures::{executor::block_on, future::poll_fn, prelude::*};
23+
use futures::{future::poll_fn, prelude::*};
2424
use futures_timer::Delay;
2525
use libp2p_core::{
2626
multiaddr::{multiaddr, Protocol},
@@ -34,6 +34,7 @@ use libp2p_swarm::{self as swarm, Swarm, SwarmEvent};
3434
use libp2p_yamux as yamux;
3535
use quickcheck::*;
3636
use rand::{random, rngs::StdRng, thread_rng, Rng, SeedableRng};
37+
use tokio::runtime::Runtime;
3738

3839
use super::*;
3940
use crate::{
@@ -64,7 +65,7 @@ fn build_node_with_config(cfg: Config) -> (Multiaddr, TestSwarm) {
6465
transport,
6566
behaviour,
6667
local_id,
67-
swarm::Config::with_async_std_executor(),
68+
swarm::Config::with_tokio_executor(),
6869
);
6970

7071
let address: Multiaddr = Protocol::Memory(random::<u64>()).into();
@@ -187,7 +188,8 @@ fn bootstrap() {
187188
let mut first = true;
188189

189190
// Run test
190-
block_on(poll_fn(move |ctx| {
191+
let rt = Runtime::new().unwrap();
192+
rt.block_on(poll_fn(move |ctx| {
191193
for (i, swarm) in swarms.iter_mut().enumerate() {
192194
loop {
193195
match swarm.poll_next_unpin(ctx) {
@@ -282,7 +284,8 @@ fn query_iter() {
282284
expected_distances.sort();
283285

284286
// Run test
285-
block_on(poll_fn(move |ctx| {
287+
let rt = Runtime::new().unwrap();
288+
rt.block_on(poll_fn(move |ctx| {
286289
for (i, swarm) in swarms.iter_mut().enumerate() {
287290
loop {
288291
match swarm.poll_next_unpin(ctx) {
@@ -345,7 +348,8 @@ fn unresponsive_not_returned_direct() {
345348
let search_target = PeerId::random();
346349
swarms[0].behaviour_mut().get_closest_peers(search_target);
347350

348-
block_on(poll_fn(move |ctx| {
351+
let rt = Runtime::new().unwrap();
352+
rt.block_on(poll_fn(move |ctx| {
349353
for swarm in &mut swarms {
350354
loop {
351355
match swarm.poll_next_unpin(ctx) {
@@ -403,7 +407,8 @@ fn unresponsive_not_returned_indirect() {
403407
let search_target = PeerId::random();
404408
swarms[1].behaviour_mut().get_closest_peers(search_target);
405409

406-
block_on(poll_fn(move |ctx| {
410+
let rt = Runtime::new().unwrap();
411+
rt.block_on(poll_fn(move |ctx| {
407412
for swarm in &mut swarms {
408413
loop {
409414
match swarm.poll_next_unpin(ctx) {
@@ -461,7 +466,8 @@ fn get_closest_with_different_num_results_inner(num_results: usize, replication_
461466
.behaviour_mut()
462467
.get_n_closest_peers(search_target, num_results_nonzero);
463468

464-
block_on(poll_fn(move |ctx| {
469+
let rt = Runtime::new().unwrap();
470+
rt.block_on(poll_fn(move |ctx| {
465471
for swarm in &mut swarms {
466472
loop {
467473
match swarm.poll_next_unpin(ctx) {
@@ -518,7 +524,8 @@ fn get_record_not_found() {
518524
let target_key = record::Key::from(random_multihash());
519525
let qid = swarms[0].behaviour_mut().get_record(target_key.clone());
520526

521-
block_on(poll_fn(move |ctx| {
527+
let rt = Runtime::new().unwrap();
528+
rt.block_on(poll_fn(move |ctx| {
522529
for swarm in &mut swarms {
523530
loop {
524531
match swarm.poll_next_unpin(ctx) {
@@ -639,7 +646,8 @@ fn put_record() {
639646
// The accumulated results for one round of publishing.
640647
let mut results = Vec::new();
641648

642-
block_on(poll_fn(move |ctx| loop {
649+
let rt = Runtime::new().unwrap();
650+
rt.block_on(poll_fn(move |ctx| loop {
643651
// Poll all swarms until they are "Pending".
644652
for swarm in &mut swarms {
645653
loop {
@@ -830,7 +838,8 @@ fn get_record() {
830838
swarms[2].behaviour_mut().store.put(record.clone()).unwrap();
831839
let qid = swarms[0].behaviour_mut().get_record(record.key.clone());
832840

833-
block_on(poll_fn(move |ctx| {
841+
let rt = Runtime::new().unwrap();
842+
rt.block_on(poll_fn(move |ctx| {
834843
for swarm in &mut swarms {
835844
loop {
836845
match swarm.poll_next_unpin(ctx) {
@@ -887,7 +896,8 @@ fn get_record_many() {
887896
let quorum = Quorum::N(NonZeroUsize::new(num_results).unwrap());
888897
let qid = swarms[0].behaviour_mut().get_record(record.key.clone());
889898

890-
block_on(poll_fn(move |ctx| {
899+
let rt = Runtime::new().unwrap();
900+
rt.block_on(poll_fn(move |ctx| {
891901
for (i, swarm) in swarms.iter_mut().enumerate() {
892902
let mut records = Vec::new();
893903
let quorum = quorum.eval(swarm.behaviour().queries.config().replication_factor);
@@ -987,7 +997,8 @@ fn add_provider() {
987997
qids.insert(qid);
988998
}
989999

990-
block_on(poll_fn(move |ctx| loop {
1000+
let rt = Runtime::new().unwrap();
1001+
rt.block_on(poll_fn(move |ctx| loop {
9911002
// Poll all swarms until they are "Pending".
9921003
for swarm in &mut swarms {
9931004
loop {
@@ -1125,7 +1136,8 @@ fn exceed_jobs_max_queries() {
11251136

11261137
assert_eq!(swarm.behaviour_mut().queries.size(), num);
11271138

1128-
block_on(poll_fn(move |ctx| {
1139+
let rt = Runtime::new().unwrap();
1140+
rt.block_on(poll_fn(move |ctx| {
11291141
for _ in 0..num {
11301142
// There are no other nodes, so the queries finish instantly.
11311143
loop {
@@ -1210,7 +1222,8 @@ fn disjoint_query_does_not_finish_before_all_paths_did() {
12101222
// Poll only `alice` and `trudy` expecting `alice` not yet to return a query
12111223
// result as it is not able to connect to `bob` just yet.
12121224
let addr_trudy = *Swarm::local_peer_id(&trudy);
1213-
block_on(poll_fn(|ctx| {
1225+
let rt = Runtime::new().unwrap();
1226+
rt.block_on(poll_fn(|ctx| {
12141227
for (i, swarm) in [&mut alice, &mut trudy].iter_mut().enumerate() {
12151228
loop {
12161229
match swarm.poll_next_unpin(ctx) {
@@ -1264,7 +1277,7 @@ fn disjoint_query_does_not_finish_before_all_paths_did() {
12641277

12651278
// Poll `alice` and `bob` expecting `alice` to return a successful query
12661279
// result as it is now able to explore the second disjoint path.
1267-
let records = block_on(poll_fn(|ctx| {
1280+
let records = rt.block_on(poll_fn(|ctx| {
12681281
let mut records = Vec::new();
12691282
for (i, swarm) in [&mut alice, &mut bob].iter_mut().enumerate() {
12701283
loop {
@@ -1337,7 +1350,8 @@ fn manual_bucket_inserts() {
13371350
.1
13381351
.behaviour_mut()
13391352
.get_closest_peers(PeerId::random());
1340-
block_on(poll_fn(move |ctx| {
1353+
let rt = Runtime::new().unwrap();
1354+
rt.block_on(poll_fn(move |ctx| {
13411355
for (_, swarm) in swarms.iter_mut() {
13421356
loop {
13431357
match swarm.poll_next_unpin(ctx) {
@@ -1460,7 +1474,8 @@ fn get_providers_single() {
14601474
.start_providing(key.clone())
14611475
.expect("could not provide");
14621476

1463-
block_on(async {
1477+
let rt = Runtime::new().unwrap();
1478+
rt.block_on(async {
14641479
match single_swarm.next().await.unwrap() {
14651480
SwarmEvent::Behaviour(Event::OutboundQueryProgressed {
14661481
result: QueryResult::StartProviding(Ok(_)),
@@ -1474,7 +1489,7 @@ fn get_providers_single() {
14741489

14751490
let query_id = single_swarm.behaviour_mut().get_providers(key);
14761491

1477-
block_on(async {
1492+
rt.block_on(async {
14781493
loop {
14791494
match single_swarm.next().await.unwrap() {
14801495
SwarmEvent::Behaviour(Event::OutboundQueryProgressed {
@@ -1538,7 +1553,8 @@ fn get_providers_limit<const N: usize>() {
15381553

15391554
let mut all_providers: Vec<PeerId> = vec![];
15401555

1541-
block_on(poll_fn(move |ctx| {
1556+
let rt = Runtime::new().unwrap();
1557+
rt.block_on(poll_fn(move |ctx| {
15421558
for (i, swarm) in swarms.iter_mut().enumerate() {
15431559
loop {
15441560
match swarm.poll_next_unpin(ctx) {
@@ -1630,7 +1646,8 @@ fn get_closest_peers_should_return_up_to_k_peers() {
16301646
let search_target = PeerId::random();
16311647
swarms[0].behaviour_mut().get_closest_peers(search_target);
16321648

1633-
block_on(poll_fn(move |ctx| {
1649+
let rt = Runtime::new().unwrap();
1650+
rt.block_on(poll_fn(move |ctx| {
16341651
for swarm in &mut swarms {
16351652
loop {
16361653
match swarm.poll_next_unpin(ctx) {

0 commit comments

Comments
 (0)