Debian Codegraph @

tests/stream.rs

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