utils::failpoint_sleep_millis_async: ability to run an async block before sleeping

This commit is contained in:
Christian Schwarz
2023-04-27 13:04:07 +02:00
parent 95b560db4e
commit bcf49d3aa1

View File

@@ -57,16 +57,20 @@ pub mod serde_regex;
/// use with fail::cfg("$name", "return(2000)")
#[macro_export]
macro_rules! failpoint_sleep_millis_async {
($name:literal) => {{
($name:literal) => {
$crate::failpoint_sleep_millis_async!($name, async {})
};
($name:literal, $pre_sleep:expr) => {{
let should_sleep: Option<std::time::Duration> = (|| {
fail::fail_point!($name, |v: Option<_>| {
let millis = v.unwrap().parse::<u64>().unwrap();
Some(Duration::from_millis(millis))
Some(::std::time::Duration::from_millis(millis))
});
None
})();
if let Some(d) = should_sleep {
tracing::info!("failpoint {:?}: sleeping for {:?}", $name, d);
$pre_sleep.await;
tokio::time::sleep(d).await;
tracing::info!("failpoint {:?}: sleep done", $name);
}