Skip to content

Commit

Permalink
Remove the test function name condition but disable creating mocks fr…
Browse files Browse the repository at this point in the history
…om within sub-threads
  • Loading branch information
lipanski committed Jun 27, 2018
1 parent 649eecf commit eb90a06
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,10 @@
//! Note how I **didn't use the same variable name** for both mocks (e.g. `let _m`), as it would have ended the
//! lifetime of the first mock with the second assignment.
//!
//! # Doctests
//! # Limitations
//!
//! Creating mocks from threads is currently not possible. Please use the main (test) thread for that.
//! See the note on threads at the end for more details.
//!
//! If you intend to use mocks in doctests, please check <https://github.com/lipanski/mockito/issues/36>.
//!
Expand Down Expand Up @@ -374,9 +377,6 @@
//! mutex lock acquired **whenever you create a mock**. Tests that don't create mocks will
//! still be run in parallel.
//!
//! Special precautions should be taken when creating mocks from within threads.
//!
//!

extern crate http_muncher;
extern crate rand;
Expand Down Expand Up @@ -404,7 +404,6 @@ use std::fmt;
use rand::{thread_rng, Rng};
use regex::Regex;
use std::sync::{Mutex, LockResult, MutexGuard};
use std::thread;
use std::cell::RefCell;

lazy_static! {
Expand Down Expand Up @@ -734,13 +733,8 @@ impl Mock {
pub fn create(self) -> Self {
server::try_start();

let current_thread = thread::current();
let current_thread_name = current_thread.name().unwrap_or("");
// Make sure we don't lock in sub-threads.
if current_thread_name.starts_with("test_") {
// Ensures Mockito tests are run sequentially.
LOCAL_TEST_MUTEX.with(|_| {});
}
// Ensures Mockito tests are run sequentially.
LOCAL_TEST_MUTEX.with(|_| {});

let state_mutex = server::STATE.clone();
let mut state = state_mutex.lock().unwrap();
Expand Down
3 changes: 3 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,9 @@ fn test_request_from_thread() {
}

#[test]
#[ignore]
// Can't work unless there's a way to apply LOCAL_TEST_MUTEX only to test threads and
// not to any of their sub-threads.
fn test_mock_from_inside_thread_does_not_lock_forever() {
let _mock_outside_thread = mock("GET", "/").with_body("outside").create();

Expand Down

0 comments on commit eb90a06

Please sign in to comment.