From 8ba7b023583f741216ace47e86433af994fe0052 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Tue, 28 Sep 2021 23:43:29 +0800 Subject: [PATCH 1/5] support async return Signed-off-by: Ruihang Xia --- Cargo.toml | 6 ++++++ src/lib.rs | 5 +++++ tests/tests.rs | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index fa097a6..b6c15f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,12 @@ log = { version = "0.4", features = ["std"] } lazy_static = "1.2" rand = "0.8" +[dev-dependencies] +tokio = { version = "1.12.0", default-features = false, features = [ + "rt", + "macros", +] } + [features] failpoints = [] diff --git a/src/lib.rs b/src/lib.rs index 7f2bac1..069dfbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -797,6 +797,11 @@ macro_rules! fail_point { panic!("Return is not supported for the fail point \"{}\"", $name); }); }}; + ($name:expr, | $arg:ident $(: $t:ty )? | async $block:tt ) => {{ + if let Some(res) = $crate::eval($name, {| $arg $(: $t)?| async $block}) { + return res.await; + } + }}; ($name:expr, $e:expr) => {{ if let Some(res) = $crate::eval($name, $e) { return res; diff --git a/tests/tests.rs b/tests/tests.rs index fecb53c..f14d2ba 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -36,6 +36,24 @@ fn test_return() { assert_eq!(f(), 2); } +#[tokio::test] +#[cfg_attr(not(feature = "failpoints"), ignore)] +async fn test_async_return() { + async fn async_fn() -> usize { + fail_point!("async_return", |s: Option| async { + (async {}).await; + s.map_or(2, |s| s.parse().unwrap()) + }); + 0 + } + + fail::cfg("async_return", "return(1000)").unwrap(); + assert_eq!(async_fn().await, 1000); + + fail::cfg("async_return", "return").unwrap(); + assert_eq!(async_fn().await, 2); +} + #[test] #[cfg_attr(not(feature = "failpoints"), ignore)] fn test_sleep() { From d139425f4b88c8a706400ff8ecd5d5a10a9bff56 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Sat, 9 Oct 2021 01:22:40 +0800 Subject: [PATCH 2/5] match async move block Signed-off-by: Ruihang Xia --- src/lib.rs | 11 +++++++++-- tests/tests.rs | 20 +++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 069dfbf..130fc62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -797,8 +797,15 @@ macro_rules! fail_point { panic!("Return is not supported for the fail point \"{}\"", $name); }); }}; - ($name:expr, | $arg:ident $(: $t:ty )? | async $block:tt ) => {{ - if let Some(res) = $crate::eval($name, {| $arg $(: $t)?| async $block}) { + ($name:expr, $($mov:ident)? | $arg:ident $(: $t:ty )? | async $block:tt ) => {{ + if let Some(res) = $crate::eval($name, { $($mov)? | $arg $(: $t)?| async $block}) { + (); + return res.await; + } + }}; + ($name:expr, $($mov:ident)? | $arg:ident $(: $t:ty )? | async move $block:tt ) => {{ + if let Some(res) = $crate::eval($name, { $($mov)? | $arg $(: $t)?| async move $block}) { + (); return res.await; } }}; diff --git a/tests/tests.rs b/tests/tests.rs index f14d2ba..e5568c1 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -40,7 +40,25 @@ fn test_return() { #[cfg_attr(not(feature = "failpoints"), ignore)] async fn test_async_return() { async fn async_fn() -> usize { - fail_point!("async_return", |s: Option| async { + fail_point!("async_return", move |s: Option| async { + (async {}).await; + s.map_or(2, |s| s.parse().unwrap()) + }); + 0 + } + + fail::cfg("async_return", "return(1000)").unwrap(); + assert_eq!(async_fn().await, 1000); + + fail::cfg("async_return", "return").unwrap(); + assert_eq!(async_fn().await, 2); +} + +#[tokio::test] +#[cfg_attr(not(feature = "failpoints"), ignore)] +async fn test_async_move_return() { + async fn async_fn() -> usize { + fail_point!("async_return", |s: Option| async move { (async {}).await; s.map_or(2, |s| s.parse().unwrap()) }); From 103abeb2b44af9ab297689ff555cc25dc189e9ed Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Wed, 25 Oct 2023 11:39:47 +0800 Subject: [PATCH 3/5] replace tokio with futures-executor Signed-off-by: Ruihang Xia --- Cargo.toml | 5 +---- tests/tests.rs | 28 ++++++++++++++++------------ 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b6c15f8..17457a8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,10 +19,7 @@ lazy_static = "1.2" rand = "0.8" [dev-dependencies] -tokio = { version = "1.12.0", default-features = false, features = [ - "rt", - "macros", -] } +futures-executor = "0.3" [features] failpoints = [] diff --git a/tests/tests.rs b/tests/tests.rs index e5568c1..10878c1 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -36,9 +36,9 @@ fn test_return() { assert_eq!(f(), 2); } -#[tokio::test] #[cfg_attr(not(feature = "failpoints"), ignore)] -async fn test_async_return() { +#[test] +fn test_async_return() { async fn async_fn() -> usize { fail_point!("async_return", move |s: Option| async { (async {}).await; @@ -47,16 +47,18 @@ async fn test_async_return() { 0 } - fail::cfg("async_return", "return(1000)").unwrap(); - assert_eq!(async_fn().await, 1000); + futures_executor::block_on(async move { + fail::cfg("async_return", "return(1000)").unwrap(); + assert_eq!(async_fn().await, 1000); - fail::cfg("async_return", "return").unwrap(); - assert_eq!(async_fn().await, 2); + fail::cfg("async_return", "return").unwrap(); + assert_eq!(async_fn().await, 2); + }) } -#[tokio::test] #[cfg_attr(not(feature = "failpoints"), ignore)] -async fn test_async_move_return() { +#[test] +fn test_async_move_return() { async fn async_fn() -> usize { fail_point!("async_return", |s: Option| async move { (async {}).await; @@ -65,11 +67,13 @@ async fn test_async_move_return() { 0 } - fail::cfg("async_return", "return(1000)").unwrap(); - assert_eq!(async_fn().await, 1000); + futures_executor::block_on(async move { + fail::cfg("async_return", "return(1000)").unwrap(); + assert_eq!(async_fn().await, 1000); - fail::cfg("async_return", "return").unwrap(); - assert_eq!(async_fn().await, 2); + fail::cfg("async_return", "return").unwrap(); + assert_eq!(async_fn().await, 2); + }) } #[test] From a6301f1a995b7a21e910880a56a2485bb372be73 Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Wed, 25 Oct 2023 11:50:41 +0800 Subject: [PATCH 4/5] define another macro for async fail point Signed-off-by: Ruihang Xia --- src/lib.rs | 29 +++++++++++++++++++---------- tests/tests.rs | 6 +++--- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 130fc62..27399e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -797,26 +797,35 @@ macro_rules! fail_point { panic!("Return is not supported for the fail point \"{}\"", $name); }); }}; - ($name:expr, $($mov:ident)? | $arg:ident $(: $t:ty )? | async $block:tt ) => {{ - if let Some(res) = $crate::eval($name, { $($mov)? | $arg $(: $t)?| async $block}) { - (); - return res.await; + ($name:expr, $e:expr) => {{ + if let Some(res) = $crate::eval($name, $e) { + return res; } }}; - ($name:expr, $($mov:ident)? | $arg:ident $(: $t:ty )? | async move $block:tt ) => {{ - if let Some(res) = $crate::eval($name, { $($mov)? | $arg $(: $t)?| async move $block}) { - (); - return res.await; + ($name:expr, $cond:expr, $e:expr) => {{ + if $cond { + fail_point!($name, $e); } }}; +} + +/// Like [fail_point], but accept a future. +#[macro_export] +#[cfg(feature = "failpoints")] +macro_rules! async_fail_point { + ($name:expr) => {{ + $crate::eval($name, |_| { + panic!("Return is not supported for the fail point \"{}\"", $name); + }); + }}; ($name:expr, $e:expr) => {{ if let Some(res) = $crate::eval($name, $e) { - return res; + return res.await; } }}; ($name:expr, $cond:expr, $e:expr) => {{ if $cond { - fail_point!($name, $e); + async_fail_point!($name, $e); } }}; } diff --git a/tests/tests.rs b/tests/tests.rs index 10878c1..d1d2b95 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -5,7 +5,7 @@ use std::sync::*; use std::time::*; use std::*; -use fail::fail_point; +use fail::{async_fail_point, fail_point}; #[test] fn test_off() { @@ -40,7 +40,7 @@ fn test_return() { #[test] fn test_async_return() { async fn async_fn() -> usize { - fail_point!("async_return", move |s: Option| async { + async_fail_point!("async_return", move |s: Option| async { (async {}).await; s.map_or(2, |s| s.parse().unwrap()) }); @@ -60,7 +60,7 @@ fn test_async_return() { #[test] fn test_async_move_return() { async fn async_fn() -> usize { - fail_point!("async_return", |s: Option| async move { + async_fail_point!("async_return", |s: Option| async move { (async {}).await; s.map_or(2, |s| s.parse().unwrap()) }); From 365aa98e31266cfb32698a883a84e2e6d19c102d Mon Sep 17 00:00:00 2001 From: Ruihang Xia Date: Wed, 25 Oct 2023 16:16:42 +0800 Subject: [PATCH 5/5] fix compile Signed-off-by: Ruihang Xia --- src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 5c4cb81..c72184c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -873,6 +873,15 @@ macro_rules! fail_point { ($name:expr, $cond:expr, $e:expr) => {{}}; } +/// Define an async fail point (disabled, see `failpoints` feature). +#[macro_export] +#[cfg(not(feature = "failpoints"))] +macro_rules! async_fail_point { + ($name:expr, $e:expr) => {{}}; + ($name:expr) => {{}}; + ($name:expr, $cond:expr, $e:expr) => {{}}; +} + #[cfg(test)] mod tests { use super::*;