Skip to content

Commit 0421da8

Browse files
committed
ensure failpoints is enabled using fail::cfg
Signed-off-by: Xintao <[email protected]>
1 parent b2dd693 commit 0421da8

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/lib.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@
102102
//! fn test_fallible_work() {
103103
//! let local_registry = fail::FailPointRegistry::new();
104104
//! local_registry.register_current();
105-
//! fail::cfg("read-dir", "panic").unwrap();
105+
//! fail::cfg("read-dir", "panic");
106106
//!
107107
//! do_fallible_work();
108108
//!
@@ -120,12 +120,12 @@
120120
//! use fail::FailScenario;
121121
//!
122122
//! let _scenario = FailScenario::setup();
123-
//! fail::cfg("p1", "sleep(100)").unwrap();
123+
//! fail::cfg("p1", "sleep(100)");
124124
//! println!("Global registry: {:?}", fail::list());
125125
//! {
126126
//! let local_registry = fail::FailPointRegistry::new();
127127
//! local_registry.register_current();
128-
//! fail::cfg("p0", "pause").unwrap();
128+
//! fail::cfg("p0", "pause");
129129
//! println!("Local registry: {:?}", fail::list());
130130
//! local_registry.teardown();
131131
//! println!("Local registry: {:?}", fail::list());
@@ -571,7 +571,7 @@ impl FailPointRegistry {
571571
let id = thread::current().id();
572572
group
573573
.get(&id)
574-
.unwrap_or(REGISTRY_GLOBAL.registry.as_ref().unwrap())
574+
.unwrap_or_else(|| REGISTRY_GLOBAL.registry.as_ref().unwrap())
575575
.clone()
576576
};
577577
FailPointRegistry {
@@ -726,7 +726,7 @@ pub fn list() -> Vec<(String, String)> {
726726
let id = thread::current().id();
727727
group
728728
.get(&id)
729-
.unwrap_or(REGISTRY_GLOBAL.registry.as_ref().unwrap())
729+
.unwrap_or_else(|| REGISTRY_GLOBAL.registry.as_ref().unwrap())
730730
.clone()
731731
};
732732

@@ -746,7 +746,7 @@ pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
746746
let id = thread::current().id();
747747
group
748748
.get(&id)
749-
.unwrap_or(REGISTRY_GLOBAL.registry.as_ref().unwrap())
749+
.unwrap_or_else(|| REGISTRY_GLOBAL.registry.as_ref().unwrap())
750750
.clone()
751751
};
752752

@@ -791,18 +791,22 @@ pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
791791
/// A call to `cfg` with a particular fail point name overwrites any existing actions for
792792
/// that fail point, including those set via the `FAILPOINTS` environment variable.
793793
pub fn cfg<S: Into<String>>(name: S, actions: &str) -> Result<(), String> {
794-
let registry = {
795-
let group = REGISTRY_GROUP.read().unwrap();
796-
let id = thread::current().id();
797-
group
798-
.get(&id)
799-
.unwrap_or(REGISTRY_GLOBAL.registry.as_ref().unwrap())
800-
.clone()
801-
};
794+
if cfg!(feature = "failpoints") {
795+
let registry = {
796+
let group = REGISTRY_GROUP.read().unwrap();
797+
let id = thread::current().id();
798+
group
799+
.get(&id)
800+
.unwrap_or_else(|| REGISTRY_GLOBAL.registry.as_ref().unwrap())
801+
.clone()
802+
};
802803

803-
let mut registry = registry.write().unwrap();
804+
let mut registry = registry.write().unwrap();
804805

805-
set(&mut registry, name.into(), actions)
806+
set(&mut registry, name.into(), actions)
807+
} else {
808+
Err("failpoints is not enabled".to_owned())
809+
}
806810
}
807811

808812
/// Configure the actions for a fail point in current registry at runtime.
@@ -819,7 +823,7 @@ where
819823
let id = thread::current().id();
820824
group
821825
.get(&id)
822-
.unwrap_or(REGISTRY_GLOBAL.registry.as_ref().unwrap())
826+
.unwrap_or_else(|| REGISTRY_GLOBAL.registry.as_ref().unwrap())
823827
.clone()
824828
};
825829

@@ -844,7 +848,7 @@ pub fn remove<S: AsRef<str>>(name: S) {
844848
let id = thread::current().id();
845849
group
846850
.get(&id)
847-
.unwrap_or(REGISTRY_GLOBAL.registry.as_ref().unwrap())
851+
.unwrap_or_else(|| REGISTRY_GLOBAL.registry.as_ref().unwrap())
848852
.clone()
849853
};
850854

0 commit comments

Comments
 (0)