INT-3355 Improve Redis Tests, RedisLockReg.list()

JIRA: https://jira.spring.io/browse/INT-3355

Previously, the RedisAvailableTests unconditionally
flushed Redis; this is not appropriate and could affect
other builds on the CI server (as well as wiping out the
developer's Redis DB).

Change the tests to clean up their own redis data.

Also, improve `RedisLockRegistry.listLocks()` to use mget.
This commit is contained in:
Gary Russell
2014-04-07 09:11:18 -04:00
committed by Artem Bilan
parent 6b81fb1e0c
commit 8a0d3803d9
12 changed files with 236 additions and 53 deletions

View File

@@ -34,7 +34,10 @@ import java.util.concurrent.locks.Lock;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
@@ -93,6 +96,8 @@ public final class RedisLockRegistry implements LockRegistry {
private final LockRegistry localRegistry = new DefaultLockRegistry();
private final LockSerializer lockSerializer = new LockSerializer();
static {
String host;
try {
@@ -176,15 +181,21 @@ public final class RedisLockRegistry implements LockRegistry {
}
public Collection<Lock> listLocks() {
Set<String> keys = this.redisTemplate.keys(this.registryKey + ":*");
List<Lock> locks = new ArrayList<Lock>(keys.size());
for (String key : keys) {
RedisLock lock = this.redisTemplate.boundValueOps(key).get();
if (lock != null) {
locks.add(lock);
return this.redisTemplate.execute(new RedisCallback<Collection<Lock>>() {
@Override
public Collection<Lock> doInRedis(RedisConnection connection) throws DataAccessException {
Set<byte[]> keys = connection.keys((registryKey + ":*").getBytes());
ArrayList<Lock> list = new ArrayList<Lock>(keys.size());
if (keys.size() > 0) {
List<byte[]> locks = connection.mGet(keys.toArray(new byte[keys.size()][]));
for (byte[] lock : locks) {
list.add(lockSerializer.deserialize(lock));
}
}
return list;
}
}
return locks;
});
}
private class RedisLock implements Lock {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -21,21 +21,29 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.support.collections.RedisList;
import org.springframework.data.redis.support.collections.RedisZSet;
import org.springframework.messaging.Message;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.SourcePollingChannelAdapter;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.SubscribableChannel;
/**
* @author Oleg Zhurakousky
* @author Artem Bilan
* @author Gary Russell
* @since 2.2
*/
public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvailableTests {
@@ -59,14 +67,18 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila
message = (Message<RedisList<Object>>) redisChannel.receive(1000);
assertNotNull(message);
assertEquals(13, message.getPayload().size());
this.deletePresidents(jcf);
context.close();
}
@Test
@RedisAvailable
@SuppressWarnings("unchecked")
// syncronization commit renames the list
public void testListInboundConfigurationWithSynchronization() throws Exception{
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
StringRedisTemplate template = this.createStringRedisTemplate(jcf);
template.delete("bar");
this.prepareList(jcf);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("list-inbound-adapter.xml", this.getClass());
SourcePollingChannelAdapter spca = context.getBean("listAdapterWithSynchronization", SourcePollingChannelAdapter.class);
@@ -80,6 +92,44 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila
//poll again, should get nothing since the collection was removed during synchronization
message = (Message<RedisList<Object>>) redisChannel.receive(1000);
assertNull(message);
assertEquals(Long.valueOf(13), template.boundListOps("bar").size());
template.delete("bar");
spca.stop();
context.close();
}
@Test
@RedisAvailable
// syncronization rollback renames the list
public void testListInboundConfigurationWithSynchronizationAndRollback() throws Exception{
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
StringRedisTemplate template = this.createStringRedisTemplate(jcf);
template.delete("baz");
this.prepareList(jcf);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("list-inbound-adapter.xml",
this.getClass());
SubscribableChannel fail = context.getBean("redisFailChannel", SubscribableChannel.class);
final CountDownLatch latch = new CountDownLatch(1);
fail.subscribe(new MessageHandler() {
@Override
public void handleMessage(Message<?> message) throws MessagingException {
latch.countDown();
throw new RuntimeException("Test Rollback");
}
});
SourcePollingChannelAdapter spca = context.getBean("listAdapterWithSynchronizationAndRollback",
SourcePollingChannelAdapter.class);
spca.start();
assertTrue(latch.await(10, TimeUnit.SECONDS));
int n = 0;
while (n++ < 100 && template.keys("baz").size() == 0) {
Thread.sleep(100);
}
assertTrue("Rename didn't occcur", n < 100);
assertEquals(Long.valueOf(13), template.boundListOps("baz").size());
template.delete("baz");
spca.stop();
context.close();
@@ -88,8 +138,11 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila
@Test
@RedisAvailable
@SuppressWarnings("unchecked")
// syncronization commit renames the list
public void testListInboundConfigurationWithSynchronizationAndTemplate() throws Exception{
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
StringRedisTemplate template = this.createStringRedisTemplate(jcf);
template.delete("bar");
this.prepareList(jcf);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("list-inbound-adapter.xml", this.getClass());
SourcePollingChannelAdapter spca = context.getBean("listAdapterWithSynchronizationAndRedisTemplate", SourcePollingChannelAdapter.class);
@@ -103,6 +156,8 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila
//poll again, should get nothing since the collection was removed during synchronization
message = (Message<RedisList<Object>>) redisChannel.receive(1000);
assertNull(message);
assertEquals(Long.valueOf(13), template.boundListOps("bar").size());
template.delete("bar");
spca.stop();
context.close();
@@ -201,6 +256,7 @@ public class RedisStoreInboundChannelAdapterIntegrationTests extends RedisAvaila
zsetAdapterNoScore.stop();
zsetAdapterWithSingleScoreAndSynchronization.stop();
this.deletePresidents(jcf);
context.close();
}

View File

@@ -49,7 +49,7 @@
</int-redis:store-inbound-channel-adapter>
<int:transaction-synchronization-factory id="syncFactory">
<int:after-commit expression="#resource.attributes['store'].rename('bar')"/>
<int:after-commit expression="#store.rename('bar')"/>
<int:after-rollback expression="#store.rename('baz')"/>
</int:transaction-synchronization-factory>

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors
* Copyright 2013-2014 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.
@@ -19,6 +19,8 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.redis.connection.RedisConnectionFactory;
@@ -30,11 +32,18 @@ import org.springframework.integration.redis.rules.RedisAvailableTests;
/**
* @author Gunnar Hillert
* @author Artem Bilan
* @author Gary Russell
* @since 3.0
*
*/
public class RedisMetadataStoreTests extends RedisAvailableTests {
@Before
@After
public void setUpTearDown() {
this.createStringRedisTemplate(this.getConnectionFactoryForTest()).delete("testMetadata");
}
@Test
@RedisAvailable
public void testGetNonExistingKeyValue() {
@@ -48,11 +57,11 @@ public class RedisMetadataStoreTests extends RedisAvailableTests {
@RedisAvailable
public void testPersistKeyValue() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "foo");
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata");
metadataStore.put("RedisMetadataStoreTests-Spring", "Integration");
StringRedisTemplate redisTemplate = new StringRedisTemplate(jcf);
BoundHashOperations<String,Object,Object> ops = redisTemplate.boundHashOps("foo");
BoundHashOperations<String,Object,Object> ops = redisTemplate.boundHashOps("testMetadata");
assertEquals("Integration", ops.get("RedisMetadataStoreTests-Spring"));
}
@@ -62,7 +71,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests {
public void testGetValueFromMetadataStore() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata");
metadataStore.put("RedisMetadataStoreTests-GetValue", "Hello Redis");
String retrievedValue = metadataStore.get("RedisMetadataStoreTests-GetValue");
@@ -74,7 +83,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests {
public void testPersistEmptyStringToMetadataStore() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata");
metadataStore.put("RedisMetadataStoreTests-PersistEmpty", "");
String retrievedValue = metadataStore.get("RedisMetadataStoreTests-PersistEmpty");
@@ -86,7 +95,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests {
public void testPersistNullStringToMetadataStore() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata");
try {
metadataStore.put("RedisMetadataStoreTests-PersistEmpty", null);
@@ -104,7 +113,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests {
@RedisAvailable
public void testPersistWithEmptyKeyToMetadataStore() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata");
metadataStore.put("", "PersistWithEmptyKey");
String retrievedValue = metadataStore.get("");
@@ -115,7 +124,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests {
@RedisAvailable
public void testPersistWithNullKeyToMetadataStore() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata");
try {
metadataStore.put(null, "something");
@@ -132,7 +141,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests {
@RedisAvailable
public void testGetValueWithNullKeyFromMetadataStore() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata");
try {
metadataStore.get(null);
@@ -149,7 +158,7 @@ public class RedisMetadataStoreTests extends RedisAvailableTests {
@RedisAvailable
public void testRemoveFromMetadataStore() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf);
RedisMetadataStore metadataStore = new RedisMetadataStore(jcf, "testMetadata");
String testKey = "RedisMetadataStoreTests-Remove";
String testValue = "Integration";

View File

@@ -47,6 +47,7 @@ import com.lambdaworks.redis.protocol.CommandType;
/**
* @author Artem Bilan
* @author Gary Russell
* @since 4.0
*/
@ContextConfiguration
@@ -121,6 +122,7 @@ public class RedisOutboundGatewayTests extends RedisAvailableTests {
receive = this.replyChannel.receive(1000);
assertNotNull(receive);
assertEquals("11", new String((byte[]) receive.getPayload()));
this.createStringRedisTemplate(this.getConnectionFactoryForTest()).delete("si.test.RedisAtomicInteger");
}
@Test
@@ -163,6 +165,7 @@ public class RedisOutboundGatewayTests extends RedisAvailableTests {
Message<?> receive = this.replyChannel.receive(1000);
assertNotNull(receive);
assertThat((List<byte[]>) receive.getPayload(), Matchers.contains(value1, value2));
connection.del("foo1".getBytes(), "foo2".getBytes());
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2007-2013 the original author or authors
* Copyright 2007-2014 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.
@@ -44,18 +44,19 @@ import org.springframework.data.redis.support.collections.RedisSet;
import org.springframework.data.redis.support.collections.RedisZSet;
import org.springframework.expression.common.LiteralExpression;
import org.springframework.expression.spel.standard.SpelExpression;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandlingException;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.integration.redis.support.RedisHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.test.util.TestUtils;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandlingException;
/**
* @author Oleg Zhurakousky
* @author Mark Fisher
* @author Gary Russell
* @since 2.2
*/
public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvailableTests {
@@ -65,6 +66,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
public void testListWithKeyAsHeader(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisList<String> redisList =
new DefaultRedisList<String>("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisList.size());
@@ -79,6 +81,8 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
redisChannel.send(message);
assertEquals(3, redisList.size());
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test
@@ -98,12 +102,14 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals(1, redisList.size());
redisTemplate.delete("foo");
context.close();
}
@Test
@RedisAvailable
public void testListWithProvidedKey(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisList<String> redisList =
new DefaultRedisList<String>("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisList.size());
@@ -118,6 +124,8 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
redisChannel.send(message);
assertEquals(3, redisList.size());
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test
@@ -143,6 +151,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals(1, redisZSet.size());
assertEquals(Double.valueOf(2), redisZSet.score("bar"));
redisTemplate.delete("foo");
context.close();
}
@Test
@@ -171,6 +180,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals(1, redisZSet.size());
assertEquals(Double.valueOf(1), redisZSet.score("bar"));
redisTemplate.delete("foo");
context.close();
}
@Test
@@ -199,6 +209,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals(1, redisZSet.size());
assertEquals(Double.valueOf(4), redisZSet.score("bar"));
redisTemplate.delete("foo");
context.close();
}
@Test
@@ -228,12 +239,14 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals(1, redisZSet.size());
assertEquals(Double.valueOf(15), redisZSet.score("bar"));
redisTemplate.delete("foo");
context.close();
}
@Test
@RedisAvailable
public void testMapToZsetWithProvidedKey(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deletePresidents(jcf);
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>("presidents", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisZset.size());
@@ -274,12 +287,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals(1, redisZset.rangeByScore(18, 18).size());
assertEquals(4, redisZset.rangeByScore(18, 19).size());
assertEquals(1, redisZset.rangeByScore(31, 31).size());
this.deletePresidents(jcf);
context.close();
}
@Test
@RedisAvailable
public void testMapToMapWithProvidedKey(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisMap<String, String> redisMap =
new DefaultRedisMap<String, String>("pepboys",
this.initTemplate(jcf, new StringRedisTemplate()));
@@ -303,12 +319,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
RedisStoreWritingMessageHandler.class);
assertEquals("pepboys", TestUtils.getPropertyValue(handler, "keyExpression", LiteralExpression.class).getExpressionString());
assertEquals("'foo'", TestUtils.getPropertyValue(handler, "mapKeyExpression", SpelExpression.class).getExpressionString());
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test(expected=MessageHandlingException.class) // map key is not provided
@RedisAvailable
public void testMapToMapAsSingleEntryWithKeyAsHeaderFail(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisMap<String, Map<String, String>> redisMap =
new DefaultRedisMap<String, Map<String, String>>("pepboys",
this.initTemplate(jcf, new RedisTemplate<String, Map<String, Map<String, String>>>()));
@@ -325,12 +344,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).
setHeader(RedisHeaders.KEY, "pepboys").build();
redisChannel.send(message);
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test(expected=MessageHandlingException.class) // key is not provided
@RedisAvailable
public void testMapToMapNoKey(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisTemplate<String, Map<String, Map<String, String>>> redisTemplate = new RedisTemplate<String, Map<String, Map<String, String>>>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
@@ -349,12 +371,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
Message<Map<String, String>> message = MessageBuilder.withPayload(pepboys).build();
redisChannel.send(message);
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test
@RedisAvailable
public void testMapToMapAsSingleEntryWithKeyAsHeader(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisTemplate<String, Map<String, Map<String, String>>> redisTemplate = new RedisTemplate<String, Map<String, Map<String, String>>>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
@@ -379,12 +404,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals("Manny", pepboyz.get("1"));
assertEquals("Moe", pepboyz.get("2"));
assertEquals("Jack", pepboyz.get("3"));
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test
@RedisAvailable
public void testStoreSimpleStringInMap(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "bar");
StringRedisTemplate redisTemplate = new StringRedisTemplate();
RedisMap<String, String> redisMap =
new DefaultRedisMap<String, String>("bar",
@@ -401,12 +429,14 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
String hello = redisMap.get("foo");
assertEquals("hello, world!", hello);
context.close();
}
@Test
@RedisAvailable
public void testSetWithKeyAsHeader(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisSet<String> redisSet =
new DefaultRedisSet<String>("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisSet.size());
@@ -421,6 +451,8 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
redisChannel.send(message);
assertEquals(3, redisSet.size());
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test
@@ -440,12 +472,14 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals(1, redisSet.size());
redisTemplate.delete("foo");
context.close();
}
@Test
@RedisAvailable
public void testSetWithKeyAsHeaderNotParsed(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
@@ -463,12 +497,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
redisChannel.send(message);
assertEquals(1, redisSet.size());
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test
@RedisAvailable
public void testPojoIntoSet(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisSet<String> redisSet =
new DefaultRedisSet<String>("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
assertEquals(0, redisSet.size());
@@ -480,12 +517,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
redisChannel.send(message);
assertEquals(1, redisSet.size());
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test
@RedisAvailable
public void testProperties(){
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "pepboys");
RedisProperties redisProperties =
new RedisProperties("pepboys", this.initTemplate(jcf, new StringRedisTemplate()));
@@ -503,6 +543,8 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals("Manny", redisProperties.get("1"));
assertEquals("Moe", redisProperties.get("2"));
assertEquals("Jack", redisProperties.get("3"));
this.deleteKey(jcf, "pepboys");
context.close();
}
@Test
@@ -525,6 +567,7 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail
assertEquals("bar", redisProperties.get("qux"));
redisTemplate.delete("foo");
context.close();
}
private <K,V> RedisTemplate<K,V> initTemplate(RedisConnectionFactory rcf, RedisTemplate<K,V> redisTemplate){

View File

@@ -35,6 +35,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.collections.DefaultRedisList;
import org.springframework.data.redis.support.collections.DefaultRedisZSet;
import org.springframework.data.redis.support.collections.RedisCollectionFactoryBean.CollectionType;
@@ -61,6 +62,7 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
@RedisAvailable
public void testListWithListPayloadParsedAndProvidedKey() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisList<String> redisList =
new DefaultRedisList<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
@@ -84,12 +86,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
assertEquals("Manny", redisList.get(0));
assertEquals("Moe", redisList.get(1));
assertEquals("Jack", redisList.get(2));
this.deleteKey(jcf, "foo");
}
@Test
@RedisAvailable
public void testListWithListPayloadParsedAndProvidedKeyAsHeader() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisList<String> redisList =
new DefaultRedisList<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
@@ -113,12 +117,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
assertEquals("Manny", redisList.get(0));
assertEquals("Moe", redisList.get(1));
assertEquals("Jack", redisList.get(2));
this.deleteKey(jcf, "foo");
}
@RedisAvailable
@Test(expected=MessageHandlingException.class)
public void testListWithListPayloadParsedAndNoKey() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisList<String> redisList =
new DefaultRedisList<String>(key, this.initTemplate(jcf, new RedisTemplate<String, String>()));
@@ -136,12 +142,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
list.add("Jack");
Message<List<String>> message = MessageBuilder.withPayload(list).build();
handler.handleMessage(message);
this.deleteKey(jcf, "foo");
}
@Test
@RedisAvailable
public void testListWithListPayloadAsSingleEntry() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisList<List<String>> redisList =
new DefaultRedisList<List<String>>(key, this.initTemplate(jcf, new RedisTemplate<String, List<String>>()));
@@ -168,12 +176,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
assertEquals("Manny", resultList.get(0));
assertEquals("Moe", resultList.get(1));
assertEquals("Jack", resultList.get(2));
this.deleteKey(jcf, "foo");
}
@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyDefault() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
@@ -207,12 +217,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
for (TypedTuple<String> pepboy : pepboys) {
assertEquals(Double.valueOf(2), pepboy.getScore());
}
this.deleteKey(jcf, "foo");
}
@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrement() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
@@ -249,12 +261,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
for (TypedTuple<String> pepboy : pepboys) {
assertTrue(pepboy.getScore() == 2);
}
this.deleteKey(jcf, "foo");
}
@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrementAsStringHeader() {// see INT-2775
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
@@ -291,12 +305,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
for (TypedTuple<String> pepboy : pepboys) {
assertTrue(pepboy.getScore() == 2);
}
this.deleteKey(jcf, "foo");
}
@Test
@RedisAvailable
public void testZsetWithListPayloadAsSingleEntryAndHeaderKeyHeaderScore() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisZSet<List<String>> redisZset =
new DefaultRedisZSet<List<String>>(key, this.initTemplate(jcf, new RedisTemplate<String, List<String>>()));
@@ -325,12 +341,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
for (TypedTuple<List<String>> pepboys : entries) {
assertTrue(pepboys.getScore() == 4);
}
this.deleteKey(jcf, "foo");
}
@Test
@RedisAvailable
public void testZsetWithMapPayloadParsedHeaderKey() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deletePresidents(jcf);
String key = "presidents";
RedisZSet<String> redisZset =
new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
@@ -368,12 +386,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
Set<TypedTuple<String>> entries = redisZset.rangeByScoreWithScores(18, 19);
assertEquals(6, entries.size());
this.deletePresidents(jcf);
}
@Test
@RedisAvailable
public void testZsetWithMapPayloadPojoParsedHeaderKey() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deletePresidents(jcf);
String key = "presidents";
RedisZSet<President> redisZset =
new DefaultRedisZSet<President>(key, this.initTemplate(jcf, new RedisTemplate<String, President>()));
@@ -412,12 +432,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
Set<TypedTuple<President>> entries = redisZset.rangeByScoreWithScores(18, 19);
assertEquals(6, entries.size());
this.deletePresidents(jcf);
}
@Test
@RedisAvailable
public void testZsetWithMapPayloadPojoAsSingleEntryHeaderKey() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deletePresidents(jcf);
String key = "presidents";
RedisZSet<Map<President, Double>> redisZset =
new DefaultRedisZSet<Map<President, Double>>(key, this.initTemplate(jcf, new RedisTemplate<String, Map<President, Double>>()));
@@ -443,6 +465,7 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
handler.handleMessage(message);
assertEquals(1, redisZset.size());
this.deletePresidents(jcf);
}
@Test(expected=IllegalStateException.class)
@@ -490,6 +513,7 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
@RedisAvailable
public void testMapWithMapKeyExpression() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisStoreWritingMessageHandler handler =
new RedisStoreWritingMessageHandler(jcf);
@@ -503,12 +527,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
catch (Exception e) {
fail("No exception expected:" + e.getMessage());
}
this.deleteKey(jcf, "foo");
}
@Test
@RedisAvailable
public void testPropertiesWithMapKeyExpression() {
RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
this.deleteKey(jcf, "foo");
String key = "foo";
RedisStoreWritingMessageHandler handler =
new RedisStoreWritingMessageHandler(jcf);
@@ -522,10 +548,12 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{
catch (Exception e) {
fail("No exception expected:" + e.getMessage());
}
this.deleteKey(jcf, "foo");
}
private <K,V> RedisTemplate<K,V> initTemplate(RedisConnectionFactory rcf, RedisTemplate<K,V> redisTemplate) {
redisTemplate.setConnectionFactory(rcf);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}

View File

@@ -17,18 +17,13 @@ package org.springframework.integration.redis.rules;
import static org.junit.Assert.assertTrue;
import java.util.UUID;
import org.junit.Rule;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.integration.test.util.TestUtils;
@@ -44,20 +39,14 @@ public class RedisAvailableTests {
@Rule
public RedisAvailableRule redisAvailableRule = new RedisAvailableRule();
protected RedisConnectionFactory getConnectionFactoryForTest() {
LettuceConnectionFactory connectionFactory = RedisAvailableRule.connectionFactoryResource.get();
RedisTemplate<UUID, Object> rt = new RedisTemplate<UUID, Object>();
rt.setConnectionFactory(connectionFactory);
rt.afterPropertiesSet();
rt.execute(new RedisCallback<Object>() {
private RedisConnectionFactory connectionFactory;
@Override
public Object doInRedis(RedisConnection connection)
throws DataAccessException {
connection.flushDb();
return null;
}
});
protected RedisConnectionFactory getConnectionFactoryForTest() {
if (this.connectionFactory != null) {
return this.connectionFactory;
}
LettuceConnectionFactory connectionFactory = RedisAvailableRule.connectionFactoryResource.get();
this.connectionFactory = connectionFactory;
return connectionFactory;
}
@@ -90,9 +79,8 @@ public class RedisAvailableTests {
protected void prepareList(RedisConnectionFactory connectionFactory){
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.afterPropertiesSet();
StringRedisTemplate redisTemplate = createStringRedisTemplate(connectionFactory);
redisTemplate.delete("presidents");
BoundListOperations<String, String> ops = redisTemplate.boundListOps("presidents");
ops.rightPush("John Adams");
@@ -114,10 +102,9 @@ public class RedisAvailableTests {
protected void prepareZset(RedisConnectionFactory connectionFactory){
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.afterPropertiesSet();
StringRedisTemplate redisTemplate = createStringRedisTemplate(connectionFactory);
redisTemplate.delete("presidents");
BoundZSetOperations<String, String> ops = redisTemplate.boundZSetOps("presidents");
ops.add("John Adams", 18);
@@ -134,7 +121,23 @@ public class RedisAvailableTests {
ops.add("Ronald Reagan", 20);
ops.add("William J. Clinton", 20);
ops.add("Abraham Lincoln", 19);
ops.add("George Washington", 18);
ops.add("George Wahington", 18);
}
protected void deletePresidents(RedisConnectionFactory connectionFactory){
this.deleteKey(connectionFactory, "presidents");
}
protected void deleteKey(RedisConnectionFactory connectionFactory, String key) {
StringRedisTemplate redisTemplate = createStringRedisTemplate(connectionFactory);
redisTemplate.delete(key);
}
protected StringRedisTemplate createStringRedisTemplate(RedisConnectionFactory connectionFactory) {
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(connectionFactory);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 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
@@ -21,6 +21,7 @@ import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
@@ -44,6 +45,7 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
/**
* @author Artem Bilan
* @author Gary Russell
* @since 3.0
*/
public class DelayerHandlerRescheduleIntegrationTests extends RedisAvailableTests {
@@ -62,6 +64,7 @@ public class DelayerHandlerRescheduleIntegrationTests extends RedisAvailableTest
connectionFactory.afterPropertiesSet();
}
@AfterClass
public static void tearDown() {
connectionFactory.destroy();
}
@@ -127,6 +130,8 @@ public class DelayerHandlerRescheduleIntegrationTests extends RedisAvailableTest
assertEquals(1, messageStore.getMessageGroupCount());
assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId));
messageStore.removeMessageGroup(delayerMessageGroupId);
}
}

View File

@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -65,7 +66,8 @@ public class RedisChannelMessageStoreTests extends RedisAvailableTests {
private RedisChannelMessageStore priorityCms;
@Before
public void setup() {
@After
public void setUpTearDown() {
this.cms.removeMessageGroup("cms:testChannel1");
this.cms.removeMessageGroup("cms:testChannel2");
this.priorityCms.removeMessageGroup("priorityCms:testChannel3");

View File

@@ -31,11 +31,14 @@ import java.util.concurrent.TimeUnit;
import junit.framework.AssertionFailedError;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.history.MessageHistory;
@@ -51,10 +54,20 @@ import org.springframework.messaging.support.GenericMessage;
/**
* @author Oleg Zhurakousky
* @author Artem Bilan
* @author Gary Russell
*
*/
public class RedisMessageGroupStoreTests extends RedisAvailableTests {
@Before
@After
public void setUpTearDown() {
StringRedisTemplate template = this.createStringRedisTemplate(this.getConnectionFactoryForTest());
template.delete("MESSAGE_GROUP_1");
template.delete("MESSAGE_GROUP_2");
template.delete("MESSAGE_GROUP_3");
}
@Test
@RedisAvailable
public void testNonExistingEmptyMessageGroup() throws Exception{

View File

@@ -24,15 +24,18 @@ import java.io.Serializable;
import java.util.Properties;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.history.MessageHistory;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.integration.redis.rules.RedisAvailable;
import org.springframework.integration.redis.rules.RedisAvailableTests;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.GenericMessage;
/**
* @author Oleg Zhurakousky
@@ -40,6 +43,13 @@ import org.springframework.messaging.Message;
*/
public class RedisMessageStoreTests extends RedisAvailableTests {
@Before
@After
public void setUpTearDown() {
StringRedisTemplate template = this.createStringRedisTemplate(this.getConnectionFactoryForTest());
template.delete(template.keys("MESSAGE_*"));
}
@Test
@RedisAvailable
public void testGetNonExistingMessage(){