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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
|
use futures::{stream, Stream};
#[cfg(all(feature = "async", not(target_os = "unknown")))]
use {
async_std::prelude::FutureExt,
flume::*,
futures::{stream::FuturesUnordered, StreamExt, TryFutureExt},
std::time::Duration,
};
#[cfg(all(feature = "async", not(target_os = "unknown")))]
#[test]
fn stream_recv() {
let (tx, rx) = unbounded();
let t = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(250));
tx.send(42u32).unwrap();
println!("sent");
});
async_std::task::block_on(async {
println!("receiving...");
let x = rx.stream().next().await;
println!("received");
assert_eq!(x, Some(42));
});
t.join().unwrap();
}
#[cfg(all(feature = "async", not(target_os = "unknown")))]
#[test]
fn stream_recv_disconnect() {
let (tx, rx) = bounded::<i32>(0);
let t = std::thread::spawn(move || {
let _ = tx.send(42);
std::thread::sleep(std::time::Duration::from_millis(250));
drop(tx)
});
async_std::task::block_on(async {
let mut stream = rx.into_stream();
assert_eq!(stream.next().await, Some(42));
assert_eq!(stream.next().await, None);
});
t.join().unwrap();
}
#[cfg(all(feature = "async", not(target_os = "unknown")))]
#[test]
fn stream_recv_drop_recv() {
let (tx, rx) = bounded::<i32>(10);
let rx2 = rx.clone();
let mut stream = rx.into_stream();
async_std::task::block_on(async {
let res =
async_std::future::timeout(std::time::Duration::from_millis(500), stream.next()).await;
assert!(res.is_err());
});
let t =
std::thread::spawn(move || async_std::task::block_on(async { rx2.stream().next().await }));
std::thread::sleep(std::time::Duration::from_millis(500));
tx.send(42).unwrap();
drop(stream);
assert_eq!(t.join().unwrap(), Some(42))
}
#[cfg(all(feature = "async", not(target_os = "unknown")))]
#[test]
fn r#stream_drop_send_disconnect() {
let (tx, rx) = bounded::<i32>(1);
let t = std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(250));
drop(tx);
});
async_std::task::block_on(async {
let mut stream = rx.into_stream();
assert_eq!(stream.next().await, None);
});
t.join().unwrap();
}
#[cfg(all(feature = "async", not(target_os = "unknown")))]
#[async_std::test]
async fn stream_send_1_million_no_drop_or_reorder() {
#[derive(Debug)]
enum Message {
Increment { old: u64 },
ReturnCount,
}
let (tx, rx) = unbounded();
let t = async_std::task::spawn(async move {
let mut count = 0u64;
let mut stream = rx.into_stream();
while let Some(Message::Increment { old }) = stream.next().await {
assert_eq!(old, count);
count += 1;
}
count
});
for next in 0..1_000_000 {
tx.send(Message::Increment { old: next }).unwrap();
}
tx.send(Message::ReturnCount).unwrap();
let count = t.await;
assert_eq!(count, 1_000_000)
}
#[cfg(all(feature = "async", not(target_os = "unknown")))]
#[async_std::test]
async fn parallel_streams_and_async_recv() {
let (tx, rx) = flume::unbounded();
let rx = ℞
let send_fut = async move {
let n_sends: usize = 100000;
for _ in 0..n_sends {
tx.send_async(()).await.unwrap();
}
};
async_std::task::spawn(
send_fut
.timeout(Duration::from_secs(5))
.map_err(|_| panic!("Send timed out!")),
);
let mut futures_unordered = (0..250)
.map(|n| async move {
if n % 2 == 0 {
let mut stream = rx.stream();
while let Some(()) = stream.next().await {}
} else {
while let Ok(()) = rx.recv_async().await {}
}
})
.collect::<FuturesUnordered<_>>();
let recv_fut = async { while futures_unordered.next().await.is_some() {} };
recv_fut
.timeout(Duration::from_secs(5))
.map_err(|_| panic!("Receive timed out!"))
.await
.unwrap();
}
#[cfg(all(feature = "async", not(target_os = "unknown")))]
#[test]
fn stream_no_double_wake() {
use futures::task::{waker, ArcWake};
use futures::Stream;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::task::Context;
let count = Arc::new(AtomicUsize::new(0));
// all this waker does is count how many times it is called
struct CounterWaker {
count: Arc<AtomicUsize>,
}
impl ArcWake for CounterWaker {
fn wake_by_ref(arc_self: &Arc<Self>) {
arc_self.count.fetch_add(1, Ordering::SeqCst);
}
}
// create waker and context
let w = CounterWaker {
count: count.clone(),
};
let w = waker(Arc::new(w));
let cx = &mut Context::from_waker(&w);
// create unbounded channel
let (tx, rx) = unbounded::<()>();
let mut stream = rx.stream();
// register waker with stream
let _ = Pin::new(&mut stream).poll_next(cx);
// send multiple items
tx.send(()).unwrap();
tx.send(()).unwrap();
tx.send(()).unwrap();
// verify that stream is only woken up once.
assert_eq!(count.load(Ordering::SeqCst), 1);
}
#[cfg(all(feature = "async", not(target_os = "unknown")))]
#[async_std::test]
async fn stream_forward_issue_55() {
// https://github.com/zesterer/flume/issues/55
fn dummy_stream() -> impl Stream<Item = usize> {
stream::unfold(0, |count| async move {
if count < 1000 {
Some((count, count + 1))
} else {
None
}
})
}
let (send_task, recv_task) = {
use futures::SinkExt;
let (tx, rx) = flume::bounded(100);
let send_task = dummy_stream().map(Ok).forward(
tx.into_sink()
.sink_map_err(|e| panic!("send error:{:#?}", e)),
);
let recv_task = rx.into_stream().for_each(|_item| async move {});
(send_task, recv_task)
};
let jh = async_std::task::spawn(send_task);
async_std::task::block_on(recv_task);
jh.await.unwrap();
}
|