From fe8080487341d6b1608849c5f35ab9ede0960580 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jiahe=20Cai=20=28=E4=BD=B3=E4=BD=95=20=E8=94=A1=29?= <739416782@qq.com> Date: Tue, 6 Mar 2018 14:08:04 +0800 Subject: [PATCH] =?UTF-8?q?DATAREDIS-779=20-=20Emit=20false=20if=20a=20con?= =?UTF-8?q?ditional=20ReactiveStringCommands.set(=E2=80=A6)=20does=20not?= =?UTF-8?q?=20set=20a=20value.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now emit false when SET XX/SET NX complete without setting the value because the condition was evaluated to not set the value. Previously, we did not emit a value as Redis responds null (nil). This behavior becomes visible through ReactiveValueOperations.set[ifPresent|ifAbsent](…) methods. This is a breaking change in the sense that changes the character of ReactiveValueOperations.set[ifPresent|ifAbsent](…) from an optionally emitted element to always emit. Reactive sequences that currently resume operation with .switchIfEmpty(…) will no longer utilize the fallback publisher. Original pull request: #317. --- .../connection/lettuce/LettuceReactiveStringCommands.java | 5 ++++- .../core/DefaultReactiveValueOperationsIntegrationTests.java | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveStringCommands.java b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveStringCommands.java index 6449c2a07..015e58fcb 100644 --- a/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveStringCommands.java +++ b/src/main/java/org/springframework/data/redis/connection/lettuce/LettuceReactiveStringCommands.java @@ -37,6 +37,7 @@ import org.springframework.util.Assert; /** * @author Christoph Strobl * @author Mark Paluch + * @author Jiahe Cai * @since 2.0 */ class LettuceReactiveStringCommands implements ReactiveStringCommands { @@ -93,7 +94,9 @@ class LettuceReactiveStringCommands implements ReactiveStringCommands { Mono mono = args != null ? cmd.set(command.getKey(), command.getValue(), args) : cmd.set(command.getKey(), command.getValue()); - return mono.map(LettuceConverters::stringToBoolean).map((value) -> new BooleanResponse<>(command, value)); + return mono.map(LettuceConverters::stringToBoolean) + .map(value -> new BooleanResponse<>(command, value)) + .switchIfEmpty(Mono.just(new BooleanResponse<>(command, Boolean.FALSE))); })); } diff --git a/src/test/java/org/springframework/data/redis/core/DefaultReactiveValueOperationsIntegrationTests.java b/src/test/java/org/springframework/data/redis/core/DefaultReactiveValueOperationsIntegrationTests.java index 463923de6..7d558e4a8 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultReactiveValueOperationsIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultReactiveValueOperationsIntegrationTests.java @@ -45,6 +45,7 @@ import org.springframework.data.redis.serializer.StringRedisSerializer; * * @author Mark Paluch * @author Christoph Strobl + * @author Jiahe Cai */ @RunWith(Parameterized.class) @SuppressWarnings("unchecked") @@ -131,7 +132,7 @@ public class DefaultReactiveValueOperationsIntegrationTests { StepVerifier.create(valueOperations.setIfAbsent(key, value)).expectNext(true).verifyComplete(); - StepVerifier.create(valueOperations.setIfAbsent(key, value)).verifyComplete(); + StepVerifier.create(valueOperations.setIfAbsent(key, value)).expectNext(false).verifyComplete(); } @Test // DATAREDIS-602 @@ -141,7 +142,7 @@ public class DefaultReactiveValueOperationsIntegrationTests { V value = valueFactory.instance(); V laterValue = valueFactory.instance(); - StepVerifier.create(valueOperations.setIfPresent(key, value)).verifyComplete(); + StepVerifier.create(valueOperations.setIfPresent(key, value)).expectNext(false).verifyComplete(); StepVerifier.create(valueOperations.set(key, value)).expectNext(true).verifyComplete();