Skip to content

Commit

Permalink
fixing Issue 188
Browse files Browse the repository at this point in the history
  • Loading branch information
vsoskov committed Aug 1, 2011
1 parent 6c3ec9f commit 22d4d3f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/main/java/redis/clients/jedis/BinaryTransaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Set;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.exceptions.JedisDataException;

public class BinaryTransaction extends Queable {
protected Client client = null;
Expand All @@ -28,10 +29,29 @@ public List<Object> exec() {
}
List<Object> formatted = new ArrayList<Object>();
for (Object o : unformatted) {
formatted.add(generateResponse(o).get());
try{
formatted.add(generateResponse(o).get());
}catch(JedisDataException e){
formatted.add(e);
}
}
return formatted;
}

public List<Response<?>> execGetResponse() {
client.exec();
client.getAll(1); // Discard all but the last reply

List<Object> unformatted = client.getObjectMultiBulkReply();
if (unformatted == null) {
return null;
}
List<Response<?>> response = new ArrayList<Response<?>>();
for (Object o : unformatted) {
response.add(generateResponse(o));
}
return response;
}

public String discard() {
client.discard();
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import redis.clients.jedis.Protocol.Command;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.util.RedisInputStream;
import redis.clients.util.RedisOutputStream;
Expand Down Expand Up @@ -204,7 +205,11 @@ public List<Object> getAll(int except) {
List<Object> all = new ArrayList<Object>();
flush();
while (pipelinedCommands > except) {
all.add(protocol.read(inputStream));
try{
all.add(protocol.read(inputStream));
}catch(JedisDataException e){
all.add(e);
}
pipelinedCommands--;
}
return all;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ private List<Object> processMultiBulkReply(final RedisInputStream is) {
}
List<Object> ret = new ArrayList<Object>(num);
for (int i = 0; i < num; i++) {
ret.add(process(is));
try{
ret.add(process(is));
}catch(JedisDataException e){
ret.add(e);
}
}
return ret;
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/redis/clients/jedis/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public T get() {
"Please close pipeline or multi block before calling this method.");
}
if (!built) {
if (data instanceof JedisDataException){
throw new JedisDataException((JedisDataException)data);
}
response = builder.build(data);
this.data = null;
built = true;
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/redis/clients/jedis/tests/PipeliningTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,20 @@ public void canRetrieveUnsetKey() {
p.sync();
assertNull(shouldNotExist.get());
}

@Test
public void piplineWithError(){
Pipeline p = jedis.pipelined();
p.set("foo", "bar");
Response<Set<String>> error = p.smembers("foo");
Response<String> r = p.get("foo");
p.sync();
try{
error.get();
fail();
}catch(JedisDataException e){
//that is fine we should be here
}
assertEquals(r.get(), "bar");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.Transaction;
import redis.clients.jedis.TransactionBlock;
Expand Down Expand Up @@ -241,4 +242,39 @@ public void transactionResponseWithinPipeline() {
string.get();
t.exec();
}

@Test
public void transactionResponseWithError() {
Transaction t = jedis.multi();
t.set("foo", "bar");
Response<Set<String>> error = t.smembers("foo");
Response<String> r = t.get("foo");
List<Object> l = t.exec();
assertEquals(JedisDataException.class, l.get(1).getClass());
try{
error.get();
fail("We expect exception here!");
}catch(JedisDataException e){
//that is fine we should be here
}
assertEquals(r.get(), "bar");
}

@Test
public void execGetResponse() {
Transaction t = jedis.multi();

t.set("foo", "bar");
t.smembers("foo");
t.get("foo");

List<Response<?>> lr = t.execGetResponse();
try{
lr.get(1).get();
fail("We expect exception here!");
}catch(JedisDataException e){
//that is fine we should be here
}
assertEquals("bar", lr.get(2).get());
}
}

0 comments on commit 22d4d3f

Please sign in to comment.