use std::time::Duration;
use crate::channels::telegram::userbot::runner::AbortOnDrop;
async fn settle() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
#[tokio::test]
async fn dropping_the_guard_aborts_the_runner() {
let handle = tokio::spawn(std::future::pending::<()>());
let probe = handle.abort_handle();
let guard = AbortOnDrop::new(handle);
settle().await;
assert!(
!probe.is_finished(),
"a pending runner is alive while guarded"
);
drop(guard);
settle().await;
assert!(
probe.is_finished(),
"dropping the guard must abort the runner"
);
}
#[tokio::test]
async fn aborting_the_owning_task_takes_the_runner_down() {
let runner = tokio::spawn(std::future::pending::<()>());
let probe = runner.abort_handle();
let guard = AbortOnDrop::new(runner);
let watch = tokio::spawn(async move {
std::future::pending::<()>().await;
drop(guard);
});
settle().await;
assert!(
!probe.is_finished(),
"runner alive while the watch task runs"
);
watch.abort();
settle().await;
assert!(
probe.is_finished(),
"aborting the watch task must abort the runner it owns"
);
}