1 #![warn(rust_2018_idioms)]
2 #![cfg(feature = "full")]
3 #![cfg(not(target_os = "wasi"))] // Wasi doesn't support panic recovery
4 #![cfg(panic = "unwind")]
5
6 use futures::future;
7 use std::error::Error;
8 use tokio::runtime::{Builder, Handle, Runtime};
9
10 mod support {
11 pub mod panic;
12 }
13 use support::panic::test_panic;
14
15 #[test]
current_handle_panic_caller() -> Result<(), Box<dyn Error>>16 fn current_handle_panic_caller() -> Result<(), Box<dyn Error>> {
17 let panic_location_file = test_panic(|| {
18 let _ = Handle::current();
19 });
20
21 // The panic location should be in this file
22 assert_eq!(&panic_location_file.unwrap(), file!());
23
24 Ok(())
25 }
26
27 #[test]
into_panic_panic_caller() -> Result<(), Box<dyn Error>>28 fn into_panic_panic_caller() -> Result<(), Box<dyn Error>> {
29 let panic_location_file = test_panic(move || {
30 let rt = current_thread();
31 rt.block_on(async {
32 let handle = tokio::spawn(future::pending::<()>());
33
34 handle.abort();
35
36 let err = handle.await.unwrap_err();
37 assert!(!&err.is_panic());
38
39 let _ = err.into_panic();
40 });
41 });
42
43 // The panic location should be in this file
44 assert_eq!(&panic_location_file.unwrap(), file!());
45
46 Ok(())
47 }
48
49 #[test]
builder_worker_threads_panic_caller() -> Result<(), Box<dyn Error>>50 fn builder_worker_threads_panic_caller() -> Result<(), Box<dyn Error>> {
51 let panic_location_file = test_panic(|| {
52 let _ = Builder::new_multi_thread().worker_threads(0).build();
53 });
54
55 // The panic location should be in this file
56 assert_eq!(&panic_location_file.unwrap(), file!());
57
58 Ok(())
59 }
60
61 #[test]
builder_max_blocking_threads_panic_caller() -> Result<(), Box<dyn Error>>62 fn builder_max_blocking_threads_panic_caller() -> Result<(), Box<dyn Error>> {
63 let panic_location_file = test_panic(|| {
64 let _ = Builder::new_multi_thread().max_blocking_threads(0).build();
65 });
66
67 // The panic location should be in this file
68 assert_eq!(&panic_location_file.unwrap(), file!());
69
70 Ok(())
71 }
72
73 #[test]
builder_global_queue_interval_panic_caller() -> Result<(), Box<dyn Error>>74 fn builder_global_queue_interval_panic_caller() -> Result<(), Box<dyn Error>> {
75 let panic_location_file = test_panic(|| {
76 let _ = Builder::new_multi_thread().global_queue_interval(0).build();
77 });
78
79 // The panic location should be in this file
80 assert_eq!(&panic_location_file.unwrap(), file!());
81
82 Ok(())
83 }
84
current_thread() -> Runtime85 fn current_thread() -> Runtime {
86 tokio::runtime::Builder::new_current_thread()
87 .enable_all()
88 .build()
89 .unwrap()
90 }
91