DATAREDIS-372 - Allow easier RedisSentinelConfiguration setup.
We now allow to initialize a RedisSentinelConfiguration based on a given String collection of host:ports. sentinelHostAndPorts[0] = 127.0.0.1:23679 sentinelHostAndPorts[1] = 127.0.0.1:23680 Further on it’s possible to pass in a PropertySource containing master name and sentinel nodes. spring.redis.sentinel.master=myMaster spring.redis.sentinel.nodes=127.0.0.1:23679,127.0.0.1:23680 Original pull request: #126.
This commit is contained in:
committed by
Thomas Darimont
parent
43a6a60c8d
commit
c662c46d02
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright 2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.redis.connection;
|
||||
|
||||
import static org.hamcrest.core.Is.*;
|
||||
import static org.hamcrest.core.IsCollectionContaining.*;
|
||||
import static org.hamcrest.core.IsNull.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.mock.env.MockPropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
*/
|
||||
public class RedisSentinelConfigurationUnitTests {
|
||||
|
||||
static final String HOST_AND_PORT_1 = "127.0.0.1:123";
|
||||
static final String HOST_AND_PORT_2 = "localhost:456";
|
||||
static final String HOST_AND_PORT_3 = "localhost:789";
|
||||
static final String HOST_AND_NO_PORT = "localhost";
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test
|
||||
public void shouldCreateRedisSentinelConfigurationCorrectlyGivenMasterAndSingleHostAndPortString() {
|
||||
|
||||
RedisSentinelConfiguration config = new RedisSentinelConfiguration("mymaster",
|
||||
Collections.singleton(HOST_AND_PORT_1));
|
||||
|
||||
assertThat(config.getSentinels().size(), is(1));
|
||||
assertThat(config.getSentinels(), hasItems(new RedisNode("127.0.0.1", 123)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test
|
||||
public void shouldCreateRedisSentinelConfigurationCorrectlyGivenMasterAndMultipleHostAndPortStrings() {
|
||||
|
||||
RedisSentinelConfiguration config = new RedisSentinelConfiguration("mymaster", new HashSet<String>(Arrays.asList(
|
||||
HOST_AND_PORT_1, HOST_AND_PORT_2, HOST_AND_PORT_3)));
|
||||
|
||||
assertThat(config.getSentinels().size(), is(3));
|
||||
assertThat(config.getSentinels(),
|
||||
hasItems(new RedisNode("127.0.0.1", 123), new RedisNode("localhost", 456), new RedisNode("localhost", 789)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowExecptionOnInvalidHostAndPortString() {
|
||||
new RedisSentinelConfiguration("mymaster", Collections.singleton(HOST_AND_NO_PORT));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowExceptionWhenListOfHostAndPortIsNull() {
|
||||
new RedisSentinelConfiguration("mymaster", Collections.<String> singleton(null));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test
|
||||
public void shouldNotFailWhenListOfHostAndPortIsEmpty() {
|
||||
|
||||
RedisSentinelConfiguration config = new RedisSentinelConfiguration("mymaster", Collections.<String> emptySet());
|
||||
|
||||
assertThat(config.getSentinels().size(), is(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void shouldThrowExceptionGivenNullPropertySource() {
|
||||
new RedisSentinelConfiguration((PropertySource<?>) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test
|
||||
public void shouldNotFailWhenGivenPropertySourceNotContainingRelevantProperties() {
|
||||
|
||||
RedisSentinelConfiguration config = new RedisSentinelConfiguration(new MockPropertySource());
|
||||
|
||||
assertThat(config.getMaster(), nullValue());
|
||||
assertThat(config.getSentinels().size(), is(0));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test
|
||||
public void shouldBeCreatedCorrecltyGivenValidPropertySourceWithMasterAndSingleHostPort() {
|
||||
|
||||
MockPropertySource propertySource = new MockPropertySource();
|
||||
propertySource.setProperty("spring.redis.sentinel.master", "myMaster");
|
||||
propertySource.setProperty("spring.redis.sentinel.nodes", HOST_AND_PORT_1);
|
||||
|
||||
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
|
||||
|
||||
assertThat(config.getMaster().getName(), is("myMaster"));
|
||||
assertThat(config.getSentinels().size(), is(1));
|
||||
assertThat(config.getSentinels(), hasItems(new RedisNode("127.0.0.1", 123)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREDIS-372
|
||||
*/
|
||||
@Test
|
||||
public void shouldBeCreatedCorrecltyGivenValidPropertySourceWithMasterAndMultipleHostPort() {
|
||||
|
||||
MockPropertySource propertySource = new MockPropertySource();
|
||||
propertySource.setProperty("spring.redis.sentinel.master", "myMaster");
|
||||
propertySource.setProperty("spring.redis.sentinel.nodes",
|
||||
StringUtils.collectionToCommaDelimitedString(Arrays.asList(HOST_AND_PORT_1, HOST_AND_PORT_2, HOST_AND_PORT_3)));
|
||||
|
||||
RedisSentinelConfiguration config = new RedisSentinelConfiguration(propertySource);
|
||||
|
||||
assertThat(config.getSentinels().size(), is(3));
|
||||
assertThat(config.getSentinels(),
|
||||
hasItems(new RedisNode("127.0.0.1", 123), new RedisNode("localhost", 456), new RedisNode("localhost", 789)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user