Skip to content

Commit

Permalink
update integration test
Browse files Browse the repository at this point in the history
Signed-off-by: TXXT <[email protected]>
  • Loading branch information
hunterlxt committed May 26, 2020
1 parent 033a215 commit ebb5745
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
44 changes: 22 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,9 @@ pub fn list() -> Vec<(String, String)> {
#[doc(hidden)]
pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
let id = thread::current().id();
let group = REGISTRY_GROUP.read().unwrap();

let p = {
let group = REGISTRY_GROUP.read().unwrap();
let registry = group
.get(&id)
.unwrap_or(&REGISTRY_GLOBAL.registry)
Expand Down Expand Up @@ -761,13 +761,13 @@ pub fn eval<R, F: FnOnce(Option<String>) -> R>(name: &str, f: F) -> Option<R> {
/// A call to `cfg` with a particular fail point name overwrites any existing actions for
/// that fail point, including those set via the `FAILPOINTS` environment variable.
pub fn cfg<S: Into<String>>(name: S, actions: &str) -> Result<(), String> {
let id = thread::current().id();
let group = REGISTRY_GROUP.read().unwrap();
let mut registry = group
.get(&id)
.unwrap_or(&REGISTRY_GLOBAL.registry)
.write()
.unwrap();
let registry = {
let group = REGISTRY_GROUP.read().unwrap();
let id = thread::current().id();
group.get(&id).unwrap_or(&REGISTRY_GLOBAL.registry).clone()
};

let mut registry = registry.write().unwrap();

set(&mut registry, name.into(), actions)
}
Expand All @@ -781,13 +781,13 @@ where
S: Into<String>,
F: Fn() + Send + Sync + 'static,
{
let id = thread::current().id();
let group = REGISTRY_GROUP.read().unwrap();
let mut registry = group
.get(&id)
.unwrap_or(&REGISTRY_GLOBAL.registry)
.write()
.unwrap();
let registry = {
let group = REGISTRY_GROUP.read().unwrap();
let id = thread::current().id();
group.get(&id).unwrap_or(&REGISTRY_GLOBAL.registry).clone()
};

let mut registry = registry.write().unwrap();

let p = registry
.entry(name.into())
Expand All @@ -803,13 +803,13 @@ where
/// If the local registry doesn't exist, it will try to delete the corresponding
/// action in the global registry.
pub fn remove<S: AsRef<str>>(name: S) {
let id = thread::current().id();
let group = REGISTRY_GROUP.read().unwrap();
let mut registry = group
.get(&id)
.unwrap_or(&REGISTRY_GLOBAL.registry)
.write()
.unwrap();
let registry = {
let group = REGISTRY_GROUP.read().unwrap();
let id = thread::current().id();
group.get(&id).unwrap_or(&REGISTRY_GLOBAL.registry).clone()
};

let mut registry = registry.write().unwrap();

if let Some(p) = registry.remove(name.as_ref()) {
// wake up all pause failpoint.
Expand Down
16 changes: 5 additions & 11 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,26 @@ fn test_pause() {

fail::cfg("pause", "pause").unwrap();
let (tx, rx) = mpsc::channel();
// control `f()` is executed before next failpoint config
let (tx_before, rx_before) = mpsc::channel();
let thread_registry = local_registry.clone();
thread::spawn(move || {
thread_registry.register_current();
// pause
tx_before.send(()).unwrap();
tx.send(f()).unwrap();
// woken up by new order pause, and then pause again.
tx_before.send(()).unwrap();
tx.send(f()).unwrap();
// woken up by remove, and then quit immediately.
tx.send(f()).unwrap();
});

rx_before.recv().unwrap();
assert!(rx.recv_timeout(Duration::from_millis(2000)).is_err());
assert!(rx.recv_timeout(Duration::from_millis(100)).is_err());
fail::cfg("pause", "pause").unwrap();
rx.recv_timeout(Duration::from_millis(500)).unwrap();
rx.recv_timeout(Duration::from_millis(100)).unwrap();

rx_before.recv().unwrap();
assert!(rx.recv_timeout(Duration::from_millis(2000)).is_err());
assert!(rx.recv_timeout(Duration::from_millis(100)).is_err());
fail::remove("pause");

rx.recv_timeout(Duration::from_millis(500)).unwrap();
rx.recv_timeout(Duration::from_millis(500)).unwrap();
rx.recv_timeout(Duration::from_millis(100)).unwrap();
rx.recv_timeout(Duration::from_millis(100)).unwrap();
}

#[test]
Expand Down

0 comments on commit ebb5745

Please sign in to comment.