Fix StringRedisConnection not passed to callbacks in StringTemplate.execute()

DATAREDIS-222
This commit is contained in:
Jennifer Hickey
2013-07-16 13:40:18 -07:00
parent 9bad0adfa4
commit 14b84ab2af
6 changed files with 45 additions and 20 deletions

View File

@@ -157,23 +157,24 @@ public class RedisTemplate<K, V> extends RedisAccessor implements RedisOperation
conn = RedisConnectionUtils.getConnection(factory);
boolean existingConnection = TransactionSynchronizationManager.hasResource(factory);
preProcessConnection(conn, existingConnection);
boolean pipelineStatus = conn.isPipelined();
RedisConnection connToUse = preProcessConnection(conn, existingConnection);
boolean pipelineStatus = connToUse.isPipelined();
if (pipeline && !pipelineStatus) {
conn.openPipeline();
connToUse.openPipeline();
}
RedisConnection connToExpose = (exposeConnection ? conn : createRedisConnectionProxy(conn));
RedisConnection connToExpose = (exposeConnection ? connToUse : createRedisConnectionProxy(connToUse));
T result = action.doInRedis(connToExpose);
// close pipeline
if (pipeline && !pipelineStatus) {
conn.closePipeline();
connToUse.closePipeline();
}
// TODO: any other connection processing?
return postProcessResult(result, conn, existingConnection);
return postProcessResult(result, connToUse, existingConnection);
} finally {
RedisConnectionUtils.releaseConnection(conn, factory);
}

View File

@@ -38,7 +38,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("RedisTemplateTests-context.xml")
@ContextConfiguration("StringRedisTemplateTests-context.xml")
@ProfileValueSourceConfiguration(RedisTestProfileValueSource.class)
public class DefaultSetOperationsTests {

View File

@@ -27,7 +27,9 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.RedisTestProfileValueSource;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -38,7 +40,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("RedisTemplateTests-context.xml")
@ContextConfiguration("StringRedisTemplateTests-context.xml")
@ProfileValueSourceConfiguration(RedisTestProfileValueSource.class)
public class DefaultValueOperationsTests {
@Autowired

View File

@@ -22,6 +22,7 @@ import org.junit.Test;
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.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
@@ -36,18 +37,19 @@ public class SessionTest {
@Test
public void testSession() throws Exception {
final RedisConnection conn = mock(RedisConnection.class);
final StringRedisConnection stringConn = mock(StringRedisConnection.class);
RedisConnectionFactory factory = mock(RedisConnectionFactory.class);
final StringRedisTemplate template = spy(new StringRedisTemplate(factory));
when(factory.getConnection()).thenReturn(conn);
final StringRedisTemplate template = new StringRedisTemplate(factory);
doReturn(stringConn).when(template).preProcessConnection(eq(conn), anyBoolean());
template.execute(new SessionCallback<Object>() {
@SuppressWarnings("rawtypes")
public Object execute(RedisOperations operations) {
checkConnection(template, conn);
checkConnection(template, stringConn);
template.discard();
assertSame(template, operations);
checkConnection(template, conn);
checkConnection(template, stringConn);
return null;
}
});
@@ -55,8 +57,6 @@ public class SessionTest {
private void checkConnection(RedisTemplate<?, ?> template, final RedisConnection expectedConnection) {
template.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
assertSame(expectedConnection, connection);
return null;

View File

@@ -25,28 +25,38 @@ import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.RedisTestProfileValueSource;
import org.springframework.data.redis.TestCondition;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.annotation.ProfileValueSourceConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* Integration test of {@link RedisTemplate}
*
* Integration test of {@link StringRedisTemplate}
*
* @author Jennifer Hickey
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class RedisTemplateTests {
@ProfileValueSourceConfiguration(RedisTestProfileValueSource.class)
public class StringRedisTemplateTests {
@Autowired
private RedisTemplate<String, String> redisTemplate;
private StringRedisTemplate redisTemplate;
@After
public void tearDown() {
redisTemplate.getConnectionFactory().getConnection().flushDb();
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
connection.flushDb();
return null;
}
});
}
@Test
@@ -90,4 +100,15 @@ public class RedisTemplateTests {
tpl.exec();
}
@Test
public void testStringTemplateExecutesWithStringConn() {
String value = redisTemplate.execute(new RedisCallback<String>() {
public String doInRedis(RedisConnection connection) {
StringRedisConnection stringConn = (StringRedisConnection) connection;
stringConn.set("test", "it");
return stringConn.get("test");
}
});
assertEquals(value,"it");
}
}