Skip to content

Commit d5dc7a8

Browse files
committed
tests: Add tests with more complicated futures
This should catch the errors from earlier. Signed-off-by: John Nunley <dev@notgull.net>
1 parent 2f3189a commit d5dc7a8

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

tests/larger_tasks.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
//! Test for larger tasks.
2+
3+
use async_executor::Executor;
4+
use futures_lite::future::{self, block_on};
5+
use futures_lite::prelude::*;
6+
7+
use std::sync::Arc;
8+
use std::thread;
9+
use std::time::Duration;
10+
11+
fn do_run<Fut: Future<Output = ()>>(mut f: impl FnMut(Arc<Executor<'static>>) -> Fut) {
12+
// This should not run for longer than two minutes.
13+
let (_stop_timeout, stopper) = async_channel::bounded::<()>(1);
14+
thread::spawn(move || {
15+
block_on(async move {
16+
let timeout = async {
17+
async_io::Timer::after(Duration::from_secs(2 * 60)).await;
18+
eprintln!("test timed out after 2m");
19+
std::process::exit(1)
20+
};
21+
22+
let _ = stopper.recv().or(timeout).await;
23+
})
24+
});
25+
26+
let ex = Arc::new(Executor::new());
27+
28+
// Test 1: Use the `run` command.
29+
block_on(ex.run(f(ex.clone())));
30+
31+
// Test 2: Loop on `tick`.
32+
block_on(async {
33+
let ticker = async {
34+
loop {
35+
ex.tick().await;
36+
}
37+
};
38+
39+
f(ex.clone()).or(ticker).await
40+
});
41+
42+
// Test 3: Run on many threads.
43+
thread::scope(|scope| {
44+
let (_signal, shutdown) = async_channel::bounded::<()>(1);
45+
46+
for _ in 0..16 {
47+
let shutdown = shutdown.clone();
48+
let ex = &ex;
49+
scope.spawn(move || block_on(ex.run(shutdown.recv())));
50+
}
51+
52+
block_on(f(ex.clone()));
53+
});
54+
55+
// Test 4: Tick loop on many threads.
56+
thread::scope(|scope| {
57+
let (_signal, shutdown) = async_channel::bounded::<()>(1);
58+
59+
for _ in 0..16 {
60+
let shutdown = shutdown.clone();
61+
let ex = &ex;
62+
scope.spawn(move || {
63+
block_on(async move {
64+
let ticker = async {
65+
loop {
66+
ex.tick().await;
67+
}
68+
};
69+
70+
shutdown.recv().or(ticker).await
71+
})
72+
});
73+
}
74+
75+
block_on(f(ex.clone()));
76+
});
77+
}
78+
79+
#[test]
80+
fn smoke() {
81+
do_run(|ex| async move { ex.spawn(async {}).await });
82+
}
83+
84+
#[test]
85+
fn yield_now() {
86+
do_run(|ex| async move { ex.spawn(future::yield_now()).await })
87+
}
88+
89+
#[test]
90+
fn timer() {
91+
do_run(|ex| async move {
92+
ex.spawn(async_io::Timer::after(Duration::from_millis(5)))
93+
.await;
94+
})
95+
}

0 commit comments

Comments
 (0)