DATAREDIS-603 - Fall back to RedisSystemException for non translateable exceptions during cluster execution.

We now use a fallback exception translation strategy in JedisClusterConnection to map all non-mapped exceptions to RedisSystemException. This change prevents null pointer exceptions caused by potentially throwing null.

Original Pull Request: #275
This commit is contained in:
Mark Paluch
2017-09-06 16:07:19 +02:00
committed by Christoph Strobl
parent 2d49661e2d
commit f5c9e75d97
6 changed files with 76 additions and 11 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.redis.connection.jedis;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.redis.connection.ClusterTestVariables.*;
@@ -47,6 +48,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.data.redis.ClusterStateFailureException;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.connection.ClusterInfo;
import org.springframework.data.redis.connection.RedisClusterCommands.AddSlots;
import org.springframework.data.redis.connection.RedisClusterNode;
@@ -107,7 +109,7 @@ public class JedisClusterConnectionUnitTests {
}
@Test // DATAREDIS-315
public void thowsExceptionWhenClusterCommandExecturorIsNull() {
public void throwsExceptionWhenClusterCommandExecutorIsNull() {
expectedException.expect(IllegalArgumentException.class);
@@ -352,6 +354,20 @@ public class JedisClusterConnectionUnitTests {
new JedisClusterTopologyProvider(clusterMock).getTopology();
}
@Test // DATAREDIS-603
public void translatesUnknownExceptions() {
IllegalArgumentException exception = new IllegalArgumentException("Aw, snap!");
expectedException.expect(RedisSystemException.class);
expectedException.expectMessage(exception.getMessage());
expectedException.expectCause(is(exception));
doThrow(exception).when(clusterMock).set("foo".getBytes(), "bar".getBytes());
connection.set("foo".getBytes(), "bar".getBytes());
}
static class StubJedisCluster extends JedisCluster {
JedisClusterConnectionHandler connectionHandler;

View File

@@ -15,13 +15,19 @@
*/
package org.springframework.data.redis.connection.lettuce;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.any;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisFuture;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.protocol.RedisCommand;
import java.lang.reflect.InvocationTargetException;
@@ -29,6 +35,7 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.mockito.Mockito;
import org.springframework.dao.InvalidDataAccessResourceUsageException;
import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase;
import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption;
@@ -136,6 +143,43 @@ public class LettuceConnectionUnitTestSuite {
verify(syncCommandsMock, times(1)).select(1);
}
@Test // DATAREDIS-603
public void translatesUnknownExceptions() {
IllegalArgumentException exception = new IllegalArgumentException("Aw, snap!");
when(syncCommandsMock.set(any(), any())).thenThrow(exception);
connection = new LettuceConnection(null, 0, clientMock, null, 1);
try {
connection.set("foo".getBytes(), "bar".getBytes());
} catch (Exception e) {
assertThat(e.getMessage(), containsString(exception.getMessage()));
assertThat(e.getCause(), is((Throwable) exception));
}
}
@Test // DATAREDIS-603
public void translatesPipelineUnknownExceptions() throws Exception {
IllegalArgumentException exception = new IllegalArgumentException("Aw, snap!");
RedisCommand future = mock(RedisCommand.class, Mockito.withSettings().extraInterfaces(RedisFuture.class));
when(((RedisFuture) future).get()).thenThrow(exception);
when(asyncCommandsMock.set(any(byte[].class), any(byte[].class))).thenReturn((RedisFuture) future);
connection = new LettuceConnection(null, 0, clientMock, null, 1);
connection.openPipeline();
try {
connection.set("foo".getBytes(), "bar".getBytes());
} catch (Exception e) {
assertThat(e.getMessage(), containsString(exception.getMessage()));
assertThat(e.getCause(), is((Throwable) exception));
}
}
}
public static class LettucePipelineConnectionUnitTests extends LettuceConnectionUnitTests {