1 | #![warn (rust_2018_idioms)] |
2 | #![allow (clippy::declare_interior_mutable_const)] |
3 | #![cfg (all(feature = "full" , not(target_os = "wasi" )))] |
4 | |
5 | use futures::future; |
6 | use std::error::Error; |
7 | use tokio::runtime::Builder; |
8 | use tokio::task::{self, block_in_place}; |
9 | |
10 | mod support { |
11 | pub mod panic; |
12 | } |
13 | use support::panic::test_panic; |
14 | |
15 | #[cfg (panic = "unwind" )] |
16 | #[test] |
17 | fn block_in_place_panic_caller() -> Result<(), Box<dyn Error>> { |
18 | let panic_location_file = test_panic(|| { |
19 | let rt = Builder::new_current_thread().enable_all().build().unwrap(); |
20 | rt.block_on(async { |
21 | block_in_place(|| {}); |
22 | }); |
23 | }); |
24 | |
25 | // The panic location should be in this file |
26 | assert_eq!(&panic_location_file.unwrap(), file!()); |
27 | |
28 | Ok(()) |
29 | } |
30 | |
31 | #[cfg (panic = "unwind" )] |
32 | #[test] |
33 | fn local_set_spawn_local_panic_caller() -> Result<(), Box<dyn Error>> { |
34 | let panic_location_file = test_panic(|| { |
35 | let _local = task::LocalSet::new(); |
36 | |
37 | let _ = task::spawn_local(async {}); |
38 | }); |
39 | |
40 | // The panic location should be in this file |
41 | assert_eq!(&panic_location_file.unwrap(), file!()); |
42 | |
43 | Ok(()) |
44 | } |
45 | |
46 | #[cfg (panic = "unwind" )] |
47 | #[test] |
48 | fn local_set_block_on_panic_caller() -> Result<(), Box<dyn Error>> { |
49 | let panic_location_file = test_panic(|| { |
50 | let rt = Builder::new_current_thread().enable_all().build().unwrap(); |
51 | let local = task::LocalSet::new(); |
52 | |
53 | rt.block_on(async { |
54 | local.block_on(&rt, future::pending::<()>()); |
55 | }); |
56 | }); |
57 | |
58 | // The panic location should be in this file |
59 | assert_eq!(&panic_location_file.unwrap(), file!()); |
60 | |
61 | Ok(()) |
62 | } |
63 | |
64 | #[cfg (panic = "unwind" )] |
65 | #[test] |
66 | fn spawn_panic_caller() -> Result<(), Box<dyn Error>> { |
67 | let panic_location_file = test_panic(|| { |
68 | tokio::spawn(future::pending::<()>()); |
69 | }); |
70 | |
71 | // The panic location should be in this file |
72 | assert_eq!(&panic_location_file.unwrap(), file!()); |
73 | |
74 | Ok(()) |
75 | } |
76 | |
77 | #[cfg (panic = "unwind" )] |
78 | #[test] |
79 | fn local_key_sync_scope_panic_caller() -> Result<(), Box<dyn Error>> { |
80 | tokio::task_local! { |
81 | static NUMBER: u32; |
82 | } |
83 | |
84 | let panic_location_file = test_panic(|| { |
85 | NUMBER.sync_scope(1, || { |
86 | NUMBER.with(|_| { |
87 | NUMBER.sync_scope(1, || {}); |
88 | }); |
89 | }); |
90 | }); |
91 | |
92 | // The panic location should be in this file |
93 | assert_eq!(&panic_location_file.unwrap(), file!()); |
94 | |
95 | Ok(()) |
96 | } |
97 | |
98 | #[cfg (panic = "unwind" )] |
99 | #[test] |
100 | fn local_key_with_panic_caller() -> Result<(), Box<dyn Error>> { |
101 | tokio::task_local! { |
102 | static NUMBER: u32; |
103 | } |
104 | |
105 | let panic_location_file = test_panic(|| { |
106 | NUMBER.with(|_| {}); |
107 | }); |
108 | |
109 | // The panic location should be in this file |
110 | assert_eq!(&panic_location_file.unwrap(), file!()); |
111 | |
112 | Ok(()) |
113 | } |
114 | |
115 | #[cfg (panic = "unwind" )] |
116 | #[test] |
117 | fn local_key_get_panic_caller() -> Result<(), Box<dyn Error>> { |
118 | tokio::task_local! { |
119 | static NUMBER: u32; |
120 | } |
121 | |
122 | let panic_location_file = test_panic(|| { |
123 | NUMBER.get(); |
124 | }); |
125 | |
126 | // The panic location should be in this file |
127 | assert_eq!(&panic_location_file.unwrap(), file!()); |
128 | |
129 | Ok(()) |
130 | } |
131 | |