Skip to content

Commit

Permalink
Add example failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sushisource committed Aug 16, 2022
1 parent acdd43f commit 54bcee7
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions futures/tests/stream.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::cell::Cell;
use std::iter;
use std::pin::Pin;
use std::rc::Rc;
use std::sync::Arc;
use std::task::Context;

use futures::channel::mpsc;
use futures::executor::block_on;
Expand All @@ -9,6 +13,7 @@ use futures::sink::SinkExt;
use futures::stream::{self, StreamExt};
use futures::task::Poll;
use futures::{ready, FutureExt};
use futures_core::Stream;
use futures_test::task::noop_context;

#[test]
Expand Down Expand Up @@ -436,3 +441,38 @@ fn ready_chunks() {
assert_eq!(s.next().await.unwrap(), vec![4]);
});
}

struct SlowStream {
times_should_poll: usize,
times_polled: Rc<Cell<usize>>,
}
impl Stream for SlowStream {
type Item = usize;

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.times_polled.set(self.times_polled.get() + 1);
if self.times_polled.get() % 2 == 0 {
cx.waker().wake_by_ref();
return Poll::Pending;
}
if self.times_polled.get() == self.times_should_poll {
return Poll::Ready(None);
}
Poll::Ready(Some(self.times_polled.get()))
}
}

#[test]
fn select_with_strategy_doesnt_terminate_early() {
let times_should_poll = 10;
let count = Rc::new(Cell::new(0));
let b = stream::iter([10, 20]);

let selected = stream::select_with_strategy(
SlowStream { times_should_poll, times_polled: count.clone() },
b,
|_: &mut ()| stream::PollNext::Left,
);
block_on(selected.for_each(|v| async move { println!("{}", v) }));
assert_eq!(count.get(), times_should_poll);
}

0 comments on commit 54bcee7

Please sign in to comment.