Skip to content

Commit

Permalink
Add panic-on-drop tests for JoinHandle::abort.
Browse files Browse the repository at this point in the history
  • Loading branch information
Diggsey committed Jul 18, 2021
1 parent 6e79a80 commit e0e4dbb
Showing 1 changed file with 60 additions and 1 deletion.
61 changes: 60 additions & 1 deletion tokio/tests/task_abort.rs
Expand Up @@ -172,5 +172,64 @@ fn test_abort_wakes_task_3964() {
// Check that the Arc has been dropped.
assert!(weak_notify_dropped.upgrade().is_none());
});
//panic!();
}

struct PanicOnDrop;

impl Drop for PanicOnDrop {
fn drop(&mut self) {
panic!("Well what did you expect would happen...");
}
}

/// Checks that aborting a task whose destructor panics does not allow the
/// panic to escape the task.
#[test]
fn test_abort_task_that_panics_on_drop_contained() {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();

rt.block_on(async move {
let handle = tokio::spawn(async move {
// Make sure the Arc is moved into the task
let _panic_dropped = PanicOnDrop;
println!("task started");
tokio::time::sleep(std::time::Duration::new(100, 0)).await
});

// wait for task to sleep.
tokio::time::sleep(std::time::Duration::from_millis(100)).await;

handle.abort();
drop(handle);

// wait for task to abort.
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
});
}

/// Checks that aborting a task whose destructor panics has the expected result.
#[test]
fn test_abort_task_that_panics_on_drop_returned() {
let rt = tokio::runtime::Builder::new_current_thread()
.enable_time()
.build()
.unwrap();

rt.block_on(async move {
let handle = tokio::spawn(async move {
// Make sure the Arc is moved into the task
let _panic_dropped = PanicOnDrop;
println!("task started");
tokio::time::sleep(std::time::Duration::new(100, 0)).await
});

// wait for task to sleep.
tokio::time::sleep(std::time::Duration::from_millis(100)).await;

handle.abort();
assert!(handle.await.unwrap_err().is_panic());
});
}

0 comments on commit e0e4dbb

Please sign in to comment.