Skip to content

Commit

Permalink
Use async mutex for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtoth committed Nov 2, 2023
1 parent d9fbb69 commit 951c0b2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tokio = { version = "1.25", features = ["full"] }
env_logger = "0.8"
testing_logger = "0.1"
reqwest = "0.11"
once_cell = "1.18"

[features]
default = ["color"]
Expand Down
14 changes: 5 additions & 9 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extern crate serde_json;

use mockito::{Matcher, Server};
use once_cell::sync::Lazy;
use rand::distributions::Alphanumeric;
use rand::Rng;
use std::fmt::Display;
Expand Down Expand Up @@ -1931,14 +1932,13 @@ fn test_running_multiple_servers() {
assert_eq!("s3", body3);
}

static SERIAL_POOL_TESTS: Mutex<()> = Mutex::new(());
static SERIAL_POOL_TESTS: Lazy<tokio::sync::Mutex<()>> = Lazy::new(|| tokio::sync::Mutex::new(()));
const DEFAULT_POOL_SIZE: usize = if cfg!(target_os = "macos") { 20 } else { 50 };

#[test]
#[allow(clippy::vec_init_then_push)]
fn test_server_pool() {
// two tests can't monopolize the pool at the same time
let _lock = SERIAL_POOL_TESTS.lock().unwrap();
let _lock = SERIAL_POOL_TESTS.blocking_lock();

// If the pool is not working, this will hit the file descriptor limit (Too many open files)
for _ in 0..20 {
Expand All @@ -1956,11 +1956,9 @@ fn test_server_pool() {
}

#[tokio::test(flavor = "multi_thread")]
#[allow(clippy::vec_init_then_push, clippy::await_holding_lock)]
async fn test_server_pool_async() {
// two tests can't monopolize the pool at the same time
tokio::task::yield_now().await;
let _lock = tokio::task::block_in_place(|| SERIAL_POOL_TESTS.lock().unwrap());
let _lock = SERIAL_POOL_TESTS.lock().await;

// If the pool is not working, this will hit the file descriptor limit (Too many open files)
for _ in 0..20 {
Expand Down Expand Up @@ -2079,10 +2077,8 @@ async fn test_match_body_asnyc() {
}

#[tokio::test(flavor = "multi_thread")]
#[allow(clippy::await_holding_lock)]
async fn test_join_all_async() {
tokio::task::yield_now().await;
let _lock = tokio::task::block_in_place(|| SERIAL_POOL_TESTS.lock().unwrap());
let _lock = SERIAL_POOL_TESTS.lock().await;

let futures = (0..10).map(|_| async {
let mut s = Server::new_async().await;
Expand Down

0 comments on commit 951c0b2

Please sign in to comment.