Skip to content

Commit

Permalink
coop: fix flaky coop test (#5074)
Browse files Browse the repository at this point in the history
  • Loading branch information
Darksonn committed Oct 5, 2022
1 parent 5dbd8ca commit fdc28bc
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions tokio/tests/rt_common.rs
Expand Up @@ -1044,22 +1044,22 @@ rt_test! {
#[test]
fn coop() {
use std::task::Poll::Ready;
use tokio::sync::mpsc;

let rt = rt();

rt.block_on(async {
// Create a bunch of tasks
let mut tasks = (0..1_000).map(|_| {
tokio::spawn(async { })
}).collect::<Vec<_>>();
let (send, mut recv) = mpsc::unbounded_channel();

// Hope that all the tasks complete...
time::sleep(Duration::from_millis(100)).await;
// Send a bunch of messages.
for _ in 0..1_000 {
send.send(()).unwrap();
}

poll_fn(|cx| {
// At least one task should not be ready
for task in &mut tasks {
if Pin::new(task).poll(cx).is_pending() {
// At least one response should return pending.
for _ in 0..1_000 {
if recv.poll_recv(cx).is_pending() {
return Ready(());
}
}
Expand All @@ -1072,22 +1072,22 @@ rt_test! {
#[test]
fn coop_unconstrained() {
use std::task::Poll::Ready;
use tokio::sync::mpsc;

let rt = rt();

rt.block_on(async {
// Create a bunch of tasks
let mut tasks = (0..1_000).map(|_| {
tokio::spawn(async { })
}).collect::<Vec<_>>();
let (send, mut recv) = mpsc::unbounded_channel();

// Hope that all the tasks complete...
time::sleep(Duration::from_millis(100)).await;
// Send a bunch of messages.
for _ in 0..1_000 {
send.send(()).unwrap();
}

tokio::task::unconstrained(poll_fn(|cx| {
// All the tasks should be ready
for task in &mut tasks {
assert!(Pin::new(task).poll(cx).is_ready());
// All the responses should be ready.
for _ in 0..1_000 {
assert_eq!(recv.poll_recv(cx), Poll::Ready(Some(())));
}

Ready(())
Expand Down

0 comments on commit fdc28bc

Please sign in to comment.