use parking_lot::{const_mutex, Mutex}; use std::error::Error; use std::panic; use std::sync::Arc; use tokio::runtime::Runtime; use tokio::sync::mpsc::channel; use tokio::time::{Duration, Instant}; use tokio_test::task; use tokio_util::io::SyncIoBridge; use tokio_util::sync::PollSender; use tokio_util::task::LocalPoolHandle; use tokio_util::time::DelayQueue;
// Taken from tokio-util::time::wheel, if that changes then const MAX_DURATION_MS: u64 = (1 << (36)) - 1;
{ let _guard = PANIC_MUTEX.lock(); let panic_file: Arc<Mutex<Option<String>>> = Arc::new(Mutex::new(None));
let prev_hook = panic::take_hook();
{ let panic_file = panic_file.clone();
panic::set_hook(Box::new(move |panic_info| { let panic_location = panic_info.location().unwrap();
panic_file
.lock()
.clone_from(&Some(panic_location.file().to_string()));
}));
}
let result = panic::catch_unwind(func); // Return to the previously set panic hook (maybe default) so that we get nice error // messages in the tests.
panic::set_hook(prev_hook);
// The panic location should be in this file
assert_eq!(&panic_location_file.unwrap(), file!());
Ok(())
}
#[test] fn future_ext_to_panic_caller() -> Result<(), Box<dyn Error>> { use tokio::{sync::oneshot, time::Duration}; use tokio_util::future::FutureExt;
let panic_location_file = test_panic(|| { let (_tx, rx) = oneshot::channel::<()>(); // this panics because there is no runtime available let _res = rx.timeout(Duration::from_millis(10));
});
// The panic location should be in this file
assert_eq!(&panic_location_file.unwrap(), file!());
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.