Skip to content

Commit

Permalink
Polishing #1611
Browse files Browse the repository at this point in the history
Add unit test for cancel in-flight connection creation.
  • Loading branch information
mp911de committed Feb 3, 2021
1 parent 5dcf3bb commit 4a98e5b
Showing 1 changed file with 62 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/
package io.lettuce.core.support;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -38,9 +37,11 @@
class BoundedAsyncPoolUnitTests {

private AtomicInteger counter = new AtomicInteger();

private List<String> destroyed = new ArrayList<>();

private AsyncObjectFactory<String> STRING_OBJECT_FACTORY = new AsyncObjectFactory<String>() {

@Override
public CompletableFuture<String> create() {
return CompletableFuture.completedFuture(counter.incrementAndGet() + "");
Expand All @@ -56,6 +57,7 @@ public CompletableFuture<Void> destroy(String object) {
public CompletableFuture<Boolean> validate(String object) {
return CompletableFuture.completedFuture(true);
}

};

@Test
Expand Down Expand Up @@ -119,8 +121,8 @@ public CompletableFuture<Boolean> validate(String object) {
@Test
void shouldCreateMinIdleObject() {

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY, BoundedPoolConfig.builder().minIdle(2)
.build());
BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY,
BoundedPoolConfig.builder().minIdle(2).build());

assertThat(pool.getIdle()).isEqualTo(2);
assertThat(pool.getObjectCount()).isEqualTo(2);
Expand All @@ -129,8 +131,8 @@ void shouldCreateMinIdleObject() {
@Test
void shouldCreateMaintainMinIdleObject() {

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY, BoundedPoolConfig.builder().minIdle(2)
.build());
BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY,
BoundedPoolConfig.builder().minIdle(2).build());

TestFutures.awaitOrTimeout(pool.acquire());

Expand All @@ -141,8 +143,8 @@ void shouldCreateMaintainMinIdleObject() {
@Test
void shouldCreateMaintainMinMaxIdleObject() {

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY, BoundedPoolConfig.builder().minIdle(2)
.maxTotal(2).build());
BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY,
BoundedPoolConfig.builder().minIdle(2).maxTotal(2).build());

TestFutures.awaitOrTimeout(pool.acquire());

Expand Down Expand Up @@ -176,8 +178,8 @@ void shouldReuseObjects() {
@Test
void shouldDestroyIdle() {

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY, BoundedPoolConfig.builder().maxIdle(2)
.maxTotal(5).build());
BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY,
BoundedPoolConfig.builder().maxIdle(2).maxTotal(5).build());

List<String> objects = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Expand All @@ -200,8 +202,8 @@ void shouldDestroyIdle() {
@Test
void shouldExhaustPool() {

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY, BoundedPoolConfig.builder().maxTotal(4)
.build());
BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY,
BoundedPoolConfig.builder().maxTotal(4).build());

String object1 = TestFutures.getOrTimeout(pool.acquire());
String object2 = TestFutures.getOrTimeout(pool.acquire());
Expand All @@ -228,8 +230,8 @@ void shouldExhaustPool() {
@Test
void shouldClearPool() {

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY, BoundedPoolConfig.builder().maxTotal(4)
.build());
BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(STRING_OBJECT_FACTORY,
BoundedPoolConfig.builder().maxTotal(4).build());

for (int i = 0; i < 20; i++) {

Expand Down Expand Up @@ -257,6 +259,7 @@ void shouldExhaustPoolConcurrent() {

List<CompletableFuture<String>> progress = new ArrayList<>();
AsyncObjectFactory<String> IN_PROGRESS = new AsyncObjectFactory<String>() {

@Override
public CompletableFuture<String> create() {

Expand All @@ -276,6 +279,7 @@ public CompletableFuture<Void> destroy(String object) {
public CompletableFuture<Boolean> validate(String object) {
return CompletableFuture.completedFuture(true);
}

};

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(IN_PROGRESS, BoundedPoolConfig.builder().maxTotal(4).build());
Expand Down Expand Up @@ -304,6 +308,7 @@ void shouldConcurrentlyFail() {

List<CompletableFuture<String>> progress = new ArrayList<>();
AsyncObjectFactory<String> IN_PROGRESS = new AsyncObjectFactory<String>() {

@Override
public CompletableFuture<String> create() {

Expand All @@ -323,6 +328,7 @@ public CompletableFuture<Void> destroy(String object) {
public CompletableFuture<Boolean> validate(String object) {
return CompletableFuture.completedFuture(true);
}

};

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(IN_PROGRESS, BoundedPoolConfig.builder().maxTotal(4).build());
Expand All @@ -343,4 +349,46 @@ public CompletableFuture<Boolean> validate(String object) {
assertThat(pool.getObjectCount()).isZero();
assertThat(pool.getCreationInProgress()).isZero();
}

@Test
void cancelShouldReturnObjectToPool() {

List<CompletableFuture<String>> progress = new ArrayList<>();
AsyncObjectFactory<String> IN_PROGRESS = new AsyncObjectFactory<String>() {

@Override
public CompletableFuture<String> create() {

CompletableFuture<String> future = new CompletableFuture<>();
progress.add(future);

return future;
}

@Override
public CompletableFuture<Void> destroy(String object) {
destroyed.add(object);
return CompletableFuture.completedFuture(null);
}

@Override
public CompletableFuture<Boolean> validate(String object) {
return CompletableFuture.completedFuture(true);
}

};

BoundedAsyncPool<String> pool = new BoundedAsyncPool<>(IN_PROGRESS,
BoundedPoolConfig.builder().maxTotal(1).maxIdle(0).build());

CompletableFuture<String> acquire = pool.acquire();

assertThat(acquire).isNotCompleted();
acquire.cancel(false);
assertThat(acquire).isCancelled();

progress.get(0).complete("after-cancel");
assertThat(destroyed).contains("after-cancel");
}

}

0 comments on commit 4a98e5b

Please sign in to comment.