Skip to content

Commit

Permalink
Fixed PoolSize and unit test
Browse files Browse the repository at this point in the history
- Funny trap I made for myself.
- Integration tests detected the bug (jdbc_all)

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Apr 11, 2023
1 parent c0b09d1 commit c9680fd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions appserver/connectors/connectors-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public int getCurrentCount() {
* @throws PoolingException if the current count already reached the capacity.
*/
public void increment() throws PoolingException {
if (this.currentCount.getAndUpdate(v -> Math.max(v, this.capacity)) == this.capacity) {
if (this.currentCount.getAndUpdate(v -> Math.min(v + 1, this.capacity)) == this.capacity) {
throw new PoolingException("Count of provided connections is already equal to the capacity (" + capacity
+ ") therefore you cannot allocate any more resources.");
}
Expand All @@ -78,7 +78,7 @@ public void increment() throws PoolingException {
* If is already zero, doesn't do anything.
*/
public void decrement() {
this.currentCount.getAndUpdate(v -> Math.min(v, 0));
this.currentCount.getAndUpdate(v -> Math.max(v - 1, 0));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -42,19 +44,21 @@ public class PoolSizeTest {

@Test
void increaseAndDecrease() throws Exception {
PoolSize poolSize = new PoolSize(1);
assertEquals(1, poolSize.getCapacity());
poolSize.decrement();
assertEquals(0, poolSize.getCurrentCount());
PoolSize poolSize = new PoolSize(2);
assertEquals(2, poolSize.getCapacity());
poolSize.increment();
assertEquals(1, poolSize.getCurrentCount());
poolSize.increment();
assertEquals(2, poolSize.getCurrentCount());
PoolingException e = assertThrows(PoolingException.class, () -> poolSize.increment());
assertAll(
() -> assertEquals(1, poolSize.getCurrentCount()),
() -> assertEquals("Count of provided connections is already equal to the capacity (1) therefore"
() -> assertEquals(2, poolSize.getCurrentCount()),
() -> assertEquals("Count of provided connections is already equal to the capacity (2) therefore"
+ " you cannot allocate any more resources.", e.getMessage())
);
poolSize.decrement();
assertEquals(1, poolSize.getCurrentCount());
poolSize.decrement();
assertEquals(0, poolSize.getCurrentCount());
poolSize.decrement();
assertEquals(0, poolSize.getCurrentCount());
Expand All @@ -72,9 +76,11 @@ void threadSafety() throws Exception {
for (int i = 0; i < taskCount; i++) {
tasks.add(() -> {
poolSize.increment();
// increment succeeded
increases.incrementAndGet();
Thread.sleep(0, 10);
poolSize.decrement();
// if inc succeeded, dec must succeed too -> same count.
decreases.incrementAndGet();
return null;
});
Expand All @@ -83,6 +89,7 @@ void threadSafety() throws Exception {
assertAll(
() -> assertTrue(futures.stream().allMatch(f -> f.isDone()), "All done."),
() -> assertFalse(futures.stream().anyMatch(f -> f.isCancelled()), "None cancelled."),
() -> assertThat("Count of increases", increases.get(), greaterThan(poolSize.getCapacity())),
() -> assertEquals(increases.get(), decreases.get(), "Count of increases and decreases"),
() -> assertEquals(0, poolSize.getCurrentCount(), "Current count")
);
Expand Down

0 comments on commit c9680fd

Please sign in to comment.