Skip to content

Commit 8ba7b02

Browse files
committed
support async return
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
1 parent 6cd91d5 commit 8ba7b02

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ log = { version = "0.4", features = ["std"] }
1818
lazy_static = "1.2"
1919
rand = "0.8"
2020

21+
[dev-dependencies]
22+
tokio = { version = "1.12.0", default-features = false, features = [
23+
"rt",
24+
"macros",
25+
] }
26+
2127
[features]
2228
failpoints = []
2329

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,11 @@ macro_rules! fail_point {
797797
panic!("Return is not supported for the fail point \"{}\"", $name);
798798
});
799799
}};
800+
($name:expr, | $arg:ident $(: $t:ty )? | async $block:tt ) => {{
801+
if let Some(res) = $crate::eval($name, {| $arg $(: $t)?| async $block}) {
802+
return res.await;
803+
}
804+
}};
800805
($name:expr, $e:expr) => {{
801806
if let Some(res) = $crate::eval($name, $e) {
802807
return res;

tests/tests.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ fn test_return() {
3636
assert_eq!(f(), 2);
3737
}
3838

39+
#[tokio::test]
40+
#[cfg_attr(not(feature = "failpoints"), ignore)]
41+
async fn test_async_return() {
42+
async fn async_fn() -> usize {
43+
fail_point!("async_return", |s: Option<String>| async {
44+
(async {}).await;
45+
s.map_or(2, |s| s.parse().unwrap())
46+
});
47+
0
48+
}
49+
50+
fail::cfg("async_return", "return(1000)").unwrap();
51+
assert_eq!(async_fn().await, 1000);
52+
53+
fail::cfg("async_return", "return").unwrap();
54+
assert_eq!(async_fn().await, 2);
55+
}
56+
3957
#[test]
4058
#[cfg_attr(not(feature = "failpoints"), ignore)]
4159
fn test_sleep() {

0 commit comments

Comments
 (0)