Guard non-initialized RedisConnectionFactory.getConnection() with IllegalStateException.

Obtaining a Connection from RedisConnectionFactory and its reactive variant is now guarded by IllegalStateException that is thrown if the connection factory was not yet initialized.

Previously, connections could be obtained where configuration was partially applied.

Closes #2057
This commit is contained in:
Mark Paluch
2021-05-12 11:56:22 +02:00
parent 8d42c7c1b9
commit b4e75e5f96
7 changed files with 86 additions and 3 deletions

View File

@@ -90,6 +90,7 @@ class JedisConnectionFactorySentinelIntegrationTests {
.sentinel("127.0.0.1", 1).sentinel("127.0.0.1", 26379);
factory = new JedisConnectionFactory(oneDownSentinelConfig);
factory.afterPropertiesSet();
assertThat(factory.getSentinelConnection().isOpen()).isTrue();
}
}

View File

@@ -346,6 +346,16 @@ class JedisConnectionFactoryUnitTests {
assertThatIllegalStateException().isThrownBy(() -> connectionFactory.setClientName("foo"));
}
@Test // GH-2057
void getConnectionShouldFailIfNotInitialized() {
JedisConnectionFactory connectionFactory = new JedisConnectionFactory();
assertThatIllegalStateException().isThrownBy(connectionFactory::getConnection);
assertThatIllegalStateException().isThrownBy(connectionFactory::getClusterConnection);
assertThatIllegalStateException().isThrownBy(connectionFactory::getSentinelConnection);
}
private JedisConnectionFactory initSpyedConnectionFactory(RedisSentinelConfiguration sentinelConfig,
JedisPoolConfig poolConfig) {

View File

@@ -1011,7 +1011,19 @@ class LettuceConnectionFactoryUnitTests {
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
assertThatIllegalStateException().isThrownBy(connectionFactory::getRequiredNativeClient)
.withMessageContaining("Client not yet initialized");
.withMessageContaining("was not initialized through");
}
@Test // GH-2057
void getConnectionShouldFailIfNotInitialized() {
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory();
assertThatIllegalStateException().isThrownBy(connectionFactory::getConnection);
assertThatIllegalStateException().isThrownBy(connectionFactory::getClusterConnection);
assertThatIllegalStateException().isThrownBy(connectionFactory::getSentinelConnection);
assertThatIllegalStateException().isThrownBy(connectionFactory::getReactiveConnection);
assertThatIllegalStateException().isThrownBy(connectionFactory::getReactiveClusterConnection);
}
@Data