use std::time::{Duration, Instant};
#[test]
fn returns_value_and_borrows_locals() {
let output = std::thread::spawn(|| {
let greeting = String::from("hello");
runite::block_on(async { format!("{greeting} world") })
})
.join()
.expect("runtime thread should not panic");
assert_eq!(output, "hello world");
}
#[test]
fn drives_timers() {
let elapsed = std::thread::spawn(|| {
runite::block_on(async {
let start = Instant::now();
runite::time::sleep(Duration::from_millis(10)).await;
start.elapsed()
})
})
.join()
.expect("runtime thread should not panic");
assert!(
elapsed >= Duration::from_millis(10),
"block_on should have driven the 10ms sleep, only {elapsed:?} elapsed"
);
}
#[test]
fn returns_before_unfinished_background_tasks() {
let value = std::thread::spawn(|| {
runite::spawn(async { std::future::pending::<()>().await });
runite::block_on(async { 99u32 })
})
.join()
.expect("runtime thread should not panic");
assert_eq!(value, 99);
}
#[test]
fn nested_block_on_panics() {
let result = std::thread::spawn(|| {
std::panic::catch_unwind(|| {
runite::block_on(async {
runite::block_on(async {});
});
})
})
.join()
.expect("runtime thread should not panic at the OS-thread boundary");
assert!(
result.is_err(),
"a nested block_on must panic via the reentrancy guard"
);
}