1 | use futures::executor::block_on; |
2 | use futures::future::{self, BoxFuture, FutureExt}; |
3 | use std::sync::mpsc; |
4 | use std::thread; |
5 | |
6 | #[test] |
7 | fn lots() { |
8 | #[cfg (not(futures_sanitizer))] |
9 | const N: i32 = 1_000; |
10 | #[cfg (futures_sanitizer)] // If N is many, asan reports stack-overflow: https://gist.github.com/taiki-e/099446d21cbec69d4acbacf7a9646136 |
11 | const N: i32 = 100; |
12 | |
13 | fn do_it(input: (i32, i32)) -> BoxFuture<'static, i32> { |
14 | let (n, x) = input; |
15 | if n == 0 { |
16 | future::ready(x).boxed() |
17 | } else { |
18 | future::ready((n - 1, x + n)).then(do_it).boxed() |
19 | } |
20 | } |
21 | |
22 | let (tx, rx) = mpsc::channel(); |
23 | thread::spawn(|| block_on(do_it((N, 0)).map(move |x| tx.send(x).unwrap()))); |
24 | assert_eq!((0..=N).sum::<i32>(), rx.recv().unwrap()); |
25 | } |
26 | |