Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[POOL-419] Add failing tests for POOL-419. #385

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/main/java/org/apache/commons/pool3/impl/GenericObjectPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -1091,11 +1091,19 @@ public void returnObject(final T obj) {
swallowException(e);
}
} else {
if (getLifo()) {
idleObjects.addFirst(p);
} else {
idleObjects.addLast(p);

synchronized (p) {
if(p.getState() == PooledObjectState.INVALID) {
throw new IllegalStateException("Object has already been returned to this pool or is invalid");
}

if (getLifo()) {
idleObjects.addFirst(p);
} else {
idleObjects.addLast(p);
}
}

if (isClosed()) {
// Pool closed while object was being added to idle objects.
// Make sure the returned object is destroyed rather than left
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -65,6 +69,7 @@
import org.apache.commons.pool3.WaiterFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.RepeatedTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

Expand Down Expand Up @@ -3150,4 +3155,84 @@ public void testWhenExhaustedFail() throws Exception {
genericObjectPool.close();
}

private BasePooledObjectFactory<Object, RuntimeException> createPooledObjectFactory() {
return new BasePooledObjectFactory<>() {
@Override
public Object create() {
return new Object();
}

@Override
public PooledObject<Object> wrap(final Object obj) {
return new DefaultPooledObject<>(obj);
}
};
}


/*
* Test for POOL-419.
* https://issues.apache.org/jira/browse/POOL-419
*/
@Test
@Timeout(value = 60000, unit = TimeUnit.MILLISECONDS)
void testPool419() throws Exception{

ExecutorService executor = Executors.newFixedThreadPool(100);

final GenericObjectPoolConfig<Object> config = new GenericObjectPoolConfig<>();

final int maxConnections = 100000;

config.setMaxTotal(maxConnections);
config.setMaxIdle(maxConnections);
config.setMinIdle(maxConnections);

final BasePooledObjectFactory<Object, RuntimeException> pof = createPooledObjectFactory();

try (final ObjectPool<Object, RuntimeException> connectionPool = new GenericObjectPool<>(pof, config)) {
assertNotNull(connectionPool);

CountDownLatch startLatch = new CountDownLatch(1);

List<Object> poolObjects = new ArrayList<>();

List<Runnable> tasks = new ArrayList<>();

for (int i = 0; i < maxConnections; i++) {
poolObjects.add(connectionPool.borrowObject());
}

for(Object poolObject : poolObjects) {

tasks.add(() -> {
try {
startLatch.await();
connectionPool.invalidateObject(poolObject);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restore interrupt status
}
});

tasks.add(() -> {
try {
startLatch.await();
connectionPool.returnObject(poolObject);
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // Restore interrupt status
}
});
}

tasks.forEach(executor::submit);

startLatch.countDown(); // Start all tasks simultaneously

executor.shutdown();
assertTrue(executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS));

assertEquals(0, connectionPool.getNumActive(), "getNumActive() must not return a negative value");

}
}
}