From bc0c22259622647e83017e4183f9fcc3e3db833a Mon Sep 17 00:00:00 2001 From: Marten Wijnja Date: Sun, 2 Feb 2025 14:28:06 +0100 Subject: [PATCH] Move all resizing-based tests to the same test module --- tests/managed.rs | 97 ----------------------------------------- tests/managed_resize.rs | 97 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 97 deletions(-) diff --git a/tests/managed.rs b/tests/managed.rs index 0a538c4..5e28cd8 100644 --- a/tests/managed.rs +++ b/tests/managed.rs @@ -108,16 +108,6 @@ async fn closing() { assert_eq!(pool.status().waiting, 0); } -#[tokio::test] -async fn close_resize() { - let mgr = Manager {}; - let pool = Pool::builder(mgr).max_size(1).build().unwrap(); - pool.close(); - pool.resize(16); - assert_eq!(pool.status().size, 0); - assert_eq!(pool.status().max_size, 0); -} - #[tokio::test(flavor = "multi_thread")] async fn concurrent() { let mgr = Manager {}; @@ -192,93 +182,6 @@ async fn object_take() { assert_eq!(status.available, 2); assert_eq!(status.waiting, 0); } - -#[tokio::test] -async fn resize_pool_shrink() { - let mgr = Manager {}; - let pool = Pool::builder(mgr).max_size(2).build().unwrap(); - let obj0 = pool.get().await.unwrap(); - let obj1 = pool.get().await.unwrap(); - pool.resize(1); - assert_eq!(pool.status().max_size, 1); - assert_eq!(pool.status().size, 2); - drop(obj1); - assert_eq!(pool.status().max_size, 1); - assert_eq!(pool.status().size, 1); - drop(obj0); - assert_eq!(pool.status().max_size, 1); - assert_eq!(pool.status().size, 1); -} - -#[tokio::test] -async fn resize_pool_grow() { - let mgr = Manager {}; - let pool = Pool::builder(mgr).max_size(1).build().unwrap(); - let obj0 = pool.get().await.unwrap(); - pool.resize(2); - assert_eq!(pool.status().max_size, 2); - assert_eq!(pool.status().size, 1); - let obj1 = pool.get().await.unwrap(); - assert_eq!(pool.status().max_size, 2); - assert_eq!(pool.status().size, 2); - drop(obj1); - assert_eq!(pool.status().max_size, 2); - assert_eq!(pool.status().size, 2); - drop(obj0); - assert_eq!(pool.status().max_size, 2); - assert_eq!(pool.status().size, 2); -} - -#[tokio::test] -async fn resize_pool_shrink_grow() { - let mgr = Manager {}; - let pool = Pool::builder(mgr).max_size(1).build().unwrap(); - let obj0 = pool.get().await.unwrap(); - pool.resize(2); - pool.resize(0); - pool.resize(5); - assert_eq!(pool.status().max_size, 5); - assert_eq!(pool.status().size, 1); - drop(obj0); - assert_eq!(pool.status().max_size, 5); - assert_eq!(pool.status().size, 1); -} - -#[tokio::test] -async fn resize_pool_grow_concurrent() { - let mgr = Manager {}; - let pool = Pool::builder(mgr).max_size(0).build().unwrap(); - let join_handle = { - let pool = pool.clone(); - tokio::spawn(async move { pool.get().await }) - }; - tokio::task::yield_now().await; - assert_eq!(pool.status().max_size, 0); - assert_eq!(pool.status().size, 0); - assert_eq!(pool.status().available, 0); - assert_eq!(pool.status().waiting, 1); - pool.resize(1); - assert_eq!(pool.status().max_size, 1); - assert_eq!(pool.status().size, 0); - assert_eq!(pool.status().available, 0); - assert_eq!(pool.status().waiting, 1); - tokio::task::yield_now().await; - assert_eq!(pool.status().max_size, 1); - assert_eq!(pool.status().size, 1); - assert_eq!(pool.status().available, 0); - assert_eq!(pool.status().waiting, 0); - let obj0 = join_handle.await.unwrap().unwrap(); - assert_eq!(pool.status().max_size, 1); - assert_eq!(pool.status().size, 1); - assert_eq!(pool.status().available, 0); - assert_eq!(pool.status().waiting, 0); - drop(obj0); - assert_eq!(pool.status().max_size, 1); - assert_eq!(pool.status().size, 1); - assert_eq!(pool.status().available, 1); - assert_eq!(pool.status().waiting, 0); -} - #[tokio::test] async fn retain() { let mgr = Manager {}; diff --git a/tests/managed_resize.rs b/tests/managed_resize.rs index 415b0f9..5c31a62 100644 --- a/tests/managed_resize.rs +++ b/tests/managed_resize.rs @@ -21,6 +21,7 @@ impl managed::Manager for Manager { } } +// Regression test for https://github.com/bikeshedder/deadpool/issues/380 #[tokio::test] async fn test_grow_reuse_existing() { // Shrink doesn't discard objects currently borrowed from the pool but @@ -49,3 +50,99 @@ async fn test_grow_reuse_existing() { assert!(pool.status().size == 1); assert!(pool.status().max_size == 1); } + +#[tokio::test] +async fn resize_pool_shrink() { + let mgr = Manager {}; + let pool = Pool::builder(mgr).max_size(2).build().unwrap(); + let obj0 = pool.get().await.unwrap(); + let obj1 = pool.get().await.unwrap(); + pool.resize(1); + assert_eq!(pool.status().max_size, 1); + assert_eq!(pool.status().size, 2); + drop(obj1); + assert_eq!(pool.status().max_size, 1); + assert_eq!(pool.status().size, 1); + drop(obj0); + assert_eq!(pool.status().max_size, 1); + assert_eq!(pool.status().size, 1); +} + +#[tokio::test] +async fn resize_pool_grow() { + let mgr = Manager {}; + let pool = Pool::builder(mgr).max_size(1).build().unwrap(); + let obj0 = pool.get().await.unwrap(); + pool.resize(2); + assert_eq!(pool.status().max_size, 2); + assert_eq!(pool.status().size, 1); + let obj1 = pool.get().await.unwrap(); + assert_eq!(pool.status().max_size, 2); + assert_eq!(pool.status().size, 2); + drop(obj1); + assert_eq!(pool.status().max_size, 2); + assert_eq!(pool.status().size, 2); + drop(obj0); + assert_eq!(pool.status().max_size, 2); + assert_eq!(pool.status().size, 2); +} + +#[tokio::test] +async fn resize_pool_shrink_grow() { + let mgr = Manager {}; + let pool = Pool::builder(mgr).max_size(1).build().unwrap(); + let obj0 = pool.get().await.unwrap(); + pool.resize(2); + pool.resize(0); + pool.resize(5); + assert_eq!(pool.status().max_size, 5); + assert_eq!(pool.status().size, 1); + drop(obj0); + assert_eq!(pool.status().max_size, 5); + assert_eq!(pool.status().size, 1); +} + +#[tokio::test] +async fn resize_pool_grow_concurrent() { + let mgr = Manager {}; + let pool = Pool::builder(mgr).max_size(0).build().unwrap(); + let join_handle = { + let pool = pool.clone(); + tokio::spawn(async move { pool.get().await }) + }; + tokio::task::yield_now().await; + assert_eq!(pool.status().max_size, 0); + assert_eq!(pool.status().size, 0); + assert_eq!(pool.status().available, 0); + assert_eq!(pool.status().waiting, 1); + pool.resize(1); + assert_eq!(pool.status().max_size, 1); + assert_eq!(pool.status().size, 0); + assert_eq!(pool.status().available, 0); + assert_eq!(pool.status().waiting, 1); + tokio::task::yield_now().await; + assert_eq!(pool.status().max_size, 1); + assert_eq!(pool.status().size, 1); + assert_eq!(pool.status().available, 0); + assert_eq!(pool.status().waiting, 0); + let obj0 = join_handle.await.unwrap().unwrap(); + assert_eq!(pool.status().max_size, 1); + assert_eq!(pool.status().size, 1); + assert_eq!(pool.status().available, 0); + assert_eq!(pool.status().waiting, 0); + drop(obj0); + assert_eq!(pool.status().max_size, 1); + assert_eq!(pool.status().size, 1); + assert_eq!(pool.status().available, 1); + assert_eq!(pool.status().waiting, 0); +} + +#[tokio::test] +async fn close_resize() { + let mgr = Manager {}; + let pool = Pool::builder(mgr).max_size(1).build().unwrap(); + pool.close(); + pool.resize(16); + assert_eq!(pool.status().size, 0); + assert_eq!(pool.status().max_size, 0); +}