Skip to content

Commit

Permalink
Test exhausting server pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Sep 14, 2023
1 parent 3099e94 commit 9faa825
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/server_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::{Deref, DerefMut, Drop};
use std::sync::Mutex;
use tokio::sync::{Semaphore, SemaphorePermit};

// macOS has small default ulimits
// macOS has small default ulimits. Sync it with test_server_pool()
const DEFAULT_POOL_SIZE: usize = if cfg!(target_os = "macos") { 20 } else { 50 };
pub(crate) static SERVER_POOL: ServerPool = ServerPool::new(DEFAULT_POOL_SIZE);

Expand Down
33 changes: 23 additions & 10 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1915,35 +1915,45 @@ fn test_running_multiple_servers() {
assert_eq!("s3", body3);
}

static SERIAL_POOL_TESTS: Mutex<()> = 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();

// If the pool is not working, this will hit the file descriptor limit (Too many open files)
for _ in 0..20 {
// The pool size is 50, anything beyond that will block
for _ in 0..50 {
let mut servers = vec![];
let mut servers = vec![];
// Anything beyond pool size will block.
for _ in 0..DEFAULT_POOL_SIZE {
servers.push(Server::new());

let s = servers.first_mut().unwrap();
let s = servers.last_mut().unwrap();
let m = s.mock("GET", "/pool").create();
let (_, _, _) = request_with_body(&s.host_with_port(), "GET /pool", "", "");
m.assert();
}
}
}

#[tokio::test]
#[tokio::test(flavor = "multi_thread")]
#[allow(clippy::vec_init_then_push)]
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());

// If the pool is not working, this will hit the file descriptor limit (Too many open files)
for _ in 0..20 {
// The pool size is 50, anything beyond that will block
for _ in 0..50 {
let mut servers = vec![];
let mut servers = vec![];
// Anything beyond pool size will block
for _ in 0..DEFAULT_POOL_SIZE {
servers.push(Server::new_async().await);

let s = servers.first_mut().unwrap();
let s = servers.last_mut().unwrap();
let m = s.mock("GET", "/pool").create_async().await;
let (_, _, _) = request_with_body(&s.host_with_port(), "GET /pool", "", "");
m.assert_async().await;
Expand Down Expand Up @@ -2052,8 +2062,11 @@ async fn test_match_body_asnyc() {
assert_eq!(200, response.status());
}

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

let futures = (0..10).map(|_| async {
let mut s = Server::new_async().await;
let m = s.mock("POST", "/").create_async().await;
Expand Down

0 comments on commit 9faa825

Please sign in to comment.