From 9a599357be01754f9b54abc63e353e6542cee568 Mon Sep 17 00:00:00 2001 From: Antonin Amand Date: Fri, 11 Feb 2022 01:14:32 +0100 Subject: [PATCH] fix spurious failure in rt_common coop test Tests relied on time to ensure readiness, replaced with channels. Fixes: #4432 --- tokio/tests/rt_common.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/tokio/tests/rt_common.rs b/tokio/tests/rt_common.rs index cb1d0f66152..dff94103e68 100644 --- a/tokio/tests/rt_common.rs +++ b/tokio/tests/rt_common.rs @@ -989,6 +989,10 @@ rt_test! { tx.send(()).unwrap(); } + + // Should be above initial coop budget, currently 128. + const COOP_NUM_TASKS: usize = 1000; + #[test] fn coop() { use std::task::Poll::Ready; @@ -996,13 +1000,19 @@ rt_test! { let rt = rt(); rt.block_on(async { + let (tx, mut rx) = tokio::sync::mpsc::channel(COOP_NUM_TASKS); + // Create a bunch of tasks - let mut tasks = (0..1_000).map(|_| { - tokio::spawn(async { }) + let mut tasks = (0..COOP_NUM_TASKS).map(|_| { + let tx = tx.clone(); + tokio::spawn(async move{ + tx.send(()).await.expect("send"); + }) }).collect::>(); - // Hope that all the tasks complete... - time::sleep(Duration::from_millis(100)).await; + for _ in 0..COOP_NUM_TASKS { + rx.recv().await; + } poll_fn(|cx| { // At least one task should not be ready @@ -1024,13 +1034,19 @@ rt_test! { let rt = rt(); rt.block_on(async { + let (tx, mut rx) = tokio::sync::mpsc::channel(COOP_NUM_TASKS); + // Create a bunch of tasks - let mut tasks = (0..1_000).map(|_| { - tokio::spawn(async { }) + let mut tasks = (0..COOP_NUM_TASKS).map(|_| { + let tx = tx.clone(); + tokio::spawn(async move{ + tx.send(()).await.expect("send"); + }) }).collect::>(); - // Hope that all the tasks complete... - time::sleep(Duration::from_millis(100)).await; + for _ in 0..COOP_NUM_TASKS { + rx.recv().await; + } tokio::task::unconstrained(poll_fn(|cx| { // All the tasks should be ready