Remove useless cluster check

This commit is contained in:
Stephane Nicoll 2017-06-27 15:16:09 +02:00
parent 1b181b2f34
commit 1a839d6656
2 changed files with 12 additions and 94 deletions

View File

@ -22,7 +22,6 @@ import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import redis.clients.jedis.Jedis;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
@ -120,23 +119,19 @@ public class RedisAutoConfigurationJedisTests {
@Test
public void testRedisConfigurationWithSentinel() throws Exception {
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");
if (isAtLeastOneNodeAvailable(sentinels)) {
load("spring.redis.sentinel.master:mymaster", "spring.redis.sentinel.nodes:"
+ StringUtils.collectionToCommaDelimitedString(sentinels));
assertThat(this.context.getBean(JedisConnectionFactory.class)
.isRedisSentinelAware()).isTrue();
}
load("spring.redis.sentinel.master:mymaster", "spring.redis.sentinel.nodes:"
+ StringUtils.collectionToCommaDelimitedString(sentinels));
assertThat(this.context.getBean(JedisConnectionFactory.class)
.isRedisSentinelAware()).isTrue();
}
@Test
public void testRedisConfigurationWithCluster() throws Exception {
List<String> clusterNodes = Arrays.asList("127.0.0.1:27379", "127.0.0.1:27380");
if (isAtLeastOneNodeAvailable(clusterNodes)) {
load("spring.redis.cluster.nodes[0]:" + clusterNodes.get(0),
"spring.redis.cluster.nodes[1]:" + clusterNodes.get(1));
assertThat(this.context.getBean(JedisConnectionFactory.class)
.getClusterConnection()).isNotNull();
}
load("spring.redis.cluster.nodes[0]:" + clusterNodes.get(0),
"spring.redis.cluster.nodes[1]:" + clusterNodes.get(1));
assertThat(this.context.getBean(JedisConnectionFactory.class)
.getClusterConnection()).isNotNull();
}
private void load(String... environment) {
@ -154,41 +149,6 @@ public class RedisAutoConfigurationJedisTests {
this.context = ctx;
}
private boolean isAtLeastOneNodeAvailable(List<String> nodes) {
for (String node : nodes) {
if (isAvailable(node)) {
return true;
}
}
return false;
}
private boolean isAvailable(String node) {
Jedis jedis = null;
try {
String[] hostAndPort = node.split(":");
jedis = new Jedis(hostAndPort[0], Integer.valueOf(hostAndPort[1]));
jedis.connect();
jedis.ping();
return true;
}
catch (Exception ex) {
return false;
}
finally {
if (jedis != null) {
try {
jedis.disconnect();
jedis.close();
}
catch (Exception ex) {
// Continue
}
}
}
}
@Configuration
static class CustomConfiguration {

View File

@ -18,11 +18,7 @@ package org.springframework.boot.autoconfigure.data.redis;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import org.junit.After;
import org.junit.Test;
@ -146,12 +142,10 @@ public class RedisAutoConfigurationTests {
@Test
public void testRedisConfigurationWithSentinel() throws Exception {
List<String> sentinels = Arrays.asList("127.0.0.1:26379", "127.0.0.1:26380");
if (isAtLeastOneNodeAvailable(sentinels)) {
load("spring.redis.sentinel.master:mymaster", "spring.redis.sentinel.nodes:"
+ StringUtils.collectionToCommaDelimitedString(sentinels));
assertThat(this.context.getBean(LettuceConnectionFactory.class)
.isRedisSentinelAware()).isTrue();
}
load("spring.redis.sentinel.master:mymaster", "spring.redis.sentinel.nodes:"
+ StringUtils.collectionToCommaDelimitedString(sentinels));
assertThat(this.context.getBean(LettuceConnectionFactory.class)
.isRedisSentinelAware()).isTrue();
}
@Test
@ -167,42 +161,6 @@ public class RedisAutoConfigurationTests {
return (DefaultLettucePool) ReflectionTestUtils.getField(factory, "pool");
}
private boolean isAtLeastOneNodeAvailable(List<String> nodes) {
for (String node : nodes) {
if (isAvailable(node)) {
return true;
}
}
return false;
}
private boolean isAvailable(String node) {
RedisClient redisClient = null;
try {
String[] hostAndPort = node.split(":");
redisClient = RedisClient.create(new RedisURI(hostAndPort[0],
Integer.valueOf(hostAndPort[1]), 10, TimeUnit.SECONDS));
StatefulRedisConnection<String, String> connection = redisClient.connect();
connection.sync().ping();
connection.close();
return true;
}
catch (Exception ex) {
return false;
}
finally {
if (redisClient != null) {
try {
redisClient.shutdown(0, 0, TimeUnit.SECONDS);
}
catch (Exception ex) {
// Continue
}
}
}
}
private void load(String... environment) {
load(null, environment);
}