-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathspinning_futures_unordered_test.rs
More file actions
50 lines (39 loc) · 1.29 KB
/
Copy pathspinning_futures_unordered_test.rs
File metadata and controls
50 lines (39 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#![cfg(feature = "futures-rs")]
#![cfg(not(miri))]
use futures::future::poll_fn;
use futures::stream::Stream;
use std::cell::Cell;
use std::future::Future;
use std::pin::{pin, Pin};
use std::task::{Context, Poll};
struct Spinner<'a>(&'a Cell<usize>);
impl Future for Spinner<'_> {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
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
}
}
#[tokio::test]
async fn test_spinning_unordered() {
use unicycle::FuturesUnordered;
let count = Cell::new(0);
let mut futures = FuturesUnordered::new();
futures.push(Spinner(&count));
let mut futures = pin!(futures);
poll_fn::<(), _>(move |cx| {
// NB: needs to be polled twice to cause the bitsets to be swapped out.
assert_eq!(Poll::Pending, futures.as_mut().poll_next(cx));
let _ = futures.as_mut().poll_next(cx);
Poll::Ready(())
})
.await;
// Note: Unicycle guarantees each future is poll at most once.
assert_eq!(2, count.get());
}