mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-27 00:00:12 +02:00
coop: fix flaky coop test (#5074)
This commit is contained in:
+18
-18
@@ -1044,22 +1044,22 @@ rt_test! {
|
|||||||
#[test]
|
#[test]
|
||||||
fn coop() {
|
fn coop() {
|
||||||
use std::task::Poll::Ready;
|
use std::task::Poll::Ready;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
let rt = rt();
|
let rt = rt();
|
||||||
|
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
// Create a bunch of tasks
|
let (send, mut recv) = mpsc::unbounded_channel();
|
||||||
let mut tasks = (0..1_000).map(|_| {
|
|
||||||
tokio::spawn(async { })
|
|
||||||
}).collect::<Vec<_>>();
|
|
||||||
|
|
||||||
// Hope that all the tasks complete...
|
// Send a bunch of messages.
|
||||||
time::sleep(Duration::from_millis(100)).await;
|
for _ in 0..1_000 {
|
||||||
|
send.send(()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
poll_fn(|cx| {
|
poll_fn(|cx| {
|
||||||
// At least one task should not be ready
|
// At least one response should return pending.
|
||||||
for task in &mut tasks {
|
for _ in 0..1_000 {
|
||||||
if Pin::new(task).poll(cx).is_pending() {
|
if recv.poll_recv(cx).is_pending() {
|
||||||
return Ready(());
|
return Ready(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1072,22 +1072,22 @@ rt_test! {
|
|||||||
#[test]
|
#[test]
|
||||||
fn coop_unconstrained() {
|
fn coop_unconstrained() {
|
||||||
use std::task::Poll::Ready;
|
use std::task::Poll::Ready;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
let rt = rt();
|
let rt = rt();
|
||||||
|
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
// Create a bunch of tasks
|
let (send, mut recv) = mpsc::unbounded_channel();
|
||||||
let mut tasks = (0..1_000).map(|_| {
|
|
||||||
tokio::spawn(async { })
|
|
||||||
}).collect::<Vec<_>>();
|
|
||||||
|
|
||||||
// Hope that all the tasks complete...
|
// Send a bunch of messages.
|
||||||
time::sleep(Duration::from_millis(100)).await;
|
for _ in 0..1_000 {
|
||||||
|
send.send(()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
tokio::task::unconstrained(poll_fn(|cx| {
|
tokio::task::unconstrained(poll_fn(|cx| {
|
||||||
// All the tasks should be ready
|
// All the responses should be ready.
|
||||||
for task in &mut tasks {
|
for _ in 0..1_000 {
|
||||||
assert!(Pin::new(task).poll(cx).is_ready());
|
assert_eq!(recv.poll_recv(cx), Poll::Ready(Some(())));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ready(())
|
Ready(())
|
||||||
|
|||||||
Reference in New Issue
Block a user