use rstest::rstest;
use tokio::signal::unix::{signal, SignalKind};
use tokio::time::{sleep, timeout, Duration};
use crate::tasks::{inner_spawn_with_exit_on_panic, spawn_with_exit_on_panic};
#[rstest]
#[tokio::test]
async fn test_spawn_with_exit_on_panic_success() {
let handle = spawn_with_exit_on_panic(async {
sleep(Duration::from_millis(10)).await;
});
handle.await.unwrap();
}
#[rstest]
#[tokio::test]
async fn test_spawn_with_exit_on_panic_failure() {
let mock_exit_process = || {
let pid = nix::unistd::getpid();
nix::sys::signal::kill(pid, nix::sys::signal::Signal::SIGTERM)
.expect("Failed to send signal");
};
let mut sigterm = signal(SignalKind::terminate()).expect("Failed to set up SIGTERM handler");
inner_spawn_with_exit_on_panic(
async {
panic!("This task will fail!");
},
mock_exit_process,
);
assert!(
timeout(Duration::from_millis(10), sigterm.recv()).await.is_ok(),
"Did not receive SIGTERM signal."
);
}