Skip to content

Commit aa7a1fa

Browse files
author
raju.gupta
committed
POOL-413 Rebase latest changes.
1 parent 59550a7 commit aa7a1fa

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

src/test/java/org/apache/commons/pool2/impl/TestGenericObjectPool.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
import java.util.concurrent.TimeUnit;
5252
import java.util.concurrent.atomic.AtomicBoolean;
5353
import java.util.concurrent.atomic.AtomicInteger;
54-
import java.util.function.Function;
5554

5655
import javax.management.MBeanServer;
5756
import javax.management.ObjectName;
@@ -1017,16 +1016,8 @@ void testReturnObjectConcurrentCallsRespectsMaxIdleLimit() throws Exception {
10171016
final int maxIdleLimit = 5;
10181017
final int numThreads = 100;
10191018
final CyclicBarrier barrier = new CyclicBarrier(numThreads);
1019+
final CountDownLatch doneLatch = new CountDownLatch(numThreads);
10201020

1021-
withConcurrentCallsRespectMaxIdle(maxIdleLimit, numThreads, pool ->
1022-
getRunnables(numThreads, barrier, pool, (a, b) -> {
1023-
String pooledObject = a.borrowObject();
1024-
b.await(); // Wait for all threads to be ready
1025-
a.returnObject(pooledObject);
1026-
}));
1027-
}
1028-
1029-
void withConcurrentCallsRespectMaxIdle(int maxIdleLimit, int numThreads, Function<GenericObjectPool<String>, List<Runnable>> operation) throws Exception {
10301021
final GenericObjectPoolConfig<String> config = new GenericObjectPoolConfig<>();
10311022
config.setJmxEnabled(false);
10321023
try (GenericObjectPool<String> pool = new GenericObjectPool<>(new SimpleFactory(), config)) {
@@ -1035,15 +1026,31 @@ void withConcurrentCallsRespectMaxIdle(int maxIdleLimit, int numThreads, Functio
10351026
pool.setMinIdle(-1);
10361027
pool.setMaxTotal(-1);
10371028

1038-
final List<Runnable> tasks = operation.apply(pool);
1029+
final List<Runnable> tasks = new ArrayList<>();
1030+
1031+
for (int i = 0; i < numThreads; i++) {
1032+
tasks.add(() -> {
1033+
try {
1034+
String pooledObject = pool.borrowObject();
1035+
barrier.await(); // Wait for all threads to be ready
1036+
pool.returnObject(pooledObject);
1037+
} catch (Exception e) {
1038+
// do nothing
1039+
} finally {
1040+
doneLatch.countDown();
1041+
}
1042+
});
1043+
}
10391044

10401045
final ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
10411046
try {
10421047
tasks.forEach(executorService::submit);
1048+
doneLatch.await();
10431049
executorService.shutdown();
10441050
assertTrue(executorService.awaitTermination(60, TimeUnit.SECONDS),
10451051
"Executor did not terminate in time");
1046-
} finally {
1052+
}
1053+
finally {
10471054
executorService.shutdownNow(); // Ensure cleanup
10481055
}
10491056

@@ -1057,30 +1064,6 @@ void withConcurrentCallsRespectMaxIdle(int maxIdleLimit, int numThreads, Functio
10571064
}
10581065
}
10591066

1060-
@FunctionalInterface
1061-
public interface PoolOperation {
1062-
void execute(GenericObjectPool<String> pool, CyclicBarrier barrier) throws Exception;
1063-
}
1064-
1065-
private List<Runnable> getRunnables(final int numThreads,
1066-
final CyclicBarrier barrier,
1067-
final GenericObjectPool<String> pool,
1068-
final PoolOperation operation) {
1069-
List<Runnable> tasks = new ArrayList<>();
1070-
1071-
for (int i = 0; i < numThreads; i++) {
1072-
tasks.add(() -> {
1073-
try {
1074-
operation.execute(pool, barrier);
1075-
} catch (Exception e) {
1076-
// do nothing
1077-
}
1078-
});
1079-
}
1080-
return tasks;
1081-
}
1082-
1083-
10841067
@Test
10851068
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
10861069
void testAddObjectConcurrentCallsRespectsMaxIdle() throws Exception {

0 commit comments

Comments
 (0)