|
| 1 | +#![warn(rust_2018_idioms)] |
| 2 | +#![cfg(feature = "full")] |
| 3 | + |
| 4 | +use futures::future; |
| 5 | +use parking_lot::{const_mutex, Mutex}; |
| 6 | +use std::error::Error; |
| 7 | +use std::panic; |
| 8 | +use std::sync::Arc; |
| 9 | +use std::time::Duration; |
| 10 | +use tokio::runtime::{Builder, Runtime}; |
| 11 | +use tokio::time::{self, interval, interval_at, timeout, Instant}; |
| 12 | + |
| 13 | +fn test_panic<Func: FnOnce() + panic::UnwindSafe>(func: Func) -> Option<String> { |
| 14 | + static PANIC_MUTEX: Mutex<()> = const_mutex(()); |
| 15 | + |
| 16 | + { |
| 17 | + let _guard = PANIC_MUTEX.lock(); |
| 18 | + let panic_file: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None)); |
| 19 | + |
| 20 | + let prev_hook = panic::take_hook(); |
| 21 | + { |
| 22 | + let panic_file = panic_file.clone(); |
| 23 | + panic::set_hook(Box::new(move |panic_info| { |
| 24 | + let panic_location = panic_info.location().unwrap(); |
| 25 | + panic_file |
| 26 | + .lock() |
| 27 | + .clone_from(&Some(panic_location.file().to_string())); |
| 28 | + })); |
| 29 | + } |
| 30 | + |
| 31 | + let result = panic::catch_unwind(func); |
| 32 | + // Return to the previously set panic hook (maybe default) so that we get nice error |
| 33 | + // messages in the tests. |
| 34 | + panic::set_hook(prev_hook); |
| 35 | + |
| 36 | + if result.is_err() { |
| 37 | + panic_file.lock().clone() |
| 38 | + } else { |
| 39 | + None |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn pause_panic_caller() -> Result<(), Box<dyn Error>> { |
| 46 | + let panic_location_file = test_panic(|| { |
| 47 | + let rt = basic(); |
| 48 | + |
| 49 | + rt.block_on(async { |
| 50 | + time::pause(); |
| 51 | + time::pause(); |
| 52 | + }); |
| 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] |
| 62 | +fn resume_panic_caller() -> Result<(), Box<dyn Error>> { |
| 63 | + let panic_location_file = test_panic(|| { |
| 64 | + let rt = basic(); |
| 65 | + |
| 66 | + rt.block_on(async { |
| 67 | + time::resume(); |
| 68 | + }); |
| 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 | +#[test] |
| 78 | +fn interval_panic_caller() -> Result<(), Box<dyn Error>> { |
| 79 | + let panic_location_file = test_panic(|| { |
| 80 | + let _ = interval(Duration::from_millis(0)); |
| 81 | + }); |
| 82 | + |
| 83 | + // The panic location should be in this file |
| 84 | + assert_eq!(&panic_location_file.unwrap(), file!()); |
| 85 | + |
| 86 | + Ok(()) |
| 87 | +} |
| 88 | + |
| 89 | +#[test] |
| 90 | +fn interval_at_panic_caller() -> Result<(), Box<dyn Error>> { |
| 91 | + let panic_location_file = test_panic(|| { |
| 92 | + let _ = interval_at(Instant::now(), Duration::from_millis(0)); |
| 93 | + }); |
| 94 | + |
| 95 | + // The panic location should be in this file |
| 96 | + assert_eq!(&panic_location_file.unwrap(), file!()); |
| 97 | + |
| 98 | + Ok(()) |
| 99 | +} |
| 100 | + |
| 101 | +#[test] |
| 102 | +fn timeout_panic_caller() -> Result<(), Box<dyn Error>> { |
| 103 | + // let rt = Builder::new_current_thread().build().unwrap(); |
| 104 | + // rt.block_on(async { |
| 105 | + // let _ = timeout(Duration::from_millis(5), future::pending::<()>()); |
| 106 | + // }); |
| 107 | + let panic_location_file = test_panic(|| { |
| 108 | + // Runtime without `enable_time` so it has no current timer set. |
| 109 | + let rt = Builder::new_current_thread().build().unwrap(); |
| 110 | + rt.block_on(async { |
| 111 | + let _ = timeout(Duration::from_millis(5), future::pending::<()>()); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + // The panic location should be in this file |
| 116 | + assert_eq!(&panic_location_file.unwrap(), file!()); |
| 117 | + |
| 118 | + Ok(()) |
| 119 | +} |
| 120 | + |
| 121 | + |
| 122 | +fn basic() -> Runtime { |
| 123 | + tokio::runtime::Builder::new_current_thread() |
| 124 | + .enable_all() |
| 125 | + .build() |
| 126 | + .unwrap() |
| 127 | +} |
0 commit comments