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