Skip to content

Commit

Permalink
fix spurious failure in rt_common coop test
Browse files Browse the repository at this point in the history
Tests relied on time to ensure readiness, replaced with channels.

Fixes: tokio-rs#4432
  • Loading branch information
gwik committed Feb 11, 2022
1 parent e7a0da6 commit 9a59935
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions tokio/tests/rt_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -989,20 +989,30 @@ 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;

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::<Vec<_>>();

// 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
Expand All @@ -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::<Vec<_>>();

// 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
Expand Down

0 comments on commit 9a59935

Please sign in to comment.