Skip to content

Commit

Permalink
DATAREDIS-524 - Polishing.
Browse files Browse the repository at this point in the history
Some formatting changes.

Original Pull Request: #204
  • Loading branch information
christophstrobl committed Jul 6, 2016
1 parent 87ebf7d commit a4b5442
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,18 @@ public void afterPropertiesSet() {
*/
private RedisURI getRedisURI() {

if (isRedisSentinelAware()) {
RedisURI redisURI = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
RedisURI redisUri = isRedisSentinelAware()
? LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration) : createSimpleHostRedisURI();

if (StringUtils.hasText(password)) {
redisURI.setPassword(password);
}

return redisURI;
if (StringUtils.hasText(password)) {
redisUri.setPassword(password);
}

return createSimpleHostRedisURI();
return redisUri;
}

private RedisURI createSimpleHostRedisURI() {
RedisURI.Builder builder = RedisURI.Builder.redis(hostName, port);
if (password != null) {
builder.withPassword(password);
}
builder.withTimeout(timeout, TimeUnit.MILLISECONDS);
return builder.build();
return RedisURI.Builder.redis(hostName, port).withTimeout(timeout, TimeUnit.MILLISECONDS).build();
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* @author Costin Leau
* @author Jennifer Hickey
* @author Thomas Darimont
* @author Christoph Strobl
* @author Mark Paluch
* @author Balázs Németh
*/
Expand Down Expand Up @@ -608,13 +609,13 @@ private AbstractRedisClient createRedisClient() {

private RedisURI getSentinelRedisURI() {

RedisURI redisURI = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);
RedisURI redisUri = LettuceConverters.sentinelConfigurationToRedisURI(sentinelConfiguration);

if (StringUtils.hasText(password)) {
redisURI.setPassword(password);
redisUri.setPassword(password);
}

return redisURI;
return redisUri;
}

/**
Expand All @@ -634,6 +635,7 @@ public boolean isClusterAware() {

@Override
public RedisSentinelConnection getSentinelConnection() {

if (!(client instanceof RedisClient)) {
throw new InvalidDataAccessResourceUsageException("Unable to connect to sentinels using " + client.getClass());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.data.redis.SettingsUtils;
import org.springframework.data.redis.connection.PoolConfig;
import org.springframework.data.redis.connection.PoolException;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;

Expand All @@ -49,20 +48,22 @@ public class DefaultLettucePoolTests {

@After
public void tearDown() {
if (this.pool != null) {

if (this.pool.getClient() != null) {
this.pool.getClient().shutdown(0, 0, TimeUnit.MILLISECONDS);
if (pool != null) {

if (pool.getClient() != null) {
pool.getClient().shutdown(0, 0, TimeUnit.MILLISECONDS);
}

this.pool.destroy();
pool.destroy();
}
}

@Test
public void testGetResource() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());

pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -72,11 +73,12 @@ public void testGetResource() {

@Test
public void testGetResourcePoolExhausted() {

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(1);
poolConfig.setMaxWaitMillis(1);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -90,10 +92,11 @@ public void testGetResourcePoolExhausted() {

@Test
public void testGetResourceValidate() {
PoolConfig poolConfig = new PoolConfig();

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setTestOnBorrow(true);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -102,19 +105,21 @@ public void testGetResourceValidate() {

@Test(expected = PoolException.class)
public void testGetResourceCreationUnsuccessful() throws Exception {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), 3333);
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());

pool = new DefaultLettucePool(SettingsUtils.getHost(), 3333);
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.afterPropertiesSet();
pool.getResource();
}

@Test
public void testReturnResource() {

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(1);
poolConfig.setMaxWaitMillis(1);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -125,11 +130,12 @@ public void testReturnResource() {

@Test
public void testReturnBrokenResource() {

GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(1);
poolConfig.setMaxWaitMillis(1);
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort(), poolConfig);
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> client = pool.getResource();
assertNotNull(client);
Expand All @@ -147,26 +153,29 @@ public void testReturnBrokenResource() {

@Test
public void testCreateWithDbIndex() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());

pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.setDatabase(1);
pool.afterPropertiesSet();
assertNotNull(pool.getResource());
}

@Test(expected = PoolException.class)
public void testCreateWithDbIndexInvalid() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());

pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.setDatabase(17);
pool.afterPropertiesSet();
pool.getResource();
}

@Test(expected = PoolException.class)
public void testCreateWithPasswordNoPassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());

pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.setPassword("notthepassword");
pool.afterPropertiesSet();
pool.getResource();
Expand All @@ -175,8 +184,9 @@ public void testCreateWithPasswordNoPassword() {
@Ignore("Redis must have requirepass set to run this test")
@Test
public void testCreatePassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());

pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.setPassword("foo");
pool.afterPropertiesSet();
RedisAsyncConnection<byte[], byte[]> conn = pool.getResource();
Expand All @@ -187,8 +197,9 @@ public void testCreatePassword() {
@Ignore("Redis must have requirepass set to run this test")
@Test(expected = PoolException.class)
public void testCreateInvalidPassword() {
this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
this.pool.setClientResources(LettuceTestClientResources.getSharedClientResources());

pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
pool.setPassword("bad");
pool.afterPropertiesSet();
pool.getResource();
Expand All @@ -205,9 +216,9 @@ public void testCreateSentinelWithPassword() {
pool.setPassword("foo");
pool.afterPropertiesSet();

RedisURI redisURI = (RedisURI) getField(pool.getClient(), "redisURI");
RedisURI redisUri = (RedisURI) getField(pool.getClient(), "redisURI");

assertThat(redisURI.getPassword(), is(equalTo(pool.getPassword().toCharArray())));
assertThat(redisUri.getPassword(), is(equalTo(pool.getPassword().toCharArray())));
}

/**
Expand All @@ -216,7 +227,7 @@ public void testCreateSentinelWithPassword() {
@Test
public void poolWorksWithoutClientResources() {

this.pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
pool.setDatabase(1);
pool.afterPropertiesSet();
assertNotNull(pool.getResource());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ public void passwordShouldBeSetCorrectlyOnSentinelClient() {
AbstractRedisClient client = (AbstractRedisClient) getField(connectionFactory, "client");
assertThat(client, instanceOf(RedisClient.class));

RedisURI redisURI = (RedisURI) getField(client, "redisURI");
RedisURI redisUri = (RedisURI) getField(client, "redisURI");

assertThat(redisURI.getPassword(), is(equalTo(connectionFactory.getPassword().toCharArray())));
assertThat(redisUri.getPassword(), is(equalTo(connectionFactory.getPassword().toCharArray())));
}

/**
Expand Down

0 comments on commit a4b5442

Please sign in to comment.