Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support async return #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = [
waynexia marked this conversation as resolved.
Show resolved Hide resolved
"rt",
"macros",
] }

[features]
failpoints = []

Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,18 @@ 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, $($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, $e:expr) => {{
if let Some(res) = $crate::eval($name, $e) {
return res;
Expand Down
36 changes: 36 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,42 @@ 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", move |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);
}

#[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<String>| async move {
(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