Skip to content

Commit

Permalink
Allow to prepare the pool
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Dec 16, 2024
1 parent c83f5ac commit 9786447
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Asmir Mustafic
* @since 2.0
* @see #getConnection(Class)
*/
Expand Down Expand Up @@ -90,8 +91,18 @@ class LettucePoolingConnectionProvider implements LettuceConnectionProvider, Red
public <T extends StatefulConnection<?, ?>> T getConnection(Class<T> connectionType) {

GenericObjectPool<StatefulConnection<?, ?>> pool = pools.computeIfAbsent(connectionType, poolType -> {
return ConnectionPoolSupport.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType),
poolConfig, false);

GenericObjectPool<StatefulConnection<?, ?>> newPool = ConnectionPoolSupport
.createGenericObjectPool(() -> connectionProvider.getConnection(connectionType), poolConfig, false);

try {
newPool.preparePool();

} catch (Exception ex) {
throw new PoolException("Could not prepare the pool", ex);
}

return newPool;
});

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,21 @@
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration.LettucePoolingClientConfigurationBuilder;

/**
* Unit tests for {@link LettucePoolingConnectionProvider}.
*
* @author Mark Paluch
* @author Asmir Mustafic
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -70,4 +73,21 @@ void shouldDiscardTransactionOnReleaseOnActiveTransaction() {

verify(commandsMock).discard();
}

@Test
void shouldPrepareThePool() {

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMinIdle(5);
poolConfig.setMaxIdle(8);
poolConfig.setMaxTotal(10);

LettucePoolingClientConfiguration config = new LettucePoolingClientConfigurationBuilder().poolConfig(poolConfig)
.build();

LettucePoolingConnectionProvider provider = new LettucePoolingConnectionProvider(connectionProviderMock, config);

provider.getConnection(StatefulRedisConnection.class);
verify(connectionProviderMock, times(5)).getConnection(any());
}
}

0 comments on commit 9786447

Please sign in to comment.