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
This commit is contained in:
Jennifer Hickey
2013-03-19 17:09:20 -07:00
parent ae3869f0ec
commit b518fa16bf
11 changed files with 158 additions and 102 deletions

View File

@@ -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<String> 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);
}
}

View File

@@ -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

View File

@@ -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")

View File

@@ -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 {

View File

@@ -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();

View File

@@ -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 {