From 8a0d3803d9584a1a2cb20f94c603d30a1b5a367d Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 7 Apr 2014 09:11:18 -0400 Subject: [PATCH] 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. --- .../redis/util/RedisLockRegistry.java | 27 ++++++--- ...InboundChannelAdapterIntegrationTests.java | 60 ++++++++++++++++++- .../redis/inbound/list-inbound-adapter.xml | 2 +- .../metadata/RedisMetadataStoreTests.java | 29 +++++---- .../outbound/RedisOutboundGatewayTests.java | 3 + ...utboundChannelAdapterIntegrationTests.java | 51 ++++++++++++++-- .../RedisStoreWritingMessageHandlerTests.java | 28 +++++++++ .../redis/rules/RedisAvailableTests.java | 53 ++++++++-------- ...ayerHandlerRescheduleIntegrationTests.java | 7 ++- .../store/RedisChannelMessageStoreTests.java | 4 +- .../store/RedisMessageGroupStoreTests.java | 13 ++++ .../redis/store/RedisMessageStoreTests.java | 12 +++- 12 files changed, 236 insertions(+), 53 deletions(-) diff --git a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java index 40215f3f57..909f95d4c4 100644 --- a/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java +++ b/spring-integration-redis/src/main/java/org/springframework/integration/redis/util/RedisLockRegistry.java @@ -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 listLocks() { - Set keys = this.redisTemplate.keys(this.registryKey + ":*"); - List locks = new ArrayList(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>() { + + @Override + public Collection doInRedis(RedisConnection connection) throws DataAccessException { + Set keys = connection.keys((registryKey + ":*").getBytes()); + ArrayList list = new ArrayList(keys.size()); + if (keys.size() > 0) { + List 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 { diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java index bf80cfe856..18c94b5831 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/RedisStoreInboundChannelAdapterIntegrationTests.java @@ -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>) 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>) 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>) 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(); } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/list-inbound-adapter.xml b/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/list-inbound-adapter.xml index b40c91700f..9027b98590 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/list-inbound-adapter.xml +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/inbound/list-inbound-adapter.xml @@ -49,7 +49,7 @@ - + diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java index f08856f77e..9e53d63c89 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/metadata/RedisMetadataStoreTests.java @@ -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 ops = redisTemplate.boundHashOps("foo"); + BoundHashOperations 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"; diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisOutboundGatewayTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisOutboundGatewayTests.java index da06b4bf28..7085e9aa71 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisOutboundGatewayTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisOutboundGatewayTests.java @@ -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) receive.getPayload(), Matchers.contains(value1, value2)); + connection.del("foo1".getBytes(), "foo2".getBytes()); } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java index 76e1f442bd..7865ad2e5b 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreOutboundChannelAdapterIntegrationTests.java @@ -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 redisList = new DefaultRedisList("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 redisList = new DefaultRedisList("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 redisZset = new DefaultRedisZSet("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 redisMap = new DefaultRedisMap("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> redisMap = new DefaultRedisMap>("pepboys", this.initTemplate(jcf, new RedisTemplate>>())); @@ -325,12 +344,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail Message> 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>> redisTemplate = new RedisTemplate>>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); @@ -349,12 +371,15 @@ public class RedisStoreOutboundChannelAdapterIntegrationTests extends RedisAvail Message> 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>> redisTemplate = new RedisTemplate>>(); 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 redisMap = new DefaultRedisMap("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 redisSet = new DefaultRedisSet("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 redisTemplate = new RedisTemplate(); 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 redisSet = new DefaultRedisSet("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 RedisTemplate initTemplate(RedisConnectionFactory rcf, RedisTemplate redisTemplate){ diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreWritingMessageHandlerTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreWritingMessageHandlerTests.java index b37df5b7e5..5bd662b8b6 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreWritingMessageHandlerTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/outbound/RedisStoreWritingMessageHandlerTests.java @@ -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 redisList = new DefaultRedisList(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 redisList = new DefaultRedisList(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 redisList = new DefaultRedisList(key, this.initTemplate(jcf, new RedisTemplate())); @@ -136,12 +142,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ list.add("Jack"); Message> 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> redisList = new DefaultRedisList>(key, this.initTemplate(jcf, new RedisTemplate>())); @@ -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 redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -207,12 +217,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ for (TypedTuple 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 redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -249,12 +261,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ for (TypedTuple 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 redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -291,12 +305,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ for (TypedTuple 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> redisZset = new DefaultRedisZSet>(key, this.initTemplate(jcf, new RedisTemplate>())); @@ -325,12 +341,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ for (TypedTuple> 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 redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new StringRedisTemplate())); @@ -368,12 +386,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ Set> 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 redisZset = new DefaultRedisZSet(key, this.initTemplate(jcf, new RedisTemplate())); @@ -412,12 +432,14 @@ public class RedisStoreWritingMessageHandlerTests extends RedisAvailableTests{ Set> 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> redisZset = new DefaultRedisZSet>(key, this.initTemplate(jcf, new RedisTemplate>())); @@ -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 RedisTemplate initTemplate(RedisConnectionFactory rcf, RedisTemplate redisTemplate) { redisTemplate.setConnectionFactory(rcf); + redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java index 6a82c43e63..0711ae1bce 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/rules/RedisAvailableTests.java @@ -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 rt = new RedisTemplate(); - rt.setConnectionFactory(connectionFactory); - rt.afterPropertiesSet(); - rt.execute(new RedisCallback() { + 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 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 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; } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java index 7a8653a78b..e16ed98ad7 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/DelayerHandlerRescheduleIntegrationTests.java @@ -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); + } } diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java index 78852eb290..4646ea5458 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisChannelMessageStoreTests.java @@ -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"); diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java index 53e542a575..394b6569f8 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageGroupStoreTests.java @@ -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{ diff --git a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java index 7a4e6e3237..135b97dfb8 100644 --- a/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java +++ b/spring-integration-redis/src/test/java/org/springframework/integration/redis/store/RedisMessageStoreTests.java @@ -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(){