Skip to content

Commit 829626d

Browse files
committed
address comments
Signed-off-by: Xintao <[email protected]>
1 parent fe2791d commit 829626d

File tree

2 files changed

+68
-70
lines changed

2 files changed

+68
-70
lines changed

src/lib.rs

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,11 @@
109109
//! }
110110
//! ```
111111
//!
112-
//! It should be noted that the local registry will inherit the global registry when
113-
//! it is created, which means that the inherited part can be shared. When you remove
114-
//! a global fail point action from the local registry, it will affect all threads
115-
//! using this fail point.
112+
//! It should be noted that the local registry will will overwrite the global registry
113+
//! if you register the current thread here. This means that the current thread can only
114+
//! use the fail points configuration of the local registry after registration.
116115
//!
117-
//! Here's a example to show the inheritance process:
116+
//! Here's a example to show the process:
118117
//!
119118
//! ```rust
120119
//! fail::setup();
@@ -138,10 +137,10 @@
138137
//! FAILPOINTS=p0=return cargo run --features fail/failpoints
139138
//! Finished dev [unoptimized + debuginfo] target(s) in 0.01s
140139
//! Running `target/debug/failpointtest`
141-
//! Global registry: [("p1", "sleep(100)"), ("p0", "return")]
142-
//! Local registry: [("p1", "sleep(100)"), ("p0", "pause")]
140+
//! Global registry: [("p1", "sleep(100)")]
141+
//! Local registry: [("p0", "pause")]
143142
//! Local registry: []
144-
//! Global registry: [("p1", "sleep(100)"), ("p0", "return")]
143+
//! Global registry: [("p1", "sleep(100)")]
145144
//! ```
146145
//!
147146
//! In this example, program update global registry with environment variable first.
@@ -325,19 +324,6 @@ struct Action {
325324
count: Option<AtomicUsize>,
326325
}
327326

328-
impl Clone for Action {
329-
fn clone(&self) -> Self {
330-
Action {
331-
count: self
332-
.count
333-
.as_ref()
334-
.map(|c| AtomicUsize::new(c.load(Ordering::Relaxed))),
335-
task: self.task.clone(),
336-
freq: self.freq,
337-
}
338-
}
339-
}
340-
341327
impl PartialEq for Action {
342328
fn eq(&self, hs: &Action) -> bool {
343329
if self.task != hs.task || self.freq != hs.freq {
@@ -477,16 +463,6 @@ struct FailPoint {
477463
actions_str: RwLock<String>,
478464
}
479465

480-
impl Clone for FailPoint {
481-
fn clone(&self) -> Self {
482-
FailPoint {
483-
actions: RwLock::new(self.actions.read().unwrap().clone()),
484-
actions_str: RwLock::new(self.actions_str.read().unwrap().clone()),
485-
..Default::default()
486-
}
487-
}
488-
}
489-
490466
#[cfg_attr(feature = "cargo-clippy", allow(clippy::mutex_atomic))]
491467
impl FailPoint {
492468
fn new() -> FailPoint {
@@ -576,24 +552,25 @@ pub struct FailPointRegistry {
576552
/// Each thread should be bound to exact one registry. Threads bound to the
577553
/// same registry share the same failpoints configuration.
578554
pub fn new_fail_group() -> FailPointRegistry {
579-
let registry = REGISTRY_GLOBAL.registry.read().unwrap();
580-
let mut new_registry = Registry::new();
581-
for (name, failpoint) in registry.iter() {
582-
new_registry.insert(name.clone(), Arc::new(FailPoint::clone(failpoint)));
583-
}
584555
FailPointRegistry {
585-
registry: Arc::new(RwLock::new(new_registry)),
556+
registry: Arc::new(RwLock::new(Registry::new())),
586557
}
587558
}
588559

589560
impl FailPointRegistry {
590561
/// Register the current thread to this failpoints registry.
591-
pub fn register_current(&self) {
562+
pub fn register_current(&self) -> Result<(), String> {
592563
let id = thread::current().id();
593-
REGISTRY_GROUP
564+
let ret = REGISTRY_GROUP
594565
.write()
595566
.unwrap()
596567
.insert(id, self.registry.clone());
568+
569+
if ret.is_some() {
570+
Err("current thread has been registered with one registry".to_owned())
571+
} else {
572+
Ok(())
573+
}
597574
}
598575

599576
/// Deregister the current thread to this failpoints registry.
@@ -696,14 +673,13 @@ pub const fn has_failpoints() -> bool {
696673
///
697674
/// Return a vector of `(name, actions)` pairs.
698675
pub fn list() -> Vec<(String, String)> {
699-
let id = thread::current().id();
700-
let group = REGISTRY_GROUP.read().unwrap();
676+
let registry = {
677+
let group = REGISTRY_GROUP.read().unwrap();
678+
let id = thread::current().id();
679+
group.get(&id).unwrap_or(&REGISTRY_GLOBAL.registry).clone()
680+
};
701681

702-
let registry = group
703-
.get(&id)
704-
.unwrap_or(&REGISTRY_GLOBAL.registry)
705-
.read()
706-
.unwrap();
682+
let registry = registry.read().unwrap();
707683

708684
registry
709685
.iter()
@@ -713,15 +689,15 @@ pub fn list() -> Vec<(String, String)> {
713689

714690
#[doc(hidden)]
715691
pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
716-
let id = thread::current().id();
717-
718692
let p = {
719-
let group = REGISTRY_GROUP.read().unwrap();
720-
let registry = group
721-
.get(&id)
722-
.unwrap_or(&REGISTRY_GLOBAL.registry)
723-
.read()
724-
.unwrap();
693+
let registry = {
694+
let group = REGISTRY_GROUP.read().unwrap();
695+
let id = thread::current().id();
696+
group.get(&id).unwrap_or(&REGISTRY_GLOBAL.registry).clone()
697+
};
698+
699+
let registry = registry.read().unwrap();
700+
725701
match registry.get(name) {
726702
None => return None,
727703
Some(p) => p.clone(),
@@ -1125,6 +1101,28 @@ mod tests {
11251101
"setup_and_teardown1=return;setup_and_teardown2=pause;",
11261102
);
11271103
setup();
1104+
1105+
let group = new_fail_group();
1106+
let handler = thread::spawn(move || {
1107+
group.register_current().unwrap();
1108+
cfg("setup_and_teardown1", "panic").unwrap();
1109+
cfg("setup_and_teardown2", "panic").unwrap();
1110+
let l = list();
1111+
assert!(
1112+
l.iter()
1113+
.find(|&x| x == &("setup_and_teardown1".to_owned(), "panic".to_owned()))
1114+
.is_some()
1115+
&& l.iter()
1116+
.find(|&x| x == &("setup_and_teardown2".to_owned(), "panic".to_owned()))
1117+
.is_some()
1118+
&& l.len() == 2
1119+
);
1120+
remove("setup_and_teardown2");
1121+
let l = list();
1122+
assert!(l.len() == 1);
1123+
});
1124+
handler.join().unwrap();
1125+
11281126
assert_eq!(f1(), 1);
11291127

11301128
let (tx, rx) = mpsc::channel();

tests/tests.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use fail::fail_point;
1111
#[cfg_attr(not(feature = "failpoints"), ignore)]
1212
fn test_pause() {
1313
let local_registry = fail::new_fail_group();
14-
local_registry.register_current();
14+
local_registry.register_current().unwrap();
1515
let f = || {
1616
fail_point!("pause");
1717
};
@@ -21,7 +21,7 @@ fn test_pause() {
2121
let (tx, rx) = mpsc::channel();
2222
let thread_registry = local_registry.clone();
2323
thread::spawn(move || {
24-
thread_registry.register_current();
24+
thread_registry.register_current().unwrap();
2525
// pause
2626
tx.send(f()).unwrap();
2727
// woken up by new order pause, and then pause again.
@@ -44,7 +44,7 @@ fn test_pause() {
4444
#[test]
4545
fn test_off() {
4646
let local_registry = fail::new_fail_group();
47-
local_registry.register_current();
47+
local_registry.register_current().unwrap();
4848

4949
let f = || {
5050
fail_point!("off", |_| 2);
@@ -60,7 +60,7 @@ fn test_off() {
6060
#[cfg_attr(not(feature = "failpoints"), ignore)]
6161
fn test_return() {
6262
let local_registry = fail::new_fail_group();
63-
local_registry.register_current();
63+
local_registry.register_current().unwrap();
6464

6565
let f = || {
6666
fail_point!("return", |s: Option<String>| s
@@ -80,7 +80,7 @@ fn test_return() {
8080
#[cfg_attr(not(feature = "failpoints"), ignore)]
8181
fn test_sleep() {
8282
let local_registry = fail::new_fail_group();
83-
local_registry.register_current();
83+
local_registry.register_current().unwrap();
8484

8585
let f = || {
8686
fail_point!("sleep");
@@ -100,7 +100,7 @@ fn test_sleep() {
100100
#[cfg_attr(not(feature = "failpoints"), ignore)]
101101
fn test_panic() {
102102
let local_registry = fail::new_fail_group();
103-
local_registry.register_current();
103+
local_registry.register_current().unwrap();
104104

105105
let f = || {
106106
fail_point!("panic");
@@ -113,7 +113,7 @@ fn test_panic() {
113113
#[cfg_attr(not(feature = "failpoints"), ignore)]
114114
fn test_print() {
115115
let local_registry = fail::new_fail_group();
116-
local_registry.register_current();
116+
local_registry.register_current().unwrap();
117117

118118
struct LogCollector(Arc<Mutex<Vec<String>>>);
119119
impl log::Log for LogCollector {
@@ -149,7 +149,7 @@ fn test_print() {
149149
#[test]
150150
fn test_yield() {
151151
let local_registry = fail::new_fail_group();
152-
local_registry.register_current();
152+
local_registry.register_current().unwrap();
153153

154154
let f = || {
155155
fail_point!("yield");
@@ -162,7 +162,7 @@ fn test_yield() {
162162
#[cfg_attr(not(feature = "failpoints"), ignore)]
163163
fn test_callback() {
164164
let local_registry = fail::new_fail_group();
165-
local_registry.register_current();
165+
local_registry.register_current().unwrap();
166166

167167
let f1 = || {
168168
fail_point!("cb");
@@ -186,7 +186,7 @@ fn test_callback() {
186186
#[cfg_attr(not(feature = "failpoints"), ignore)]
187187
fn test_delay() {
188188
let local_registry = fail::new_fail_group();
189-
local_registry.register_current();
189+
local_registry.register_current().unwrap();
190190

191191
let f = || fail_point!("delay");
192192
let timer = Instant::now();
@@ -199,7 +199,7 @@ fn test_delay() {
199199
#[cfg_attr(not(feature = "failpoints"), ignore)]
200200
fn test_freq_and_count() {
201201
let local_registry = fail::new_fail_group();
202-
local_registry.register_current();
202+
local_registry.register_current().unwrap();
203203

204204
let f = || {
205205
fail_point!("freq_and_count", |s: Option<String>| s
@@ -223,7 +223,7 @@ fn test_freq_and_count() {
223223
#[cfg_attr(not(feature = "failpoints"), ignore)]
224224
fn test_condition() {
225225
let local_registry = fail::new_fail_group();
226-
local_registry.register_current();
226+
local_registry.register_current().unwrap();
227227

228228
let f = |_enabled| {
229229
fail_point!("condition", _enabled, |_| 2);
@@ -240,7 +240,7 @@ fn test_condition() {
240240
#[test]
241241
fn test_list() {
242242
let local_registry = fail::new_fail_group();
243-
local_registry.register_current();
243+
local_registry.register_current().unwrap();
244244

245245
assert!(!fail::list().contains(&("list".to_string(), "off".to_string())));
246246
fail::cfg("list", "off").unwrap();
@@ -252,11 +252,11 @@ fn test_list() {
252252
#[test]
253253
fn test_multiple_threads_cleanup() {
254254
let local_registry = fail::new_fail_group();
255-
local_registry.register_current();
255+
local_registry.register_current().unwrap();
256256

257257
let (tx, rx) = mpsc::channel();
258258
thread::spawn(move || {
259-
local_registry.register_current();
259+
local_registry.register_current().unwrap();
260260
fail::cfg("thread_point", "sleep(10)").unwrap();
261261
tx.send(()).unwrap();
262262
});
@@ -272,7 +272,7 @@ fn test_multiple_threads_cleanup() {
272272
let (tx, rx) = mpsc::channel();
273273
let t = thread::spawn(move || {
274274
let local_registry = fail::new_fail_group();
275-
local_registry.register_current();
275+
local_registry.register_current().unwrap();
276276
fail::cfg("thread_point", "panic").unwrap();
277277
let l = fail::list();
278278
assert!(

0 commit comments

Comments
 (0)