1 use tokio::sync::oneshot; 2 use tokio_util::task::AbortOnDropHandle; 3 4 #[tokio::test] aborts_task_on_drop()5async fn aborts_task_on_drop() { 6 let (mut tx, rx) = oneshot::channel::<bool>(); 7 let handle = tokio::spawn(async move { 8 let _ = rx.await; 9 }); 10 let handle = AbortOnDropHandle::new(handle); 11 drop(handle); 12 tx.closed().await; 13 assert!(tx.is_closed()); 14 } 15 16 #[tokio::test] aborts_task_directly()17async fn aborts_task_directly() { 18 let (mut tx, rx) = oneshot::channel::<bool>(); 19 let handle = tokio::spawn(async move { 20 let _ = rx.await; 21 }); 22 let handle = AbortOnDropHandle::new(handle); 23 handle.abort(); 24 tx.closed().await; 25 assert!(tx.is_closed()); 26 assert!(handle.is_finished()); 27 } 28