From b518fa16bfa40b7d62ae8d5513e6c06954b7509d Mon Sep 17 00:00:00 2001 From: Jennifer Hickey Date: Tue, 19 Mar 2013 17:09:20 -0700 Subject: [PATCH] Modify connection tests to reuse connection factories - Use Spring integration test support to instantiate ConnectionFactory once per instance of test class (instead of per method) - Minimizes number of connection factories and thus connections in pooled scenarios --- .../AbstractConnectionIntegrationTests.java | 28 ++++++--------- .../JedisConnectionIntegrationTests.java | 35 ++++++++++--------- .../JRedisConnectionIntegrationTests.java | 33 ++++++++--------- .../LettuceConnectionIntegrationTests.java | 22 +++--------- .../rjc/RjcConnectionIntegrationTests.java | 23 +++--------- .../srp/SrpConnectionIntegrationTests.java | 21 +++-------- ...edisConnectionIntegrationTests-context.xml | 24 +++++++++++++ ...edisConnectionIntegrationTests-context.xml | 22 ++++++++++++ ...tuceConnectionIntegrationTests-context.xml | 16 +++++++++ .../RjcConnectionIntegrationTests-context.xml | 18 ++++++++++ .../SrpConnectionIntegrationTests-context.xml | 18 ++++++++++ 11 files changed, 158 insertions(+), 102 deletions(-) create mode 100644 src/test/resources/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests-context.xml create mode 100644 src/test/resources/org/springframework/data/redis/connection/jredis/JredisConnectionIntegrationTests-context.xml create mode 100644 src/test/resources/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests-context.xml create mode 100644 src/test/resources/org/springframework/data/redis/connection/rjc/RjcConnectionIntegrationTests-context.xml create mode 100644 src/test/resources/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests-context.xml diff --git a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java index fb32a12c4..4d8b25877 100644 --- a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionIntegrationTests.java @@ -32,12 +32,11 @@ import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.TimeUnit; import org.junit.After; -import org.junit.AfterClass; import org.junit.Before; import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.Address; -import org.springframework.data.redis.ConnectionFactoryTracker; import org.springframework.data.redis.Person; import org.springframework.data.redis.connection.SortParameters.Order; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; @@ -63,22 +62,15 @@ public abstract class AbstractConnectionIntegrationTests { private static final String listName = "test-list"; private static final byte[] EMPTY_ARRAY = new byte[0]; - protected abstract RedisConnectionFactory getConnectionFactory(); + @Autowired + private RedisConnectionFactory connectionFactory; @Before public void setUp() { - connection = new DefaultStringRedisConnection(getConnectionFactory().getConnection()); - ConnectionFactoryTracker.add(getConnectionFactory()); - + connection = new DefaultStringRedisConnection(connectionFactory.getConnection()); } - @AfterClass - public static void cleanUp() { - ConnectionFactoryTracker.cleanUp(); - } - - @After public void tearDown() { connection.close(); @@ -197,7 +189,7 @@ public abstract class AbstractConnectionIntegrationTests { assertNull(mGet.get(0)); assertNull(mGet.get(1)); - StringRedisTemplate stringTemplate = new StringRedisTemplate(getConnectionFactory()); + StringRedisTemplate stringTemplate = new StringRedisTemplate(connectionFactory); List multiGet = stringTemplate.opsForValue().multiGet(Arrays.asList(keys)); assertEquals(2, multiGet.size()); assertNull(multiGet.get(0)); @@ -235,7 +227,7 @@ public abstract class AbstractConnectionIntegrationTests { } // open a new connection - RedisConnection connection2 = getConnectionFactory().getConnection(); + RedisConnection connection2 = connectionFactory.getConnection(); connection2.publish(expectedChannel.getBytes(), expectedMessage.getBytes()); connection2.close(); // In some clients, unsubscribe happens async of message receipt, so not all @@ -284,7 +276,7 @@ public abstract class AbstractConnectionIntegrationTests { } // open a new connection - RedisConnection connection2 = getConnectionFactory().getConnection(); + RedisConnection connection2 = connectionFactory.getConnection(); connection2.publish("channel1".getBytes(), expectedMessage.getBytes()); connection2.publish("channel2".getBytes(), expectedMessage.getBytes()); connection2.close(); @@ -426,11 +418,11 @@ public abstract class AbstractConnectionIntegrationTests { } private boolean isAsync() { - return (getConnectionFactory() instanceof LettuceConnectionFactory) || - (getConnectionFactory() instanceof SrpConnectionFactory); + return (connectionFactory instanceof LettuceConnectionFactory) || + (connectionFactory instanceof SrpConnectionFactory); } private boolean isRjc() { - return (getConnectionFactory() instanceof RjcConnectionFactory); + return (connectionFactory instanceof RjcConnectionFactory); } } \ No newline at end of file diff --git a/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java index f712657b5..a98503305 100644 --- a/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests.java @@ -17,28 +17,29 @@ package org.springframework.data.redis.connection.jedis; import static org.junit.Assert.assertEquals; + +import org.junit.After; import org.junit.Test; -import org.springframework.data.redis.SettingsUtils; +import org.junit.runner.RunWith; +import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests; -import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrationTests { - - JedisConnectionFactory factory; - - public JedisConnectionIntegrationTests() { - factory = new JedisConnectionFactory(); - factory.setUsePool(true); - - factory.setPort(SettingsUtils.getPort()); - factory.setHostName(SettingsUtils.getHost()); - - factory.afterPropertiesSet(); - } - - protected RedisConnectionFactory getConnectionFactory() { - return factory; + @After + public void tearDown() { + try { + connection.close(); + }catch(DataAccessException e) { + // Jedis leaves some incomplete data in OutputStream on NPE caused by null key/value tests + // Attempting to close the connection will result in error on sending QUIT to Redis + System.out.println("Connection already closed"); + } + connection = null; } @Test diff --git a/src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionIntegrationTests.java index 059c3fa70..7be632cd6 100644 --- a/src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionIntegrationTests.java @@ -16,28 +16,29 @@ package org.springframework.data.redis.connection.jredis; +import org.junit.After; import org.junit.Ignore; import org.junit.Test; -import org.springframework.data.redis.SettingsUtils; +import org.junit.runner.RunWith; +import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests; -import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration public class JRedisConnectionIntegrationTests extends AbstractConnectionIntegrationTests { - - JredisConnectionFactory factory; - - public JRedisConnectionIntegrationTests() { - factory = new JredisConnectionFactory(); - factory.setPort(SettingsUtils.getPort()); - factory.setHostName(SettingsUtils.getHost()); - - factory.setUsePool(true); - factory.afterPropertiesSet(); - } - - protected RedisConnectionFactory getConnectionFactory() { - return factory; + @After + public void tearDown() { + try { + connection.close(); + }catch(DataAccessException e) { + // Jredis closes a connection on Exception (which some tests intentionally throw) + // Attempting to close the connection again will result in error + System.out.println("Connection already closed"); + } + connection = null; } @Ignore("JRedis does not support pipelining") diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java index 421f5d33a..8743f0284 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests.java @@ -22,27 +22,15 @@ import static org.junit.Assert.assertNull; import java.util.List; import org.junit.Test; -import org.springframework.data.redis.SettingsUtils; +import org.junit.runner.RunWith; import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests; -import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration public class LettuceConnectionIntegrationTests extends AbstractConnectionIntegrationTests { - LettuceConnectionFactory factory; - - public LettuceConnectionIntegrationTests() { - factory = new LettuceConnectionFactory(); - - factory.setPort(SettingsUtils.getPort()); - factory.setHostName(SettingsUtils.getHost()); - - factory.afterPropertiesSet(); - } - - - protected RedisConnectionFactory getConnectionFactory() { - return factory; - } @Test public void testMultiExec() throws Exception { diff --git a/src/test/java/org/springframework/data/redis/connection/rjc/RjcConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/rjc/RjcConnectionIntegrationTests.java index e19b93696..c80402f38 100644 --- a/src/test/java/org/springframework/data/redis/connection/rjc/RjcConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/rjc/RjcConnectionIntegrationTests.java @@ -23,31 +23,18 @@ import java.util.UUID; import org.junit.Ignore; import org.junit.Test; -import org.springframework.data.redis.SettingsUtils; +import org.junit.runner.RunWith; import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests; -import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Costin Leau */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration public class RjcConnectionIntegrationTests extends AbstractConnectionIntegrationTests { - RjcConnectionFactory factory; - - public RjcConnectionIntegrationTests() { - factory = new RjcConnectionFactory(); - factory.setPort(SettingsUtils.getPort()); - factory.setHostName(SettingsUtils.getHost()); - - factory.setUsePool(true); - factory.afterPropertiesSet(); - } - - - protected RedisConnectionFactory getConnectionFactory() { - return factory; - } - @Test public void testMultiExec() throws Exception { byte[] key = "key".getBytes(); diff --git a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests.java b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests.java index 18e516567..f2708903f 100644 --- a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests.java @@ -17,28 +17,17 @@ package org.springframework.data.redis.connection.srp; import org.junit.Ignore; -import org.springframework.data.redis.SettingsUtils; +import org.junit.runner.RunWith; import org.springframework.data.redis.connection.AbstractConnectionIntegrationTests; -import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * @author Costin Leau */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration public class SrpConnectionIntegrationTests extends AbstractConnectionIntegrationTests { - SrpConnectionFactory factory; - - public SrpConnectionIntegrationTests() { - factory = new SrpConnectionFactory(); - factory.setPort(SettingsUtils.getPort()); - factory.setHostName(SettingsUtils.getHost()); - - factory.afterPropertiesSet(); - } - - - protected RedisConnectionFactory getConnectionFactory() { - return factory; - } @Ignore public void testNullCollections() throws Exception { diff --git a/src/test/resources/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests-context.xml b/src/test/resources/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests-context.xml new file mode 100644 index 000000000..0138acddf --- /dev/null +++ b/src/test/resources/org/springframework/data/redis/connection/jedis/JedisConnectionIntegrationTests-context.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/org/springframework/data/redis/connection/jredis/JredisConnectionIntegrationTests-context.xml b/src/test/resources/org/springframework/data/redis/connection/jredis/JredisConnectionIntegrationTests-context.xml new file mode 100644 index 000000000..96aea6242 --- /dev/null +++ b/src/test/resources/org/springframework/data/redis/connection/jredis/JredisConnectionIntegrationTests-context.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests-context.xml b/src/test/resources/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests-context.xml new file mode 100644 index 000000000..75da6c57d --- /dev/null +++ b/src/test/resources/org/springframework/data/redis/connection/lettuce/LettuceConnectionIntegrationTests-context.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/org/springframework/data/redis/connection/rjc/RjcConnectionIntegrationTests-context.xml b/src/test/resources/org/springframework/data/redis/connection/rjc/RjcConnectionIntegrationTests-context.xml new file mode 100644 index 000000000..3fb031250 --- /dev/null +++ b/src/test/resources/org/springframework/data/redis/connection/rjc/RjcConnectionIntegrationTests-context.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests-context.xml b/src/test/resources/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests-context.xml new file mode 100644 index 000000000..89a45052b --- /dev/null +++ b/src/test/resources/org/springframework/data/redis/connection/srp/SrpConnectionIntegrationTests-context.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + \ No newline at end of file