Skip to content

Commit

Permalink
macros: add docs for unhandled_panic
Browse files Browse the repository at this point in the history
  • Loading branch information
name1e5s committed Jun 2, 2024
1 parent 47597a0 commit 9290e24
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
77 changes: 77 additions & 0 deletions tokio-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,44 @@ use proc_macro::TokenStream;
/// })
/// }
/// ```
///
/// ### Configure unhandled panic behavior
///
/// Available options are `shutdown_runtime` and `ignore`. For more details, see
/// [`Builder::unhandled_panic`].
///
/// ```ignore
/// #[tokio::main(unhandled_panic = "shutdown_runtime")]
/// async fn main() {
/// let _ = tokio::spawn(async {
/// panic!("This panic will shutdown the runtime.");
/// }).await;
/// }
/// ```
///
/// Equivalent code not using `#[tokio::test]`
///
/// ```ignore
/// fn main() {
/// tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .unhandled_panic(UnhandledPanic::ShutdownRuntime)
/// .build()
/// .unwrap()
/// .block_on(async {
/// let _ = tokio::spawn(async {
/// panic!("This panic will shutdown the runtime.");
/// }).await;
/// })
/// }
/// ```
///
/// **Note**: This option depends on Tokio's [unstable API][unstable]. See [the
/// documentation on unstable features][unstable] for details on how to enable
/// Tokio's unstable features.
///
/// [`Builder::unhandled_panic`]: ../tokio/runtime/struct.Builder.html#method.unhandled_panic
/// [unstable]: ../tokio/index.html#unstable-features
#[proc_macro_attribute]
pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
entry::main(args.into(), item.into(), true).into()
Expand Down Expand Up @@ -423,6 +461,45 @@ pub fn main_rt(args: TokenStream, item: TokenStream) -> TokenStream {
/// println!("Hello world");
/// }
/// ```
///
/// ### Configure unhandled panic behavior
///
/// Available options are `shutdown_runtime` and `ignore`. For more details, see
/// [`Builder::unhandled_panic`].
///
/// ```ignore
/// #[tokio::test(unhandled_panic = "shutdown_runtime")]
/// async fn my_test() {
/// let _ = tokio::spawn(async {
/// panic!("This panic will shutdown the runtime.");
/// }).await;
/// }
/// ```
///
/// Equivalent code not using `#[tokio::test]`
///
/// ```ignore
/// #[test]
/// fn my_test() {
/// tokio::runtime::Builder::new_current_thread()
/// .enable_all()
/// .unhandled_panic(UnhandledPanic::ShutdownRuntime)
/// .build()
/// .unwrap()
/// .block_on(async {
/// let _ = tokio::spawn(async {
/// panic!("This panic will shutdown the runtime.");
/// }).await;
/// })
/// }
/// ```
///
/// **Note**: This option depends on Tokio's [unstable API][unstable]. See [the
/// documentation on unstable features][unstable] for details on how to enable
/// Tokio's unstable features.
///
/// [`Builder::unhandled_panic`]: ../tokio/runtime/struct.Builder.html#method.unhandled_panic
/// [unstable]: ../tokio/index.html#unstable-features
#[proc_macro_attribute]
pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
entry::test(args.into(), item.into(), true).into()
Expand Down
3 changes: 1 addition & 2 deletions tokio/tests/macros_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ pub mod macro_rt_arg_unhandled_panic {
#[tokio::test(unhandled_panic = "shutdown_runtime")]
#[should_panic]
async fn unhandled_panic_shutdown_runtime() {
let rt = tokio::spawn(async {
let _ = tokio::spawn(async {
panic!("This panic should shutdown the runtime.");
})
.await;
assert_err!(rt);
}

#[tokio::test(unhandled_panic = "ignore")]
Expand Down

0 comments on commit 9290e24

Please sign in to comment.