From d116d3825a8b32c8b9689dc2afa57ff272336ec5 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Fri, 14 Mar 2014 10:09:33 +0100 Subject: [PATCH] DATAREDIS-184 - Add support for 'SAVE/NOSAVE' to shutdown. Created and added 'ShutdownOptions' to 'RedisServerCommands'. The full implementation is available for 'lettuce' and 'srp'. When using 'jedis' the command is emulated using lua script via 'eval'. For 'jredis' the method is not available and will throw 'UnsupportedOperationException'. Original Pull Request: #47 --- .../DefaultStringRedisConnection.java | 9 ++ .../redis/connection/RedisServerCommands.java | 12 ++ .../connection/jedis/JedisConnection.java | 17 +++ .../connection/jredis/JredisConnection.java | 9 ++ .../connection/lettuce/LettuceConnection.java | 24 ++++ .../redis/connection/srp/SrpConnection.java | 35 ++++- .../AbstractConnectionUnitTestBase.java | 69 +++++++++ .../DefaultStringRedisConnectionTests.java | 11 ++ .../jedis/JedisConnectionUnitTestSuite.java | 133 ++++++++++++++++++ .../jredis/JRedisConnectionUnitTests.java | 59 ++++++++ .../LettuceConnectionUnitTestSuite.java | 97 +++++++++++++ .../srp/SrpConnectionUnitTestSuite.java | 128 +++++++++++++++++ 12 files changed, 602 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/springframework/data/redis/connection/AbstractConnectionUnitTestBase.java create mode 100644 src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionUnitTestSuite.java create mode 100644 src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionUnitTests.java create mode 100644 src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTestSuite.java create mode 100644 src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionUnitTestSuite.java diff --git a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java index 9d2805c66..3eaba7053 100644 --- a/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/DefaultStringRedisConnection.java @@ -760,6 +760,15 @@ public class DefaultStringRedisConnection implements StringRedisConnection { delegate.shutdown(); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.connection.RedisServerCommands#shutdown(org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption) + */ + @Override + public void shutdown(ShutdownOption option) { + delegate.shutdown(option); + } + public Set sInter(byte[]... keys) { Set results = delegate.sInter(keys); if (isFutureConversion()) { diff --git a/src/main/java/org/springframework/data/redis/connection/RedisServerCommands.java b/src/main/java/org/springframework/data/redis/connection/RedisServerCommands.java index b21cc0d3e..d3f7fff39 100644 --- a/src/main/java/org/springframework/data/redis/connection/RedisServerCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/RedisServerCommands.java @@ -27,6 +27,10 @@ import java.util.Properties; */ public interface RedisServerCommands { + public enum ShutdownOption { + SAVE, NOSAVE; + } + /** * Start an {@literal Append Only File} rewrite process on server. * @@ -116,6 +120,14 @@ public interface RedisServerCommands { */ void shutdown(); + /** + * Shutdown server. + * + * @see http://redis.io/commands/shutdown + * @since 1.3 + */ + void shutdown(ShutdownOption option); + /** * Load configuration parameters for given {@code pattern} from server. * diff --git a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java index 67460b754..26d51b338 100644 --- a/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jedis/JedisConnection.java @@ -76,6 +76,8 @@ public class JedisConnection implements RedisConnection { private static final Method SEND_COMMAND; private static final Method GET_RESPONSE; + private static final String SHUTDOWN_SCRIPT = "return redis.call('SHUTDOWN','%s')"; + static { CLIENT_FIELD = ReflectionUtils.findField(BinaryJedis.class, "client", Client.class); ReflectionUtils.makeAccessible(CLIENT_FIELD); @@ -624,6 +626,21 @@ public class JedisConnection implements RedisConnection { } } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.connection.RedisServerCommands#shutdown(org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption) + */ + @Override + public void shutdown(ShutdownOption option) { + + if (option == null) { + shutdown(); + return; + } + + eval(String.format(SHUTDOWN_SCRIPT, option.name()).getBytes(), ReturnType.STATUS, 0); + } + public byte[] echo(byte[] message) { try { if (isPipelined()) { diff --git a/src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java b/src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java index 6ecab2ee4..bfb8ae347 100644 --- a/src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/jredis/JredisConnection.java @@ -291,6 +291,15 @@ public class JredisConnection implements RedisConnection { throw new UnsupportedOperationException(); } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.connection.RedisServerCommands#shutdown(org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption) + */ + @Override + public void shutdown(ShutdownOption option) { + throw new UnsupportedOperationException(); + } + public Long del(byte[]... keys) { try { return jredis.del(keys); diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java index 8cdbadc1f..ad1e0cac3 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnection.java @@ -662,6 +662,30 @@ public class LettuceConnection implements RedisConnection { } } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.connection.RedisServerCommands#shutdown(org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption) + */ + @Override + public void shutdown(ShutdownOption option) { + + if (option == null) { + shutdown(); + return; + } + + boolean save = ShutdownOption.SAVE.equals(option); + try { + if (isPipelined()) { + getAsyncConnection().shutdown(save); + return; + } + getConnection().shutdown(save); + } catch (Exception ex) { + throw convertLettuceAccessException(ex); + } + } + public byte[] echo(byte[] message) { try { if (isPipelined()) { diff --git a/src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java b/src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java index ba7c5a6c3..f55d06db5 100644 --- a/src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java +++ b/src/main/java/org/springframework/data/redis/connection/srp/SrpConnection.java @@ -24,6 +24,7 @@ import java.util.Map; import java.util.Properties; import java.util.Queue; import java.util.Set; +import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.Future; @@ -149,7 +150,7 @@ public class SrpConnection implements RedisConnection { public void addCommand(FutureResult result) { futureResults.add(result); - if (!(result instanceof SrpTxResult)) { + if (!(result instanceof SrpTxResult) && result.getResultHolder() != null) { Futures.addCallback(((SrpGenericResult) result).getResultHolder(), this); } } @@ -208,6 +209,13 @@ public class SrpConnection implements RedisConnection { } } + SrpConnection(RedisClient client) { + + Assert.notNull(client); + this.client = client; + this.queue = new ArrayBlockingQueue(50); + } + public SrpConnection(String host, int port, BlockingQueue queue) { try { this.client = new RedisClient(host, port); @@ -481,6 +489,31 @@ public class SrpConnection implements RedisConnection { } } + /* + * (non-Javadoc) + * @see org.springframework.data.redis.connection.RedisServerCommands#shutdown(org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption) + */ + @Override + public void shutdown(ShutdownOption option) { + + if (option == null) { + shutdown(); + return; + } + + byte[] save = option.name().getBytes(Charsets.UTF_8); + try { + if (isPipelined()) { + pipeline(new SrpStatusResult(pipeline.shutdown(save, null))); + return; + } + client.shutdown(save, null); + } catch (Exception ex) { + throw convertSrpAccessException(ex); + } + + } + public byte[] echo(byte[] message) { try { if (isPipelined()) { diff --git a/src/test/java/org/springframework/data/redis/connection/AbstractConnectionUnitTestBase.java b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionUnitTestBase.java new file mode 100644 index 000000000..d90e846be --- /dev/null +++ b/src/test/java/org/springframework/data/redis/connection/AbstractConnectionUnitTestBase.java @@ -0,0 +1,69 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +import org.mockito.Mockito; + +/** + * @author Christoph Strobl + */ +public abstract class AbstractConnectionUnitTestBase { + + private T nativeRedisConnectionMock; + + protected T getNativeRedisConnectionMock() { + + if (this.nativeRedisConnectionMock == null) { + Class type = resolveReturnedClassFromGernericType(); + this.nativeRedisConnectionMock = Mockito.mock(type); + } + + return this.nativeRedisConnectionMock; + } + + protected T verifyNativeConnectionInvocation() { + return Mockito.verify(getNativeRedisConnectionMock(), Mockito.times(1)); + } + + protected void setNativeRedisConnectionMock(T nativeRedisConnectionMock) { + this.nativeRedisConnectionMock = nativeRedisConnectionMock; + } + + @SuppressWarnings("unchecked") + private Class resolveReturnedClassFromGernericType() { + + ParameterizedType parameterizedType = resolveReturnedClassFromGernericType(getClass()); + return (Class) parameterizedType.getActualTypeArguments()[0]; + } + + private ParameterizedType resolveReturnedClassFromGernericType(Class clazz) { + + Object genericSuperclass = clazz.getGenericSuperclass(); + if (genericSuperclass instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass; + Type rawtype = parameterizedType.getRawType(); + if (AbstractConnectionUnitTestBase.class.equals(rawtype)) { + return parameterizedType; + } + } + + return resolveReturnedClassFromGernericType(clazz.getSuperclass()); + } + +} diff --git a/src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java b/src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java index 816a0594c..532a4dcb3 100644 --- a/src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java +++ b/src/test/java/org/springframework/data/redis/connection/DefaultStringRedisConnectionTests.java @@ -35,6 +35,7 @@ import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.data.redis.connection.RedisListCommands.Position; +import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption; import org.springframework.data.redis.connection.RedisStringCommands.BitOperation; import org.springframework.data.redis.connection.RedisZSetCommands.Aggregate; import org.springframework.data.redis.connection.RedisZSetCommands.Tuple; @@ -1699,6 +1700,16 @@ public class DefaultStringRedisConnectionTests { verifyResults(Arrays.asList(1L)); } + /** + * @see DATAREDIS-184 + */ + @Test + public void testShutdownInDelegatedCorrectlyToNativeConnection() { + + connection.shutdown(ShutdownOption.NOSAVE); + verify(nativeConnection, times(1)).shutdown(eq(ShutdownOption.NOSAVE)); + } + protected List getResults() { return actual; } diff --git a/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionUnitTestSuite.java b/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionUnitTestSuite.java new file mode 100644 index 000000000..545ad2e4e --- /dev/null +++ b/src/test/java/org/springframework/data/redis/connection/jedis/JedisConnectionUnitTestSuite.java @@ -0,0 +1,133 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.jedis; + +import static org.hamcrest.core.IsEqual.*; +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.junit.runners.Suite.SuiteClasses; +import org.mockito.ArgumentCaptor; +import org.mockito.Matchers; +import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase; +import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption; +import org.springframework.data.redis.connection.jedis.JedisConnectionUnitTestSuite.JedisConnectionPipelineUnitTests; +import org.springframework.data.redis.connection.jedis.JedisConnectionUnitTestSuite.JedisConnectionUnitTests; + +import redis.clients.jedis.Client; +import redis.clients.jedis.Jedis; + +/** + * @author Christoph Strobl + */ +@RunWith(Suite.class) +@SuiteClasses({ JedisConnectionUnitTests.class, JedisConnectionPipelineUnitTests.class }) +public class JedisConnectionUnitTestSuite { + + public static class JedisConnectionUnitTests extends AbstractConnectionUnitTestBase { + + protected JedisConnection connection; + + @Before + public void setUp() { + connection = new JedisConnection(new MockedClientJedis("http://localhost:1234", getNativeRedisConnectionMock())); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithNullShouldDelegateCommandCorrectly() { + + connection.shutdown(null); + + verifyNativeConnectionInvocation().shutdown(); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownNosaveShouldBeSentCorrectlyUsingLuaScript() { + + connection.shutdown(ShutdownOption.NOSAVE); + + ArgumentCaptor captor = ArgumentCaptor.forClass(byte[].class); + verifyNativeConnectionInvocation().eval(captor.capture(), Matchers.any(byte[].class), + Matchers.any(byte[][].class)); + + assertThat(captor.getValue(), equalTo("return redis.call('SHUTDOWN','NOSAVE')".getBytes())); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownSaveShouldBeSentCorrectlyUsingLuaScript() { + + connection.shutdown(ShutdownOption.SAVE); + + ArgumentCaptor captor = ArgumentCaptor.forClass(byte[].class); + verifyNativeConnectionInvocation().eval(captor.capture(), Matchers.any(byte[].class), + Matchers.any(byte[][].class)); + + assertThat(captor.getValue(), equalTo("return redis.call('SHUTDOWN','SAVE')".getBytes())); + } + + } + + public static class JedisConnectionPipelineUnitTests extends JedisConnectionUnitTests { + + @Before + public void setUp() { + super.setUp(); + connection.openPipeline(); + } + + /** + * @see DATAREDIS-184 + */ + @Override + @Test(expected = UnsupportedOperationException.class) + public void shutdownNosaveShouldBeSentCorrectlyUsingLuaScript() { + super.shutdownNosaveShouldBeSentCorrectlyUsingLuaScript(); + } + + /** + * @see DATAREDIS-184 + */ + @Override + @Test(expected = UnsupportedOperationException.class) + public void shutdownSaveShouldBeSentCorrectlyUsingLuaScript() { + super.shutdownSaveShouldBeSentCorrectlyUsingLuaScript(); + } + + } + + /** + * {@link Jedis} extension allowing to use mocked object as {@link Client}. + */ + private static class MockedClientJedis extends Jedis { + + public MockedClientJedis(String host, Client client) { + super(host); + this.client = client; + } + } +} diff --git a/src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionUnitTests.java b/src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionUnitTests.java new file mode 100644 index 000000000..14b60f0de --- /dev/null +++ b/src/test/java/org/springframework/data/redis/connection/jredis/JRedisConnectionUnitTests.java @@ -0,0 +1,59 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.jredis; + +import org.jredis.JRedis; +import org.junit.Before; +import org.junit.Test; +import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase; +import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption; + +/** + * @author Christoph Strobl + */ +public class JRedisConnectionUnitTests extends AbstractConnectionUnitTestBase { + + private JredisConnection connection; + + @Before + public void setUp() { + connection = new JredisConnection(getNativeRedisConnectionMock()); + } + + /** + * @see DATAREDIS-184 + */ + @Test(expected = UnsupportedOperationException.class) + public void shutdownSaveShouldThrowUnsupportedOperationException() { + connection.shutdown(ShutdownOption.SAVE); + } + + /** + * @see DATAREDIS-184 + */ + @Test(expected = UnsupportedOperationException.class) + public void shutdownNosaveShouldThrowUnsupportedOperationException() { + connection.shutdown(ShutdownOption.NOSAVE); + } + + /** + * @see DATAREDIS-184 + */ + @Test(expected = UnsupportedOperationException.class) + public void shutdownWithNullShouldThrowUnsupportedOperationException() { + connection.shutdown(null); + } +} diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTestSuite.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTestSuite.java new file mode 100644 index 000000000..a5c5df63f --- /dev/null +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionUnitTestSuite.java @@ -0,0 +1,97 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.lettuce; + +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase; +import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionUnitTestSuite.LettuceConnectionUnitTests; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionUnitTestSuite.LettucePipelineConnectionUnitTests; + +import com.lambdaworks.redis.RedisAsyncConnection; +import com.lambdaworks.redis.RedisClient; +import com.lambdaworks.redis.codec.RedisCodec; + +/** + * @author Christoph Strobl + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ LettuceConnectionUnitTests.class, LettucePipelineConnectionUnitTests.class }) +public class LettuceConnectionUnitTestSuite { + + @SuppressWarnings("rawtypes") + public static class LettuceConnectionUnitTests extends AbstractConnectionUnitTestBase { + + protected LettuceConnection connection; + + @SuppressWarnings({ "unchecked" }) + @Before + public void setUp() { + + RedisClient clientMock = mock(RedisClient.class); + when(clientMock.connectAsync((RedisCodec) any())).thenReturn(getNativeRedisConnectionMock()); + connection = new LettuceConnection(0, clientMock); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithNullOpionsIsCalledCorrectly() { + + connection.shutdown(null); + verifyNativeConnectionInvocation().shutdown(true); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithNosaveOptionIsCalledCorrectly() { + + connection.shutdown(ShutdownOption.NOSAVE); + verifyNativeConnectionInvocation().shutdown(false); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithSaveOptionIsCalledCorrectly() { + + connection.shutdown(ShutdownOption.SAVE); + verifyNativeConnectionInvocation().shutdown(true); + } + + } + + public static class LettucePipelineConnectionUnitTests extends LettuceConnectionUnitTests { + + @Override + @Before + public void setUp() { + super.setUp(); + this.connection.openPipeline(); + } + } + +} diff --git a/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionUnitTestSuite.java b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionUnitTestSuite.java new file mode 100644 index 000000000..52af77a85 --- /dev/null +++ b/src/test/java/org/springframework/data/redis/connection/srp/SrpConnectionUnitTestSuite.java @@ -0,0 +1,128 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.redis.connection.srp; + +import static org.mockito.Mockito.*; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import org.springframework.data.redis.connection.AbstractConnectionUnitTestBase; +import org.springframework.data.redis.connection.RedisServerCommands.ShutdownOption; +import org.springframework.data.redis.connection.srp.SrpConnectionUnitTestSuite.SrpConnectionPiplineUnitTests; +import org.springframework.data.redis.connection.srp.SrpConnectionUnitTestSuite.SrpConnectionUnitTests; + +import redis.client.RedisClient; +import redis.client.RedisClient.Pipeline; + +import com.google.common.base.Charsets; + +/** + * @author Christoph Strobl + */ +@RunWith(Suite.class) +@Suite.SuiteClasses({ SrpConnectionUnitTests.class, SrpConnectionPiplineUnitTests.class }) +public class SrpConnectionUnitTestSuite { + + public static class SrpConnectionUnitTests extends AbstractConnectionUnitTestBase { + + protected SrpConnection connection; + + @Before + public void setUp() { + connection = new SrpConnection(getNativeRedisConnectionMock()); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithNullOpionsIsCalledCorrectly() { + + connection.shutdown(null); + verifyNativeConnectionInvocation().shutdown("SAVE".getBytes(Charsets.UTF_8), null); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithSaveIsCalledCorrectly() { + + connection.shutdown(ShutdownOption.SAVE); + verifyNativeConnectionInvocation().shutdown("SAVE".getBytes(Charsets.UTF_8), null); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithNosaveIsCalledCorrectly() { + + connection.shutdown(ShutdownOption.NOSAVE); + verifyNativeConnectionInvocation().shutdown("NOSAVE".getBytes(Charsets.UTF_8), null); + } + + } + + public static class SrpConnectionPiplineUnitTests extends AbstractConnectionUnitTestBase { + + protected SrpConnection connection; + + @Before + public void setUp() { + + RedisClient clientMock = mock(RedisClient.class); + connection = new SrpConnection(clientMock); + when(clientMock.pipeline()).thenReturn(getNativeRedisConnectionMock()); + + connection.openPipeline(); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithNullOpionsIsCalledCorrectly() { + + connection.shutdown(null); + verifyNativeConnectionInvocation().shutdown("SAVE".getBytes(Charsets.UTF_8), null); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithSaveIsCalledCorrectly() { + + connection.shutdown(ShutdownOption.SAVE); + verifyNativeConnectionInvocation().shutdown("SAVE".getBytes(Charsets.UTF_8), null); + } + + /** + * @see DATAREDIS-184 + */ + @Test + public void shutdownWithNosaveIsCalledCorrectly() { + + connection.shutdown(ShutdownOption.NOSAVE); + verifyNativeConnectionInvocation().shutdown("NOSAVE".getBytes(Charsets.UTF_8), null); + } + + } + +}