Make Jedis return converted tx results

DATAREDIS-208
This commit is contained in:
Jennifer Hickey
2013-07-23 12:40:23 -07:00
parent 893b9f5007
commit 08d502083c
11 changed files with 286 additions and 128 deletions

View File

@@ -685,8 +685,7 @@ public abstract class AbstractConnectionIntegrationTests {
actual.add(connection.exec());
List<Object> results = getResults();
List<Object> execResults = (List<Object>) results.get(0);
assertEquals(2, execResults.size());
assertEquals("value", new String((byte[]) execResults.get(1)));
assertEquals(Arrays.asList(new Object[] {"value"}), execResults);
assertEquals("value", connection.get("key"));
}
@@ -763,7 +762,7 @@ public abstract class AbstractConnectionIntegrationTests {
actual.add(connection.exec());
List<Object> results = getResults();
List<Object> execResults = (List<Object>) results.get(0);
assertEquals("somethingelse", new String((byte[]) execResults.get(1)));
assertEquals(Arrays.asList(new Object[] {"somethingelse"}), execResults);
}
@Test

View File

@@ -0,0 +1,116 @@
package org.springframework.data.redis.connection;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
public class ConvertingAbstractConnectionTransactionIntegrationTests extends
AbstractConnectionIntegrationTests {
@Ignore
public void testMultiDiscard() {
}
@Ignore
public void testMultiExec() {
}
@Ignore
public void testUnwatch() {
}
@Ignore
public void testWatch() {
}
@Ignore
@Test
public void testExecWithoutMulti() {
}
@Ignore
@Test
public void testErrorInTx() {
}
/*
* Using blocking ops inside a tx does not make a lot of sense as it would require blocking the
* entire server in order to execute the block atomically, which in turn does not allow other
* clients to perform a push operation. *
*/
@Ignore
public void testBLPop() {
}
@Ignore
public void testBRPop() {
}
@Ignore
public void testBRPopLPush() {
}
@Ignore
public void testBLPopTimeout() {
}
@Ignore
public void testBRPopTimeout() {
}
@Ignore
public void testBRPopLPushTimeout() {
}
@Ignore("Pub/Sub not supported with transactions")
public void testPubSubWithNamedChannels() throws Exception {
}
@Ignore("Pub/Sub not supported with transactions")
public void testPubSubWithPatterns() throws Exception {
}
@Ignore
public void testNullKey() throws Exception {
}
@Ignore
public void testNullValue() throws Exception {
}
@Ignore
public void testHashNullKey() throws Exception {
}
@Ignore
public void testHashNullValue() throws Exception {
}
@Test(expected = UnsupportedOperationException.class)
public void testWatchWhileInTx() {
connection.watch("foo".getBytes());
}
protected void initConnection() {
connection.multi();
}
protected List<Object> getResults() {
return connection.exec();
}
protected void verifyResults(List<Object> expected) {
List<Object> expectedTx = new ArrayList<Object>();
for (int i = 0; i < actual.size(); i++) {
expectedTx.add(null);
}
assertEquals(expectedTx, actual);
List<Object> results = getResults();
assertEquals(expected, results);
}
}

View File

@@ -16,10 +16,6 @@
package org.springframework.data.redis.connection.jedis;
import static org.junit.Assert.assertTrue;
import java.util.List;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -30,8 +26,6 @@ import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.exceptions.JedisDataException;
/**
* Integration test of {@link JedisConnection}
*
@@ -57,18 +51,6 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
connection = null;
}
@Test
public void testErrorInTx() {
connection.multi();
connection.set("foo","bar");
// Try to do a list op on a value
connection.lPop("foo");
List<Object> results = connection.exec();
// Jedis puts the Exception in the exec results instead of throwing an Exception on exec
// This should be fixed in DATAREDIS-208 when we convert results of tx.exec()
assertTrue(results.get(1) instanceof JedisDataException);
}
@Test(expected=UnsupportedOperationException.class)
public void testPExpire() {
super.testPExpire();
@@ -231,4 +213,9 @@ public class JedisConnectionIntegrationTests extends AbstractConnectionIntegrati
public void testExecWithoutMulti() {
super.testExecWithoutMulti();
}
@Test(expected=InvalidDataAccessApiUsageException.class)
public void testErrorInTx() {
super.testErrorInTx();
}
}

View File

@@ -115,7 +115,7 @@ public class JedisConnectionPipelineIntegrationTests extends
actual.add(connection.exec());
List<Object> results = getResults();
List<Object> execResults = (List<Object>) results.get(0);
assertEquals("somethingelse", new String((byte[]) execResults.get(1)));
assertEquals(Arrays.asList(new Object[] {"somethingelse"}), execResults);
}
// Unsupported Ops

View File

@@ -57,17 +57,6 @@ public class JedisConnectionPipelineTxIntegrationTests extends JedisConnectionTr
@SuppressWarnings("unchecked")
protected List<Object> getResults() {
assertNull(connection.exec());
List<Object> pipelined = connection.closePipeline();
// We expect only the results of exec to be in the closed pipeline
assertEquals(1,pipelined.size());
List<Object> txResults = (List<Object>)pipelined.get(0);
// Return exec results and this test should behave exactly like its superclass
return convertResults(txResults);
}
@SuppressWarnings("unchecked")
protected List<Object> getResultsNoConversion() {
assertNull(connection.exec());
List<Object> pipelined = connection.closePipeline();
// We expect only the results of exec to be in the closed pipeline
@@ -76,5 +65,4 @@ public class JedisConnectionPipelineTxIntegrationTests extends JedisConnectionTr
// Return exec results and this test should behave exactly like its superclass
return txResults;
}
}

View File

@@ -15,28 +15,15 @@
*/
package org.springframework.data.redis.connection.jedis;
import static org.junit.Assert.fail;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.redis.RedisVersionUtils;
import org.springframework.data.redis.connection.AbstractConnectionTransactionIntegrationTests;
import org.springframework.data.redis.connection.DefaultStringTuple;
import org.springframework.data.redis.connection.StringRedisConnection.StringTuple;
import org.springframework.data.redis.connection.ConvertingAbstractConnectionTransactionIntegrationTests;
import org.springframework.test.annotation.IfProfileValue;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Tuple;
/**
* Integration test of {@link JedisConnection} transaction functionality.
* <p>
@@ -50,7 +37,7 @@ import redis.clients.jedis.Tuple;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("JedisConnectionIntegrationTests-context.xml")
public class JedisConnectionTransactionIntegrationTests extends
AbstractConnectionTransactionIntegrationTests {
ConvertingAbstractConnectionTransactionIntegrationTests {
@After
public void tearDown() {
@@ -375,41 +362,4 @@ public class JedisConnectionTransactionIntegrationTests extends
public void testLastSave() {
super.testLastSave();
}
@Test
public void exceptionExecuteNative() throws Exception {
actual.add(connection.execute("ZadD", getClass() + "#foo\t0.90\titem"));
try {
// In Redis 2.4, syntax error on queued commands are swallowed and no results are
// returned
verifyResults(Arrays.asList(new Object[] {}));
if (RedisVersionUtils.atLeast("2.6.5", connection)) {
fail("Redis 2.6 should throw an Exception on exec if commands are invalid");
}
} catch (InvalidDataAccessApiUsageException e) {
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object convertResult(Object result) {
Object convertedResult = super.convertResult(result);
if (convertedResult instanceof Set) {
if (!(((Set) convertedResult).isEmpty())
&& ((Set) convertedResult).iterator().next() instanceof Tuple) {
Set<StringTuple> tuples = new LinkedHashSet<StringTuple>();
for (Tuple value : ((Set<Tuple>) convertedResult)) {
DefaultStringTuple tuple = new DefaultStringTuple(
(byte[]) value.getBinaryElement(), value.getElement(), value.getScore());
tuples.add(tuple);
}
return tuples;
}
}
return convertedResult;
}
@Override
protected DataAccessException convertException(Exception ex) {
return JedisConverters.toDataAccessException(ex);
}
}