DATAREDIS-930 - Return port/host of WithHostAndPort configurations using LettuceConnectionFactory.getPort() and getHost().
We now correctly return host and port from configurations that implement WithHostAndPort instead of always using RedisStandaloneConfiguration. Original pull request: #385.
This commit is contained in:
committed by
Mark Paluch
parent
5d338bacc9
commit
99508cb332
@@ -18,6 +18,7 @@ package org.springframework.data.redis.connection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.IntSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
@@ -29,6 +30,7 @@ import org.springframework.util.Assert;
|
||||
* configurations for the individual purposes.
|
||||
*
|
||||
* @author Christoph Strobl
|
||||
* @author Luis De Bello
|
||||
* @since 2.1
|
||||
*/
|
||||
public interface RedisConfiguration {
|
||||
@@ -85,6 +87,14 @@ public interface RedisConfiguration {
|
||||
return configuration instanceof SentinelConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configuration can be {@literal null}.
|
||||
* @return {@code true} if given {@link RedisConfiguration} is instance of {@link WithHostAndPort}.
|
||||
*/
|
||||
static boolean isHostAndPortAware(@Nullable RedisConfiguration configuration) {
|
||||
return configuration instanceof WithHostAndPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configuration can be {@literal null}.
|
||||
* @return {@code true} if given {@link RedisConfiguration} is instance of {@link ClusterConfiguration}.
|
||||
@@ -135,6 +145,32 @@ public interface RedisConfiguration {
|
||||
return isPasswordAware(configuration) ? ((WithPassword) configuration).getPassword() : other.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configuration can be {@literal null}.
|
||||
* @param other a {@code Supplier} whose result is returned if given {@link RedisConfiguration} is not
|
||||
* {@link #isHostAndPortAware(RedisConfiguration) port aware}.
|
||||
* @return never {@literal null}.
|
||||
* @throws IllegalArgumentException if {@code other} is {@literal null}.
|
||||
*/
|
||||
static int getPortOrElse(@Nullable RedisConfiguration configuration, IntSupplier other) {
|
||||
|
||||
Assert.notNull(other, "Other must not be null!");
|
||||
return isHostAndPortAware(configuration) ? ((WithHostAndPort) configuration).getPort() : other.getAsInt();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param configuration can be {@literal null}.
|
||||
* @param other a {@code Supplier} whose result is returned if given {@link RedisConfiguration} is not
|
||||
* {@link #isHostAndPortAware(RedisConfiguration) host aware}.
|
||||
* @return never {@literal null}.
|
||||
* @throws IllegalArgumentException if {@code other} is {@literal null}.
|
||||
*/
|
||||
static String getHostOrElse(@Nullable RedisConfiguration configuration, Supplier<String> other) {
|
||||
|
||||
Assert.notNull(other, "Other must not be null!");
|
||||
return isHostAndPortAware(configuration) ? ((WithHostAndPort) configuration).getHostName() : other.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link RedisConfiguration} part suitable for configurations that may use authentication when connecting.
|
||||
*
|
||||
|
||||
@@ -86,6 +86,7 @@ import org.springframework.util.ClassUtils;
|
||||
* @author Mark Paluch
|
||||
* @author Balázs Németh
|
||||
* @author Ruben Cervilla
|
||||
* @author Luis De Bello
|
||||
*/
|
||||
public class LettuceConnectionFactory
|
||||
implements InitializingBean, DisposableBean, RedisConnectionFactory, ReactiveRedisConnectionFactory {
|
||||
@@ -465,7 +466,7 @@ public class LettuceConnectionFactory
|
||||
* @return the host.
|
||||
*/
|
||||
public String getHostName() {
|
||||
return standaloneConfig.getHostName();
|
||||
return RedisConfiguration.getHostOrElse(configuration, standaloneConfig::getHostName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -485,7 +486,7 @@ public class LettuceConnectionFactory
|
||||
* @return the port.
|
||||
*/
|
||||
public int getPort() {
|
||||
return standaloneConfig.getPort();
|
||||
return RedisConfiguration.getPortOrElse(configuration, standaloneConfig::getPort);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,6 +22,7 @@ import static org.hamcrest.core.IsNull.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.redis.connection.ClusterTestVariables.*;
|
||||
import static org.springframework.data.redis.connection.RedisConfiguration.*;
|
||||
import static org.springframework.data.redis.connection.lettuce.LettuceTestClientResources.*;
|
||||
import static org.springframework.test.util.ReflectionTestUtils.*;
|
||||
|
||||
@@ -47,6 +48,7 @@ import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.data.redis.ConnectionFactoryTracker;
|
||||
import org.springframework.data.redis.connection.RedisClusterConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisClusterConnection;
|
||||
import org.springframework.data.redis.connection.RedisConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisPassword;
|
||||
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
|
||||
import org.springframework.data.redis.connection.RedisSocketConfiguration;
|
||||
@@ -60,6 +62,7 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
* @author Mark Paluch
|
||||
* @author Balázs Németh
|
||||
* @author Ruben Cervilla
|
||||
* @author Luis De Bello
|
||||
*/
|
||||
public class LettuceConnectionFactoryUnitTests {
|
||||
|
||||
@@ -106,6 +109,52 @@ public class LettuceConnectionFactoryUnitTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-930
|
||||
public void portShouldBeReturnedProperlyBasedOnConfiguration() {
|
||||
|
||||
RedisConfiguration redisConfiguration = new RedisStandaloneConfiguration("localhost", 16379);
|
||||
|
||||
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisConfiguration,
|
||||
LettuceClientConfiguration.defaultConfiguration());
|
||||
|
||||
assertThat(connectionFactory.getPort(), is(16379));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-930
|
||||
public void portShouldBeReturnedProperlyBasedOnCustomRedisConfiguration() {
|
||||
|
||||
RedisConfiguration redisConfiguration = new CustomRedisConfiguration("localhost", 16379);
|
||||
|
||||
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisConfiguration,
|
||||
LettuceClientConfiguration.defaultConfiguration());
|
||||
|
||||
assertThat(connectionFactory.getPort(), is(16379));
|
||||
assertThat(connectionFactory.getHostName(), is("localhost"));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-930
|
||||
public void hostNameShouldBeReturnedProperlyBasedOnConfiguration() {
|
||||
|
||||
RedisConfiguration redisConfiguration = new RedisStandaloneConfiguration("external");
|
||||
|
||||
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisConfiguration,
|
||||
LettuceClientConfiguration.defaultConfiguration());
|
||||
|
||||
assertThat(connectionFactory.getHostName(), is("external"));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-930
|
||||
public void hostNameShouldBeReturnedProperlyBasedOnCustomRedisConfiguration() {
|
||||
|
||||
RedisConfiguration redisConfiguration = new CustomRedisConfiguration("external");
|
||||
|
||||
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisConfiguration,
|
||||
LettuceClientConfiguration.defaultConfiguration());
|
||||
|
||||
assertThat(connectionFactory.getPort(), is(6379));
|
||||
assertThat(connectionFactory.getHostName(), is("external"));
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-315
|
||||
@SuppressWarnings("unchecked")
|
||||
public void passwordShouldBeSetCorrectlyOnClusterClient() {
|
||||
@@ -630,4 +679,42 @@ public class LettuceConnectionFactoryUnitTests {
|
||||
|
||||
assertThat(redisUri.getDatabase(), is(equalTo(1)));
|
||||
}
|
||||
|
||||
private static class CustomRedisConfiguration implements RedisConfiguration, WithHostAndPort {
|
||||
|
||||
private static final String DEFAULT_HOST = "localhost";
|
||||
private static final int DEFAULT_PORT = 6379;
|
||||
|
||||
private String hostName = DEFAULT_HOST;
|
||||
private int port = DEFAULT_PORT;
|
||||
|
||||
public CustomRedisConfiguration(String hostName) {
|
||||
this(hostName, DEFAULT_PORT);
|
||||
}
|
||||
|
||||
public CustomRedisConfiguration(String hostName, int port) {
|
||||
this.hostName = hostName;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setHostName(String hostName) {
|
||||
this.hostName = hostName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user