Skip to content

Commit

Permalink
Fix test case since rust-lang/futures-rs#2049
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed Jan 28, 2020
1 parent 64573d1 commit 793c1f4
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions tests/spinning_futures_unordered.rs
Expand Up @@ -3,33 +3,64 @@ use futures::{
stream::{FuturesUnordered, Stream as _},
};
use std::{
cell::Cell,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use unicycle::Unordered;

struct Spinner;
struct Spinner<'a>(&'a Cell<usize>);

impl Future for Spinner {
impl Future for Spinner<'_> {
type Output = ();

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
println!("Hello!");
self.0.set(self.0.get() + 1);

// Note: this will not be needed once we have a futures release with:
// https://github.com/rust-lang/futures-rs/pull/2049
if self.0.get() > 16 {
return Poll::Ready(());
}

cx.waker().wake_by_ref();
Poll::Pending
}
}

#[ignore]
#[tokio::test]
async fn test_spinning_futures_unordered() {
let count = Cell::new(0);

let futures = FuturesUnordered::new();
futures.push(Spinner);
futures.push(Spinner(&count));
pin_utils::pin_mut!(futures);

let _ = poll_fn::<(), _>(move |cx| {
let _ = Pin::new(&mut futures).poll_next(cx);
panic!("We never reach this...");
Poll::Ready(())
})
.await;

// Note: FuturesUnordered will spin a bit before yielding.
assert!(count.get() > 1);
}

#[tokio::test]
async fn test_spinning_unordered() {
let count = Cell::new(0);

let mut futures = Unordered::new();
futures.push(Spinner(&count));
pin_utils::pin_mut!(futures);

let _ = poll_fn::<(), _>(move |cx| {
let _ = Pin::new(&mut futures).poll_next(cx);
Poll::Ready(())
})
.await;

// Note: Unicycle guarantees each future is poll at most once.
assert_eq!(1, count.get());
}

0 comments on commit 793c1f4

Please sign in to comment.