You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2021/12/01 01:43:02 UTC

[commons-pool] branch master updated: [POOL-402] Check blockWhenExhausted in hasBorrowWaiters (#116)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-pool.git


The following commit(s) were added to refs/heads/master by this push:
     new 2286800  [POOL-402] Check blockWhenExhausted in hasBorrowWaiters (#116)
2286800 is described below

commit 22868002d800cd19600e06c9d2c599fe0e04a453
Author: Cp-John <60...@users.noreply.github.com>
AuthorDate: Wed Dec 1 09:42:53 2021 +0800

    [POOL-402] Check blockWhenExhausted in hasBorrowWaiters (#116)
    
    * Check blockWhenExhausted in hasBorrowWaiters
    
    * Add jacoco plugin
    
    * Add unite tests for getBlockWhenExhausted in hasBorrowWaiters
    
    * Restore pom file
---
 .../commons/pool2/impl/GenericKeyedObjectPool.java |  2 +-
 .../pool2/impl/TestGenericKeyedObjectPool.java     | 29 ++++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
index b41d8c4..0ec43f0 100644
--- a/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
+++ b/src/main/java/org/apache/commons/pool2/impl/GenericKeyedObjectPool.java
@@ -1280,7 +1280,7 @@ public class GenericKeyedObjectPool<K, T> extends BaseGenericObjectPool<T>
      *         {@code false}
      */
     private boolean hasBorrowWaiters() {
-        return poolMap.values().stream().anyMatch(deque -> deque != null && deque.getIdleObjects().hasTakeWaiters());
+        return getBlockWhenExhausted() && poolMap.values().stream().anyMatch(deque -> deque != null && deque.getIdleObjects().hasTakeWaiters());
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
index ca26e50..1972989 100644
--- a/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
+++ b/src/test/java/org/apache/commons/pool2/impl/TestGenericKeyedObjectPool.java
@@ -2608,6 +2608,35 @@ public class TestGenericKeyedObjectPool extends TestKeyedObjectPool {
         assertFalse(borrower.isAlive());
     }
 
+    @Test
+    public void testReturnObjectWithBlockWhenExhausted() throws Exception {
+        gkoPool.setBlockWhenExhausted(true);
+        gkoPool.setMaxTotal(1);
+
+        // Test return object with no take waiters
+        String obj = gkoPool.borrowObject("0");
+        gkoPool.returnObject("0", obj);
+
+        // Test return object with a take waiter
+        final TestThread<String> testA = new TestThread<>(gkoPool, 1, 0, 500, false, null, "0");
+        final TestThread<String> testB = new TestThread<>(gkoPool, 1, 0, 0, false, null, "1");
+        final Thread threadA = new Thread(testA);
+        final Thread threadB = new Thread(testB);
+        threadA.start();
+        threadB.start();
+        threadA.join();
+        threadB.join();
+    }
+
+    @Test
+    public void testReturnObjectWithoutBlockWhenExhausted() throws Exception {
+        gkoPool.setBlockWhenExhausted(false);
+
+        // Test return object with no take waiters
+        String obj = gkoPool.borrowObject("0");
+        gkoPool.returnObject("0", obj);
+    }
+
 }