Skip to content

Commit

Permalink
support async return
Browse files Browse the repository at this point in the history
  • Loading branch information
waynexia committed Sep 28, 2021
1 parent 6cd91d5 commit 609d94c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>| 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() {
Expand Down

0 comments on commit 609d94c

Please sign in to comment.