stream: iter() should yield every so often. (#2343)

This commit is contained in:
Carl Lerche 2020-03-25 14:56:24 -07:00 committed by GitHub
parent 57ba37c978
commit 186196b911
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -44,7 +44,8 @@ where
{
type Item = I::Item;
fn poll_next(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<I::Item>> {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<I::Item>> {
ready!(crate::coop::poll_proceed(cx));
Poll::Ready(self.iter.next())
}

View File

@ -0,0 +1,18 @@
use tokio::stream;
use tokio_test::task;
use std::iter;
#[tokio::test]
async fn coop() {
let mut stream = task::spawn(stream::iter(iter::repeat(1)));
for _ in 0..10_000 {
if stream.poll_next().is_pending() {
assert!(stream.is_woken());
return;
}
}
panic!("did not yield");
}