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

fail: check condition only when its failpoint is open #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
25 changes: 18 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,21 @@ pub fn list() -> Vec<(String, String)> {
}

#[doc(hidden)]
pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
pub fn eval<R, F: FnOnce(Option<String>) -> R>(
name: &str,
f: F,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use impl trait here too.

cond: impl FnOnce() -> bool,
BusyJay marked this conversation as resolved.
Show resolved Hide resolved
) -> Option<R> {
let p = {
let registry = REGISTRY.registry.read().unwrap();
match registry.get(name) {
None => return None,
Some(p) => p.clone(),
}
};
if !cond() {
return None;
}
p.eval(name).map(f)
}

Expand Down Expand Up @@ -727,18 +734,22 @@ fn set(
#[cfg(feature = "failpoints")]
macro_rules! fail_point {
($name:expr) => {{
$crate::eval($name, |_| {
panic!("Return is not supported for the fail point \"{}\"", $name);
});
$crate::eval(
$name,
|_| {
panic!("Return is not supported for the fail point \"{}\"", $name);
},
|| true,
);
}};
($name:expr, $e:expr) => {{
if let Some(res) = $crate::eval($name, $e) {
if let Some(res) = $crate::eval($name, $e, || true) {
return res;
}
}};
($name:expr, $cond:expr, $e:expr) => {{
if $cond {
fail_point!($name, $e);
if let Some(res) = $crate::eval($name, $e, || $cond) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it hard to share mutable states between $cond and $e.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If true then we better add a test case that captures the expected behavior.

@gengliqi can you try adding a test case that shares mutable references between the two expressions and seeing if they worked previously but don't work after this patch?

return res;
}
}};
}
Expand Down