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 | #[cfg (panic = "unwind" )] |
15 | #[test] |
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 | #[cfg (panic = "unwind" )] |
28 | #[test] |
29 | fn into_panic_panic_caller() -> Result<(), Box<dyn Error>> { |
30 | let panic_location_file = test_panic(move || { |
31 | let rt = current_thread(); |
32 | rt.block_on(async { |
33 | let handle = tokio::spawn(future::pending::<()>()); |
34 | |
35 | handle.abort(); |
36 | |
37 | let err = handle.await.unwrap_err(); |
38 | assert!(!&err.is_panic()); |
39 | |
40 | let _ = err.into_panic(); |
41 | }); |
42 | }); |
43 | |
44 | // The panic location should be in this file |
45 | assert_eq!(&panic_location_file.unwrap(), file!()); |
46 | |
47 | Ok(()) |
48 | } |
49 | |
50 | #[cfg (panic = "unwind" )] |
51 | #[test] |
52 | fn builder_worker_threads_panic_caller() -> Result<(), Box<dyn Error>> { |
53 | let panic_location_file = test_panic(|| { |
54 | let _ = Builder::new_multi_thread().worker_threads(0).build(); |
55 | }); |
56 | |
57 | // The panic location should be in this file |
58 | assert_eq!(&panic_location_file.unwrap(), file!()); |
59 | |
60 | Ok(()) |
61 | } |
62 | |
63 | #[cfg (panic = "unwind" )] |
64 | #[test] |
65 | fn builder_max_blocking_threads_panic_caller() -> Result<(), Box<dyn Error>> { |
66 | let panic_location_file = test_panic(|| { |
67 | let _ = Builder::new_multi_thread().max_blocking_threads(0).build(); |
68 | }); |
69 | |
70 | // The panic location should be in this file |
71 | assert_eq!(&panic_location_file.unwrap(), file!()); |
72 | |
73 | Ok(()) |
74 | } |
75 | |
76 | fn current_thread() -> Runtime { |
77 | tokio::runtime::Builder::new_current_thread() |
78 | .enable_all() |
79 | .build() |
80 | .unwrap() |
81 | } |
82 | |