|
| 1 | +#![warn(rust_2018_idioms)] |
| 2 | +#![cfg(feature = "full")] |
| 3 | + |
| 4 | +use parking_lot::{const_mutex, Mutex}; |
| 5 | +use std::error::Error; |
| 6 | +use std::panic; |
| 7 | +use std::sync::Arc; |
| 8 | +use tokio::runtime::Runtime; |
| 9 | +use tokio::sync::mpsc::channel; |
| 10 | +use tokio::time::{Duration, Instant}; |
| 11 | +use tokio_test::task; |
| 12 | +use tokio_util::sync::PollSender; |
| 13 | +use tokio_util::time::DelayQueue; |
| 14 | + |
| 15 | +fn test_panic<Func: FnOnce() + panic::UnwindSafe>(func: Func) -> Option<String> { |
| 16 | + static PANIC_MUTEX: Mutex<()> = const_mutex(()); |
| 17 | + |
| 18 | + { |
| 19 | + let _guard = PANIC_MUTEX.lock(); |
| 20 | + let panic_file: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None)); |
| 21 | + |
| 22 | + let prev_hook = panic::take_hook(); |
| 23 | + { |
| 24 | + let panic_file = panic_file.clone(); |
| 25 | + panic::set_hook(Box::new(move |panic_info| { |
| 26 | + let panic_location = panic_info.location().unwrap(); |
| 27 | + panic_file |
| 28 | + .lock() |
| 29 | + .clone_from(&Some(panic_location.file().to_string())); |
| 30 | + })); |
| 31 | + } |
| 32 | + |
| 33 | + let result = panic::catch_unwind(func); |
| 34 | + // Return to the previously set panic hook (maybe default) so that we get nice error |
| 35 | + // messages in the tests. |
| 36 | + panic::set_hook(prev_hook); |
| 37 | + |
| 38 | + if result.is_err() { |
| 39 | + panic_file.lock().clone() |
| 40 | + } else { |
| 41 | + None |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[test] |
| 47 | +fn test_poll_sender_send_item_panic_caller() -> Result<(), Box<dyn Error>> { |
| 48 | + let panic_location_file = test_panic(|| { |
| 49 | + let (send, _) = channel::<u32>(3); |
| 50 | + let mut send = PollSender::new(send); |
| 51 | + |
| 52 | + let _ = send.send_item(42); |
| 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 test_delay_queue_insert_at_panic_caller() -> Result<(), Box<dyn Error>> { |
| 63 | + let panic_location_file = test_panic(|| { |
| 64 | + let rt = basic(); |
| 65 | + rt.block_on(async { |
| 66 | + let mut queue = task::spawn(DelayQueue::with_capacity(3)); |
| 67 | + |
| 68 | + let _k = queue.insert_at( |
| 69 | + "1", |
| 70 | + // 36,500 days in the future. |
| 71 | + Instant::now() + Duration::from_secs(31_536_000_u64 * 100), |
| 72 | + ); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + // The panic location should be in this file |
| 77 | + assert_eq!(&panic_location_file.unwrap(), file!()); |
| 78 | + |
| 79 | + Ok(()) |
| 80 | +} |
| 81 | + |
| 82 | +#[test] |
| 83 | +fn test_delay_queue_insert_panic_caller() -> Result<(), Box<dyn Error>> { |
| 84 | + let panic_location_file = test_panic(|| { |
| 85 | + let rt = basic(); |
| 86 | + rt.block_on(async { |
| 87 | + let mut queue = task::spawn(DelayQueue::with_capacity(3)); |
| 88 | + |
| 89 | + let _k = queue.insert( |
| 90 | + "1", |
| 91 | + // 36,500 days |
| 92 | + Duration::from_secs(31_536_000_u64 * 100), |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + // The panic location should be in this file |
| 98 | + assert_eq!(&panic_location_file.unwrap(), file!()); |
| 99 | + |
| 100 | + Ok(()) |
| 101 | +} |
| 102 | + |
| 103 | +#[test] |
| 104 | +fn test_delay_queue_remove_panic_caller() -> Result<(), Box<dyn Error>> { |
| 105 | + let panic_location_file = test_panic(|| { |
| 106 | + let rt = basic(); |
| 107 | + rt.block_on(async { |
| 108 | + let mut queue = task::spawn(DelayQueue::with_capacity(3)); |
| 109 | + |
| 110 | + let key = queue.insert_at("1", Instant::now()); |
| 111 | + queue.remove(&key); |
| 112 | + queue.remove(&key); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + // The panic location should be in this file |
| 117 | + assert_eq!(&panic_location_file.unwrap(), file!()); |
| 118 | + |
| 119 | + Ok(()) |
| 120 | +} |
| 121 | + |
| 122 | +#[test] |
| 123 | +fn test_delay_queue_reset_at_panic_caller() -> Result<(), Box<dyn Error>> { |
| 124 | + let panic_location_file = test_panic(|| { |
| 125 | + let rt = basic(); |
| 126 | + rt.block_on(async { |
| 127 | + let mut queue = task::spawn(DelayQueue::with_capacity(3)); |
| 128 | + |
| 129 | + let key = queue.insert_at("1", Instant::now()); |
| 130 | + queue.reset_at( |
| 131 | + &key, |
| 132 | + // 36,500 days in the future. |
| 133 | + Instant::now() + Duration::from_secs(31_536_000_u64 * 100), |
| 134 | + ); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + // The panic location should be in this file |
| 139 | + assert_eq!(&panic_location_file.unwrap(), file!()); |
| 140 | + |
| 141 | + Ok(()) |
| 142 | +} |
| 143 | + |
| 144 | +#[test] |
| 145 | +fn test_delay_queue_reset_panic_caller() -> Result<(), Box<dyn Error>> { |
| 146 | + let panic_location_file = test_panic(|| { |
| 147 | + let rt = basic(); |
| 148 | + rt.block_on(async { |
| 149 | + let mut queue = task::spawn(DelayQueue::with_capacity(3)); |
| 150 | + |
| 151 | + let key = queue.insert_at("1", Instant::now()); |
| 152 | + queue.reset( |
| 153 | + &key, |
| 154 | + // 36,500 days |
| 155 | + Duration::from_secs(31_536_000_u64 * 100), |
| 156 | + ); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + // The panic location should be in this file |
| 161 | + assert_eq!(&panic_location_file.unwrap(), file!()); |
| 162 | + |
| 163 | + Ok(()) |
| 164 | +} |
| 165 | + |
| 166 | +fn basic() -> Runtime { |
| 167 | + tokio::runtime::Builder::new_current_thread() |
| 168 | + .enable_all() |
| 169 | + .build() |
| 170 | + .unwrap() |
| 171 | +} |
0 commit comments