Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UNDERTOW-1487: SimpleObjectPool.close may be invoked multiple times
- Loading branch information
1 parent
cbcdcf8
commit 7fa9d39
Showing
3 changed files
with
62 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/src/test/java/io/undertow/util/SimpleObjectPoolTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.undertow.util; | ||
|
||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class SimpleObjectPoolTestCase { | ||
|
||
@Rule | ||
public final ExpectedException expected = ExpectedException.none(); | ||
|
||
@Test | ||
public void testObjectAlreadyReturned() { | ||
SimpleObjectPool<Object> pool = new SimpleObjectPool<>(1, Object::new, obj -> {}, obj -> {}); | ||
PooledObject<Object> pooled = pool.allocate(); | ||
pooled.close(); | ||
|
||
expected.expect(IllegalStateException.class); | ||
pooled.getObject(); | ||
} | ||
|
||
@Test | ||
public void testCloseMayBeInvokedMultipleTimesWhenObjectIsRecycled() { | ||
AtomicInteger recycled = new AtomicInteger(); | ||
AtomicInteger destroyed = new AtomicInteger(); | ||
SimpleObjectPool<Object> pool = new SimpleObjectPool<>( | ||
1, Object::new, obj -> recycled.incrementAndGet(), obj -> destroyed.incrementAndGet()); | ||
PooledObject<Object> pooled = pool.allocate(); | ||
pooled.close(); | ||
pooled.close(); | ||
assertEquals("Pooled object should only be recycled once", 1, recycled.get()); | ||
assertEquals("Pooled object should be queued for reuse, not destroyed", 0, destroyed.get()); | ||
} | ||
|
||
@Test | ||
public void testCloseMayBeInvokedMultipleTimesWhenObjectIsConsumed() { | ||
AtomicInteger recycled = new AtomicInteger(); | ||
AtomicInteger destroyed = new AtomicInteger(); | ||
SimpleObjectPool<Object> pool = new SimpleObjectPool<>( | ||
1, Object::new, obj -> recycled.incrementAndGet(), obj -> destroyed.incrementAndGet()); | ||
PooledObject<Object> initial = pool.allocate(); | ||
PooledObject<Object> pooled = pool.allocate(); | ||
initial.close(); // This object fills the queue so that 'pooled' should be destroyed | ||
pooled.close(); | ||
pooled.close(); | ||
assertEquals("Each pooled object should be recycled", 2, recycled.get()); | ||
assertEquals("Pooled object should be destroyed exactly once", 1, destroyed.get()); | ||
} | ||
} |