• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![warn(rust_2018_idioms)]
2 #![cfg(all(feature = "full", not(target_os = "wasi")))] // Wasi doesn't support panic recovery
3 
4 use futures::future;
5 use std::error::Error;
6 use std::time::Duration;
7 use tokio::runtime::{Builder, Runtime};
8 use tokio::time::{self, interval, interval_at, timeout, Instant};
9 
10 mod support {
11     pub mod panic;
12 }
13 use support::panic::test_panic;
14 
15 #[test]
pause_panic_caller() -> Result<(), Box<dyn Error>>16 fn pause_panic_caller() -> Result<(), Box<dyn Error>> {
17     let panic_location_file = test_panic(|| {
18         let rt = current_thread();
19 
20         rt.block_on(async {
21             time::pause();
22             time::pause();
23         });
24     });
25 
26     // The panic location should be in this file
27     assert_eq!(&panic_location_file.unwrap(), file!());
28 
29     Ok(())
30 }
31 
32 #[test]
resume_panic_caller() -> Result<(), Box<dyn Error>>33 fn resume_panic_caller() -> Result<(), Box<dyn Error>> {
34     let panic_location_file = test_panic(|| {
35         let rt = current_thread();
36 
37         rt.block_on(async {
38             time::resume();
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]
interval_panic_caller() -> Result<(), Box<dyn Error>>49 fn interval_panic_caller() -> Result<(), Box<dyn Error>> {
50     let panic_location_file = test_panic(|| {
51         let _ = interval(Duration::from_millis(0));
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]
interval_at_panic_caller() -> Result<(), Box<dyn Error>>61 fn interval_at_panic_caller() -> Result<(), Box<dyn Error>> {
62     let panic_location_file = test_panic(|| {
63         let _ = interval_at(Instant::now(), Duration::from_millis(0));
64     });
65 
66     // The panic location should be in this file
67     assert_eq!(&panic_location_file.unwrap(), file!());
68 
69     Ok(())
70 }
71 
72 #[test]
timeout_panic_caller() -> Result<(), Box<dyn Error>>73 fn timeout_panic_caller() -> Result<(), Box<dyn Error>> {
74     let panic_location_file = test_panic(|| {
75         // Runtime without `enable_time` so it has no current timer set.
76         let rt = Builder::new_current_thread().build().unwrap();
77         rt.block_on(async {
78             let _ = timeout(Duration::from_millis(5), future::pending::<()>());
79         });
80     });
81 
82     // The panic location should be in this file
83     assert_eq!(&panic_location_file.unwrap(), file!());
84 
85     Ok(())
86 }
87 
current_thread() -> Runtime88 fn current_thread() -> Runtime {
89     tokio::runtime::Builder::new_current_thread()
90         .enable_all()
91         .build()
92         .unwrap()
93 }
94