use nezuko::{Runtime, sleep, spawn};
use std::panic::{self, AssertUnwindSafe};
use std::time::Duration;
#[test]
fn panicking_task_does_not_kill_runtime() {
let rt = Runtime::new().unwrap();
let value = rt.block_on(async {
for i in 0..8u32 {
spawn(async move { panic!("task {i} blew up") });
}
let mut handles = Vec::new();
for i in 0..16u32 {
handles.push(spawn(async move {
sleep(Duration::from_millis(10)).await;
i
}));
}
let mut total = 0;
for handle in handles {
total += handle.await;
}
total
});
assert_eq!(value, (0..16).sum::<u32>());
}
#[test]
fn join_handle_propagates_panic() {
let rt = Runtime::new().unwrap();
let result = panic::catch_unwind(AssertUnwindSafe(|| {
rt.block_on(async {
let handle = spawn(async {
sleep(Duration::from_millis(10)).await;
panic!("inner panic");
});
handle.await
})
}));
let payload = result.expect_err("panic should have propagated");
assert_eq!(payload.downcast_ref::<&str>(), Some(&"inner panic"));
}
#[test]
fn block_on_propagates_panic() {
let rt = Runtime::new().unwrap();
let result = panic::catch_unwind(AssertUnwindSafe(|| {
rt.block_on(async {
sleep(Duration::from_millis(10)).await;
panic!("top level panic");
})
}));
let payload = result.expect_err("panic should have propagated");
assert_eq!(payload.downcast_ref::<&str>(), Some(&"top level panic"));
}