Skip to content

Fix for SimplePool.setPoolSize() #3143 #3144

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,9 @@ public synchronized void setPoolSize(int poolSize) {
break;
}
T item = this.available.poll();
if (item == null) {
this.permits.release();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have stopped to release permits.
Why is that, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this branch we need to call only acquire(). Original intent was to revert acquire if item==null. But this is absolutely incorrect. For example, if we have a new instance of pool, then "available" collection is empty. But setPoolSize() method should correctly update all structures. After calling setPoolSize(5) everything should contain 5. (permits==5, poolSize==5, and targetPoolSize==5). Of course, I am describing the situation when no objects were allocated. Please, check added test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added additional checks to test that "permits" field is correctly updated

break;
if (item != null) {
doRemoveItem(item);
}
doRemoveItem(item);
this.poolSize.decrementAndGet();
delta++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@

package org.springframework.integration.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;

import org.junit.Test;

import org.springframework.integration.test.util.TestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, do not rearrange imports order.
Consider to run gradlew check --parallel task to be sure that your changes follows our Checkstyle rules.
Also that will say you if your fix is valid against all the existing tests in the project.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I tried to keep imports untouched. I will revert this


/**
* @author Gary Russell
* @since 2.2
Expand Down Expand Up @@ -146,7 +146,27 @@ public void testDoubleReturn() {
pool.releaseItem(s1);
assertThat(permits.availablePermits()).isEqualTo(2);
}

@Test
public void testSizeUpdateIfNotAllocated() {
SimplePool<String> pool = stringPool(10, new HashSet<>(), new AtomicBoolean());
pool.setPoolSize(5);
assertThat(pool.getPoolSize()).isEqualTo(5);
}

@Test
public void testSizeUpdateIfAllocated() {
SimplePool<String> pool = stringPool(10, new HashSet<>(), new AtomicBoolean());
List<String> allocated = new ArrayList<>();
for (int i = 0; i < 10; i++) {
allocated.add(pool.getItem());
}
for (int i = 0; i < 10; i++) {
pool.releaseItem(allocated.get(i));
}
pool.setPoolSize(5);
assertThat(pool.getPoolSize()).isEqualTo(5);
}

private SimplePool<String> stringPool(int size, final Set<String> strings,
final AtomicBoolean stale) {
Expand Down