Skip to content

Commit f8a03c0

Browse files
author
Raju Kumar Gupta
committed
[POOL-419] Add failing tests for POOL-419.
1 parent a2c5809 commit f8a03c0

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

src/test/java/org/apache/commons/pool3/TestPoolUtils.java

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.jupiter.api.Assertions.assertEquals;
2222
import static org.junit.jupiter.api.Assertions.assertNotNull;
2323
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
2425
import static org.junit.jupiter.api.Assertions.fail;
2526

2627
import java.lang.reflect.InvocationHandler;
@@ -33,10 +34,16 @@
3334
import java.util.List;
3435
import java.util.Map;
3536
import java.util.TimerTask;
37+
import java.util.concurrent.CountDownLatch;
38+
import java.util.concurrent.ExecutorService;
39+
import java.util.concurrent.Executors;
40+
import java.util.concurrent.FutureTask;
41+
import java.util.concurrent.TimeUnit;
3642

3743
import org.apache.commons.pool3.impl.DefaultPooledObject;
3844
import org.apache.commons.pool3.impl.GenericKeyedObjectPool;
3945
import org.apache.commons.pool3.impl.GenericObjectPool;
46+
import org.apache.commons.pool3.impl.GenericObjectPoolConfig;
4047
import org.junit.jupiter.api.Test;
4148
import org.opentest4j.AssertionFailedError;
4249

@@ -684,4 +691,60 @@ public void testTimerHolder() {
684691
assertNotNull(h);
685692
assertNotNull(PoolUtils.TimerHolder.MIN_IDLE_TIMER);
686693
}
694+
695+
/*
696+
* Test for POOL-419.
697+
* https://issues.apache.org/jira/browse/POOL-419
698+
*/
699+
@Test
700+
void testPool419() throws Exception{
701+
702+
ExecutorService executor = Executors.newFixedThreadPool(100);
703+
704+
final List<String> calledMethods = new ArrayList<>();
705+
706+
final GenericObjectPoolConfig<Object> config = new GenericObjectPoolConfig<>();
707+
708+
config.setMaxTotal(10000);
709+
config.setMaxIdle(10000);
710+
config.setMinIdle(10000);
711+
712+
@SuppressWarnings("unchecked")
713+
final PooledObjectFactory<Object, RuntimeException> pof = createProxy(PooledObjectFactory.class, calledMethods);
714+
try (final ObjectPool<Object, RuntimeException> connectionPool = new GenericObjectPool<>(pof, config)) {
715+
assertNotNull(connectionPool);
716+
717+
CountDownLatch startLatch = new CountDownLatch(1);
718+
719+
for (int i = 0; i < 10000; i++) {
720+
Object poolObject = connectionPool.borrowObject();
721+
722+
FutureTask<Boolean> invalidateObject = new FutureTask<>(() -> {
723+
startLatch.await();
724+
connectionPool.invalidateObject(poolObject);
725+
return true;
726+
});
727+
728+
FutureTask<Boolean> returnObject = new FutureTask<>(() -> {
729+
startLatch.await();
730+
connectionPool.returnObject(poolObject);
731+
return true;
732+
});
733+
734+
executor.submit(returnObject);
735+
executor.submit(invalidateObject);
736+
737+
}
738+
739+
startLatch.countDown(); // Start all tasks simultaneously
740+
741+
executor.shutdown();
742+
assertTrue(executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS));
743+
744+
assertEquals(0, connectionPool.getNumActive(), "getNumActive() must not return a negative value");
745+
746+
}
747+
}
748+
687749
}
750+

0 commit comments

Comments
 (0)