Skip to content

Commit

Permalink
Merge branch 'master' into async-return
Browse files Browse the repository at this point in the history
  • Loading branch information
waynexia authored Oct 25, 2023
2 parents a6301f1 + 5bc95a1 commit 3bf77c4
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# 0.5.1 - 2022-10-08

- Switch to 2021 edition and use once cell (#61)
- Support configuring fail point in RAII style (#62)
- Fix recursive macro invocation (#66)

# 0.5.0 - 2021-11-04

- update rand to 0.8

# 0.4.0 - 2020-04-13

- re-arrange macros to avoid leaking
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fail"
version = "0.4.0"
version = "0.5.1"
authors = ["The TiKV Project Developers"]
license = "Apache-2.0"
keywords = ["failpoints", "fail"]
Expand All @@ -10,12 +10,12 @@ homepage = "https://github.com/tikv/fail-rs"
documentation = "https://docs.rs/fail"
description = "Fail points for rust."
categories = ["development-tools::testing"]
edition = "2018"
edition = "2021"
exclude = ["/.github/*", "/.travis.yml", "/appveyor.yml"]

[dependencies]
log = { version = "0.4", features = ["std"] }
lazy_static = "1.2"
once_cell = "1.9.0"
rand = "0.8"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ First, add this to your `Cargo.toml`:

```toml
[dependencies]
fail = "0.4"
fail = "0.5"
```

Now you can import the `fail_point!` macro from the `fail` crate and use it to inject dynamic failures.
Expand Down
46 changes: 40 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//!
//! ```toml
//! [dependencies]
//! fail = "0.4"
//! fail = "0.5"
//! ```
//!
//! Now you can import the `fail_point!` macro from the `fail` crate and use it
Expand Down Expand Up @@ -523,10 +523,10 @@ struct FailPointRegistry {
registry: RwLock<Registry>,
}

lazy_static::lazy_static! {
static ref REGISTRY: FailPointRegistry = FailPointRegistry::default();
static ref SCENARIO: Mutex<&'static FailPointRegistry> = Mutex::new(&REGISTRY);
}
use once_cell::sync::Lazy;

static REGISTRY: Lazy<FailPointRegistry> = Lazy::new(FailPointRegistry::default);
static SCENARIO: Lazy<Mutex<&'static FailPointRegistry>> = Lazy::new(|| Mutex::new(&REGISTRY));

/// Test scenario with configured fail points.
#[derive(Debug)]
Expand Down Expand Up @@ -708,6 +708,40 @@ pub fn remove<S: AsRef<str>>(name: S) {
}
}

/// Configure fail point in RAII style.
#[derive(Debug)]
pub struct FailGuard(String);

impl Drop for FailGuard {
fn drop(&mut self) {
remove(&self.0);
}
}

impl FailGuard {
/// Configure the actions for a fail point during the lifetime of the returning `FailGuard`.
///
/// Read documentation of [`cfg`] for more details.
pub fn new<S: Into<String>>(name: S, actions: &str) -> Result<FailGuard, String> {
let name = name.into();
cfg(&name, actions)?;
Ok(FailGuard(name))
}

/// Configure the actions for a fail point during the lifetime of the returning `FailGuard`.
///
/// Read documentation of [`cfg_callback`] for more details.
pub fn with_callback<S, F>(name: S, f: F) -> Result<FailGuard, String>
where
S: Into<String>,
F: Fn() + Send + Sync + 'static,
{
let name = name.into();
cfg_callback(&name, f)?;
Ok(FailGuard(name))
}
}

fn set(
registry: &mut HashMap<String, Arc<FailPoint>>,
name: String,
Expand Down Expand Up @@ -804,7 +838,7 @@ macro_rules! fail_point {
}};
($name:expr, $cond:expr, $e:expr) => {{
if $cond {
fail_point!($name, $e);
$crate::fail_point!($name, $e);
}
}};
}
Expand Down
20 changes: 20 additions & 0 deletions tests/no_use_import.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2022 TiKV Project Authors. Licensed under Apache-2.0.

use std::*;

#[test]
#[cfg_attr(not(feature = "failpoints"), ignore)]
fn test_return() {
let f = || {
fail::fail_point!("return", |s: Option<String>| s
.map_or(2, |s| s.parse().unwrap()));
0
};
assert_eq!(f(), 0);

fail::cfg("return", "return(1000)").unwrap();
assert_eq!(f(), 1000);

fail::cfg("return", "return").unwrap();
assert_eq!(f(), 2);
}
9 changes: 6 additions & 3 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ fn test_pause() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
// pause
tx.send(f()).unwrap();
f();
tx.send(()).unwrap();
// woken up by new order pause, and then pause again.
tx.send(f()).unwrap();
f();
tx.send(()).unwrap();
// woken up by remove, and then quit immediately.
tx.send(f()).unwrap();
f();
tx.send(()).unwrap();
});

assert!(rx.recv_timeout(Duration::from_millis(500)).is_err());
Expand Down

0 comments on commit 3bf77c4

Please sign in to comment.