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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
|
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;
use futures::future::{self, Future};
use futures::lock::Mutex;
use futures::sink::SinkExt;
use futures::stream::{self, StreamExt};
use futures::task::Poll;
use futures::{ready, FutureExt};
use futures_core::Stream;
use futures_executor::ThreadPool;
use futures_test::task::noop_context;
#[test]
fn select() {
fn select_and_compare(a: Vec<u32>, b: Vec<u32>, expected: Vec<u32>) {
let a = stream::iter(a);
let b = stream::iter(b);
let vec = block_on(stream::select(a, b).collect::<Vec<_>>());
assert_eq!(vec, expected);
}
select_and_compare(vec![1, 2, 3], vec![4, 5, 6], vec![1, 4, 2, 5, 3, 6]);
select_and_compare(vec![1, 2, 3], vec![4, 5], vec![1, 4, 2, 5, 3]);
select_and_compare(vec![1, 2], vec![4, 5, 6], vec![1, 4, 2, 5, 6]);
}
#[test]
fn flat_map() {
block_on(async {
let st =
stream::iter(vec![stream::iter(0..=4u8), stream::iter(6..=10), stream::iter(0..=2)]);
let values: Vec<_> =
st.flat_map(|s| s.filter(|v| futures::future::ready(v % 2 == 0))).collect().await;
assert_eq!(values, vec![0, 2, 4, 6, 8, 10, 0, 2]);
});
}
#[test]
fn scan() {
block_on(async {
let values = stream::iter(vec![1u8, 2, 3, 4, 6, 8, 2])
.scan(1, |state, e| {
*state += 1;
futures::future::ready(if e < *state { Some(e) } else { None })
})
.collect::<Vec<_>>()
.await;
assert_eq!(values, vec![1u8, 2, 3, 4]);
});
}
#[test]
fn flatten_unordered() {
use futures::executor::block_on;
use futures::stream::*;
use futures::task::*;
use std::convert::identity;
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::Duration;
struct DataStream {
data: Vec<u8>,
polled: bool,
wake_immediately: bool,
}
impl Stream for DataStream {
type Item = u8;
fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
if !self.polled {
if !self.wake_immediately {
let waker = ctx.waker().clone();
let sleep_time =
Duration::from_millis(*self.data.first().unwrap_or(&0) as u64 / 10);
thread::spawn(move || {
thread::sleep(sleep_time);
waker.wake_by_ref();
});
} else {
ctx.waker().wake_by_ref();
}
self.polled = true;
Poll::Pending
} else {
self.polled = false;
Poll::Ready(self.data.pop())
}
}
}
struct Interchanger {
polled: bool,
base: u8,
wake_immediately: bool,
}
impl Stream for Interchanger {
type Item = DataStream;
fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Option<Self::Item>> {
if !self.polled {
self.polled = true;
if !self.wake_immediately {
let waker = ctx.waker().clone();
let sleep_time = Duration::from_millis(self.base as u64);
thread::spawn(move || {
thread::sleep(sleep_time);
waker.wake_by_ref();
});
} else {
ctx.waker().wake_by_ref();
}
Poll::Pending
} else {
let data: Vec<_> = (0..6).rev().map(|v| v + self.base * 6).collect();
self.base += 1;
self.polled = false;
Poll::Ready(Some(DataStream {
polled: false,
data,
wake_immediately: self.wake_immediately && self.base % 2 == 0,
}))
}
}
}
// basic behaviour
{
block_on(async {
let st = stream::iter(vec![
stream::iter(0..=4u8),
stream::iter(6..=10),
stream::iter(10..=12),
]);
let fl_unordered = st.flatten_unordered(3).collect::<Vec<_>>().await;
assert_eq!(fl_unordered, vec![0, 6, 10, 1, 7, 11, 2, 8, 12, 3, 9, 4, 10]);
});
block_on(async {
let st = stream::iter(vec![
stream::iter(0..=4u8),
stream::iter(6..=10),
stream::iter(0..=2),
]);
let mut fm_unordered = st
.flat_map_unordered(1, |s| s.filter(|v| futures::future::ready(v % 2 == 0)))
.collect::<Vec<_>>()
.await;
fm_unordered.sort_unstable();
assert_eq!(fm_unordered, vec![0, 0, 2, 2, 4, 6, 8, 10]);
});
}
// wake up immediately
{
block_on(async {
let mut fl_unordered = Interchanger { polled: false, base: 0, wake_immediately: true }
.take(10)
.map(|s| s.map(identity))
.flatten_unordered(10)
.collect::<Vec<_>>()
.await;
fl_unordered.sort_unstable();
assert_eq!(fl_unordered, (0..60).collect::<Vec<u8>>());
});
block_on(async {
let mut fm_unordered = Interchanger { polled: false, base: 0, wake_immediately: true }
.take(10)
.flat_map_unordered(10, |s| s.map(identity))
.collect::<Vec<_>>()
.await;
fm_unordered.sort_unstable();
assert_eq!(fm_unordered, (0..60).collect::<Vec<u8>>());
});
}
// wake up after delay
{
block_on(async {
let mut fl_unordered = Interchanger { polled: false, base: 0, wake_immediately: false }
.take(10)
.map(|s| s.map(identity))
.flatten_unordered(10)
.collect::<Vec<_>>()
.await;
fl_unordered.sort_unstable();
assert_eq!(fl_unordered, (0..60).collect::<Vec<u8>>());
});
block_on(async {
let mut fm_unordered = Interchanger { polled: false, base: 0, wake_immediately: false }
.take(10)
.flat_map_unordered(10, |s| s.map(identity))
.collect::<Vec<_>>()
.await;
fm_unordered.sort_unstable();
assert_eq!(fm_unordered, (0..60).collect::<Vec<u8>>());
});
block_on(async {
let (mut fm_unordered, mut fl_unordered) = futures_util::join!(
Interchanger { polled: false, base: 0, wake_immediately: false }
.take(10)
.flat_map_unordered(10, |s| s.map(identity))
.collect::<Vec<_>>(),
Interchanger { polled: false, base: 0, wake_immediately: false }
.take(10)
.map(|s| s.map(identity))
.flatten_unordered(10)
.collect::<Vec<_>>()
);
fm_unordered.sort_unstable();
fl_unordered.sort_unstable();
assert_eq!(fm_unordered, fl_unordered);
assert_eq!(fm_unordered, (0..60).collect::<Vec<u8>>());
});
}
// waker panics
{
let stream = Arc::new(Mutex::new(
Interchanger { polled: false, base: 0, wake_immediately: true }
.take(10)
.flat_map_unordered(10, |s| s.map(identity)),
));
struct PanicWaker;
impl ArcWake for PanicWaker {
fn wake_by_ref(_arc_self: &Arc<Self>) {
panic!("WAKE UP");
}
}
std::thread::spawn({
let stream = stream.clone();
move || {
let mut st = poll_fn(|cx| {
let mut lock = ready!(stream.lock().poll_unpin(cx));
let panic_waker = waker(Arc::new(PanicWaker));
let mut panic_cx = Context::from_waker(&panic_waker);
let _ = ready!(lock.poll_next_unpin(&mut panic_cx));
Poll::Ready(Some(()))
});
block_on(st.next())
}
})
.join()
.unwrap_err();
block_on(async move {
let mut values: Vec<_> = stream.lock().await.by_ref().collect().await;
values.sort_unstable();
assert_eq!(values, (0..60).collect::<Vec<u8>>());
});
}
// stream panics
{
let st = stream::iter(iter::once(
once(Box::pin(async { panic!("Polled") })).left_stream::<DataStream>(),
))
.chain(
Interchanger { polled: false, base: 0, wake_immediately: true }
.map(|stream| stream.right_stream())
.take(10),
);
let stream = Arc::new(Mutex::new(st.flatten_unordered(10)));
std::thread::spawn({
let stream = stream.clone();
move || {
let mut st = poll_fn(|cx| {
let mut lock = ready!(stream.lock().poll_unpin(cx));
let data = ready!(lock.poll_next_unpin(cx));
Poll::Ready(data)
});
block_on(st.next())
}
})
.join()
.unwrap_err();
block_on(async move {
let mut values: Vec<_> = stream.lock().await.by_ref().collect().await;
values.sort_unstable();
assert_eq!(values, (0..60).collect::<Vec<u8>>());
});
}
fn timeout<I: Clone>(time: Duration, value: I) -> impl Future<Output = I> {
let ready = Arc::new(AtomicBool::new(false));
let mut spawned = false;
future::poll_fn(move |cx| {
if !spawned {
let waker = cx.waker().clone();
let ready = ready.clone();
std::thread::spawn(move || {
std::thread::sleep(time);
ready.store(true, Ordering::Release);
waker.wake_by_ref()
});
spawned = true;
}
if ready.load(Ordering::Acquire) {
Poll::Ready(value.clone())
} else {
Poll::Pending
}
})
}
fn build_nested_fu<S: Stream + Unpin>(st: S) -> impl Stream<Item = S::Item> + Unpin
where
S::Item: Clone,
{
let inner = st
.then(|item| timeout(Duration::from_millis(50), item))
.enumerate()
.map(|(idx, value)| {
stream::once(if idx % 2 == 0 {
future::ready(value).left_future()
} else {
timeout(Duration::from_millis(100), value).right_future()
})
})
.flatten_unordered(None);
stream::once(future::ready(inner)).flatten_unordered(None)
}
// nested `flatten_unordered`
let te = ThreadPool::new().unwrap();
let base_handle = te
.spawn_with_handle(async move {
let fu = build_nested_fu(stream::iter(1..=10));
assert_eq!(fu.count().await, 10);
})
.unwrap();
block_on(base_handle);
let empty_state_move_handle = te
.spawn_with_handle(async move {
let mut fu = build_nested_fu(stream::iter(1..10));
{
let mut cx = noop_context();
let _ = fu.poll_next_unpin(&mut cx);
let _ = fu.poll_next_unpin(&mut cx);
}
assert_eq!(fu.count().await, 9);
})
.unwrap();
block_on(empty_state_move_handle);
}
#[test]
fn take_until() {
fn make_stop_fut(stop_on: u32) -> impl Future<Output = ()> {
let mut i = 0;
future::poll_fn(move |_cx| {
i += 1;
if i <= stop_on {
Poll::Pending
} else {
Poll::Ready(())
}
})
}
block_on(async {
// Verify stopping works:
let stream = stream::iter(1u32..=10);
let stop_fut = make_stop_fut(5);
let stream = stream.take_until(stop_fut);
let last = stream.fold(0, |_, i| async move { i }).await;
assert_eq!(last, 5);
// Verify take_future() works:
let stream = stream::iter(1..=10);
let stop_fut = make_stop_fut(5);
let mut stream = stream.take_until(stop_fut);
assert_eq!(stream.next().await, Some(1));
assert_eq!(stream.next().await, Some(2));
stream.take_future();
let last = stream.fold(0, |_, i| async move { i }).await;
assert_eq!(last, 10);
// Verify take_future() returns None if stream is stopped:
let stream = stream::iter(1u32..=10);
let stop_fut = make_stop_fut(1);
let mut stream = stream.take_until(stop_fut);
assert_eq!(stream.next().await, Some(1));
assert_eq!(stream.next().await, None);
assert!(stream.take_future().is_none());
// Verify TakeUntil is fused:
let mut i = 0;
let stream = stream::poll_fn(move |_cx| {
i += 1;
match i {
1 => Poll::Ready(Some(1)),
2 => Poll::Ready(None),
_ => panic!("TakeUntil not fused"),
}
});
let stop_fut = make_stop_fut(1);
let mut stream = stream.take_until(stop_fut);
assert_eq!(stream.next().await, Some(1));
assert_eq!(stream.next().await, None);
assert_eq!(stream.next().await, None);
});
}
#[test]
#[should_panic]
fn chunks_panic_on_cap_zero() {
let (_, rx1) = mpsc::channel::<()>(1);
let _ = rx1.chunks(0);
}
#[test]
#[should_panic]
fn ready_chunks_panic_on_cap_zero() {
let (_, rx1) = mpsc::channel::<()>(1);
let _ = rx1.ready_chunks(0);
}
#[test]
fn ready_chunks() {
let (mut tx, rx1) = mpsc::channel::<i32>(16);
let mut s = rx1.ready_chunks(2);
let mut cx = noop_context();
assert!(s.next().poll_unpin(&mut cx).is_pending());
block_on(async {
tx.send(1).await.unwrap();
assert_eq!(s.next().await.unwrap(), vec![1]);
tx.send(2).await.unwrap();
tx.send(3).await.unwrap();
tx.send(4).await.unwrap();
assert_eq!(s.next().await.unwrap(), vec![2, 3]);
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() {
for side in [stream::PollNext::Left, stream::PollNext::Right] {
let times_should_poll = 10;
let count = Rc::new(Cell::new(0));
let b = stream::iter([10, 20]);
let mut selected = stream::select_with_strategy(
SlowStream { times_should_poll, times_polled: count.clone() },
b,
|_: &mut ()| side,
);
block_on(async move { while selected.next().await.is_some() {} });
assert_eq!(count.get(), times_should_poll + 1);
}
}
|