From 370e75417efb6ec55d06b24a21631b29a44bc391 Mon Sep 17 00:00:00 2001 From: Abutalib Aghayev Date: Wed, 9 Nov 2022 10:03:59 -0500 Subject: [PATCH] add more tests -- one to go --- tokio/tests/task_local.rs | 51 +++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/tokio/tests/task_local.rs b/tokio/tests/task_local.rs index 49a416a69d5..d87f1760bf2 100644 --- a/tokio/tests/task_local.rs +++ b/tokio/tests/task_local.rs @@ -193,7 +193,7 @@ async fn task_ids_match_multi_thread() { async fn task_id_future_destructor_completion() { use tokio::task; - struct MyFuture {} + struct MyFuture; impl Future for MyFuture { type Output = (); @@ -207,7 +207,7 @@ async fn task_id_future_destructor_completion() { } } - tokio::spawn(MyFuture {}).await.unwrap(); + tokio::spawn(MyFuture).await.unwrap(); } #[cfg(tokio_unstable)] @@ -215,7 +215,7 @@ async fn task_id_future_destructor_completion() { async fn task_id_future_destructor_abort() { use tokio::task; - struct MyFuture {} + struct MyFuture; impl Future for MyFuture { type Output = (); @@ -229,7 +229,42 @@ async fn task_id_future_destructor_abort() { } } - tokio::spawn(MyFuture {}).abort(); + tokio::spawn(MyFuture).abort(); +} + +#[cfg(tokio_unstable)] +#[tokio::test(flavor = "current_thread")] +async fn task_id_output_destructor_handle_dropped_before_completion() { + use tokio::task; + + struct MyOutput; + impl Drop for MyOutput { + fn drop(&mut self) { + println!("task id: {}", task::id()); + } + } + + struct MyFuture { + tx: Option>, + } + impl Future for MyFuture { + type Output = MyOutput; + + fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll { + let _ = self.tx.take().unwrap().send(()); + Poll::Ready(MyOutput) + } + } + impl Drop for MyFuture { + fn drop(&mut self) { + println!("task id: {}", task::id()); + } + } + + let (tx, rx) = oneshot::channel(); + let handle = tokio::spawn(MyFuture { tx: Some(tx) }); + drop(handle); + rx.await.unwrap(); } #[cfg(tokio_unstable)] @@ -237,10 +272,10 @@ async fn task_id_future_destructor_abort() { async fn task_id_output_destructor_handle_dropped_after_completion() { use tokio::task; - struct MyOutput {} + struct MyOutput; impl Drop for MyOutput { fn drop(&mut self) { - println!("output task id: {}", task::id()); + println!("task id: {}", task::id()); } } @@ -252,12 +287,12 @@ async fn task_id_output_destructor_handle_dropped_after_completion() { fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll { let _ = self.tx.take().unwrap().send(()); - Poll::Ready(MyOutput {}) + Poll::Ready(MyOutput) } } impl Drop for MyFuture { fn drop(&mut self) { - println!("future task id: {}", task::id()); + println!("task id: {}", task::id()); } }