Merge pull request #6040 from phrone/phr/fix-computeAsync

Fix computeAsync for empty keys
pull/6045/head
Nikita Koksharov 8 months ago committed by GitHub
commit 88d1635719
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -258,14 +258,14 @@ public class RedissonMap<K, V> extends RedissonExpirable implements RMap<K, V> {
return oldValueFuture.thenCompose(oldValue -> {
return CompletableFuture.supplyAsync(() -> remappingFunction.apply(key, oldValue), getServiceManager().getExecutor())
.thenCompose(newValue -> {
if (newValue != null) {
if (newValue == null) {
if (oldValue != null) {
return fastPutAsync(key, newValue)
return fastRemoveAsync(key)
.thenApply(rr -> newValue);
}
return CompletableFuture.completedFuture(newValue);
}
return fastRemoveAsync(key)
return fastPutAsync(key, newValue)
.thenApply(rr -> newValue);
});
}).whenComplete((c, e) -> {

@ -269,14 +269,43 @@ public abstract class BaseMapTest extends RedisDockerTest {
@Test
public void testCompute() {
RMap<String, String> map = getMap("map");
map.compute("1", (key, oldValue) -> {
return "12";
});
assertThat(map.get("1")).isEqualTo("12");
map.compute("1", (key, oldValue) -> {
return (oldValue == null) ? "12" : oldValue.concat("34");
});
assertThat(map.get("1")).isEqualTo("1234");
map.compute("1", (key, oldValue) -> {
return null;
});
assertThat(map.get("1")).isNull();
}
@Test
public void testComputeAsync() {
RMap<String, String> map = getMap("mapAsync");
RFuture<String> res1 = map.computeAsync("1", (key, oldValue) -> {
return "12";
});
assertThat(res1.toCompletableFuture().join()).isEqualTo("12");
assertThat(map.get("1")).isEqualTo("12");
RFuture<String> res2 = map.computeAsync("1", (key, oldValue) -> {
return (oldValue == null) ? "12" : oldValue.concat("34");
});
assertThat(res2.toCompletableFuture().join()).isEqualTo("1234");
assertThat(map.get("1")).isEqualTo("1234");
RFuture<String> res3 = map.computeAsync("1", (key, oldValue) -> {
return null;
});
assertThat(res3.toCompletableFuture().join()).isNull();
assertThat(map.get("1")).isNull();
}

Loading…
Cancel
Save