From 5688d56eabc95b4238a2eab092b417100a5fb638 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 14 Jun 2018 16:01:25 +0200 Subject: [PATCH] DATAREDIS-708 - Move BGREWRITEAOF and BGSAVE tests to unit tests. Run tests against Lettuce connection mocks to not interfere with running Redis background processes. Original Pull Request: #347 --- ...uceReactiveClusterServerCommandsTests.java | 13 --- ...activeRedisClusterConnectionUnitTests.java | 89 +++++++++++++++++++ ...ttuceReactiveRedisConnectionUnitTests.java | 23 +++++ .../LettuceReactiveServerCommandsTests.java | 16 ---- 4 files changed, 112 insertions(+), 29 deletions(-) create mode 100644 src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisClusterConnectionUnitTests.java diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveClusterServerCommandsTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveClusterServerCommandsTests.java index 8583e3183..a9ae27d52 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveClusterServerCommandsTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveClusterServerCommandsTests.java @@ -19,7 +19,6 @@ import static org.assertj.core.api.Assertions.*; import static org.springframework.data.redis.connection.ClusterTestVariables.*; import static org.springframework.data.redis.connection.lettuce.LettuceReactiveCommandsTestsBase.*; -import org.junit.Ignore; import reactor.test.StepVerifier; import org.junit.Test; @@ -40,18 +39,6 @@ public class LettuceReactiveClusterServerCommandsTests extends LettuceReactiveCl StepVerifier.create(connection.ping(NODE1)).expectNext("PONG").verifyComplete(); } - @Test // DATAREDIS-659 - @Ignore("DATAREDIS-708, Causes ERR Background append only file rewriting already") - public void bgReWriteAofShouldRespondCorrectly() { - StepVerifier.create(connection.serverCommands().bgReWriteAof(NODE1)).expectNextCount(1).verifyComplete(); - } - - @Test // DATAREDIS-659 - @Ignore("DATAREDIS-708, Causes ERR Background append only file rewriting already") - public void bgSaveShouldRespondCorrectly() { - StepVerifier.create(connection.serverCommands().bgSave(NODE1)).expectNextCount(1).verifyComplete(); - } - @Test // DATAREDIS-659 public void lastSaveShouldRespondCorrectly() { StepVerifier.create(connection.serverCommands().lastSave(NODE1)).expectNextCount(1).verifyComplete(); diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisClusterConnectionUnitTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisClusterConnectionUnitTests.java new file mode 100644 index 000000000..ffd58401a --- /dev/null +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisClusterConnectionUnitTests.java @@ -0,0 +1,89 @@ +/* + * Copyright 2018 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.ArgumentMatchers.any; +import static org.mockito.Mockito.*; +import static org.mockito.Mockito.anyInt; +import static org.mockito.Mockito.anyString; +import static org.springframework.data.redis.connection.ClusterTestVariables.*; + +import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.reactive.RedisReactiveCommands; +import io.lettuce.core.cluster.RedisClusterClient; +import io.lettuce.core.cluster.api.StatefulRedisClusterConnection; +import io.lettuce.core.cluster.api.reactive.RedisAdvancedClusterReactiveCommands; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import java.nio.ByteBuffer; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.data.redis.connection.RedisClusterNode; +import org.springframework.data.redis.connection.lettuce.LettuceConnectionProvider.TargetAware; + +/** + * Unit tests for {@link LettuceReactiveRedisClusterConnection}. + * + * @author Mark Paluch + */ +@RunWith(MockitoJUnitRunner.class) +public class LettuceReactiveRedisClusterConnectionUnitTests { + + static final RedisClusterNode NODE1 = new RedisClusterNode(CLUSTER_HOST, MASTER_NODE_1_PORT); + + @Mock StatefulRedisClusterConnection sharedConnection; + @Mock StatefulRedisConnection nodeConnection; + + @Mock RedisClusterClient clusterClient; + @Mock RedisAdvancedClusterReactiveCommands reactiveCommands; + @Mock RedisReactiveCommands reactiveNodeCommands; + @Mock(extraInterfaces = TargetAware.class) LettuceConnectionProvider connectionProvider; + + @Before + public void before() { + + when(connectionProvider.getConnection(any())).thenReturn(sharedConnection); + when(sharedConnection.getConnection(anyString(), anyInt())).thenReturn(nodeConnection); + when(nodeConnection.reactive()).thenReturn(reactiveNodeCommands); + } + + @Test // DATAREDIS-659, DATAREDIS-708 + public void bgReWriteAofShouldRespondCorrectly() { + + LettuceReactiveRedisClusterConnection connection = new LettuceReactiveRedisClusterConnection(connectionProvider, + clusterClient); + + when(reactiveNodeCommands.bgrewriteaof()).thenReturn(Mono.just("OK")); + + StepVerifier.create(connection.serverCommands().bgReWriteAof(NODE1)).expectNextCount(1).verifyComplete(); + } + + @Test // DATAREDIS-659, DATAREDIS-708 + public void bgSaveShouldRespondCorrectly() { + + LettuceReactiveRedisClusterConnection connection = new LettuceReactiveRedisClusterConnection(connectionProvider, + clusterClient); + + when(reactiveNodeCommands.bgsave()).thenReturn(Mono.just("OK")); + + StepVerifier.create(connection.serverCommands().bgSave(NODE1)).expectNextCount(1).verifyComplete(); + } +} diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisConnectionUnitTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisConnectionUnitTests.java index 4c53e29dc..011f87800 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisConnectionUnitTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveRedisConnectionUnitTests.java @@ -21,6 +21,7 @@ import static org.mockito.Mockito.*; import io.lettuce.core.RedisConnectionException; import io.lettuce.core.api.StatefulConnection; import io.lettuce.core.api.StatefulRedisConnection; +import io.lettuce.core.api.reactive.RedisReactiveCommands; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -47,11 +48,13 @@ public class LettuceReactiveRedisConnectionUnitTests { @Mock(answer = Answers.RETURNS_MOCKS) StatefulRedisConnection sharedConnection; + @Mock RedisReactiveCommands reactiveCommands; @Mock LettuceConnectionProvider connectionProvider; @Before public void before() { when(connectionProvider.getConnection(any())).thenReturn(sharedConnection); + when(sharedConnection.reactive()).thenReturn(reactiveCommands); } @Test // DATAREDIS-720 @@ -195,4 +198,24 @@ public class LettuceReactiveRedisConnectionUnitTests { connection.getConnection(); } + + @Test // DATAREDIS-659, DATAREDIS-708 + public void bgReWriteAofShouldRespondCorrectly() { + + LettuceReactiveRedisConnection connection = new LettuceReactiveRedisConnection(connectionProvider); + + when(reactiveCommands.bgrewriteaof()).thenReturn(Mono.just("OK")); + + StepVerifier.create(connection.serverCommands().bgReWriteAof()).expectNextCount(1).verifyComplete(); + } + + @Test // DATAREDIS-659, DATAREDIS-667, DATAREDIS-708 + public void bgSaveShouldRespondCorrectly() { + + LettuceReactiveRedisConnection connection = new LettuceReactiveRedisConnection(connectionProvider); + + when(reactiveCommands.bgsave()).thenReturn(Mono.just("OK")); + + StepVerifier.create(connection.serverCommands().bgSave()).expectNextCount(1).verifyComplete(); + } } diff --git a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveServerCommandsTests.java b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveServerCommandsTests.java index 1583c7172..da08bff7f 100644 --- a/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveServerCommandsTests.java +++ b/src/test/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveServerCommandsTests.java @@ -18,7 +18,6 @@ package org.springframework.data.redis.connection.lettuce; import static org.assertj.core.api.Assertions.*; import static org.junit.Assume.*; -import org.junit.Ignore; import reactor.test.StepVerifier; import org.junit.Test; @@ -34,21 +33,6 @@ public class LettuceReactiveServerCommandsTests extends LettuceReactiveCommandsT StepVerifier.create(connection.ping()).expectNext("PONG").verifyComplete(); } - @Test // DATAREDIS-659 - @Ignore("DATAREDIS-708, Causes ERR Background append only file rewriting already") - public void bgReWriteAofShouldRespondCorrectly() { - StepVerifier.create(connection.serverCommands().bgReWriteAof()).expectNextCount(1).verifyComplete(); - } - - @Test // DATAREDIS-659, DATAREDIS-667 - @Ignore("DATAREDIS-708, Causes ERR Background append only file rewriting already") - public void bgSaveShouldRespondCorrectly() { - - assumeTrue(connectionProvider instanceof StandaloneConnectionProvider); - - StepVerifier.create(connection.serverCommands().bgSave()).expectNextCount(1).verifyComplete(); - } - @Test // DATAREDIS-659 public void lastSaveShouldRespondCorrectly() { StepVerifier.create(connection.serverCommands().lastSave()).expectNextCount(1).verifyComplete();