Add test cases for computeAsync, improve fix

Signed-off-by: Yordan Grancharov <phrone@gmail.com>
pull/6040/head
Yordan Grancharov 8 months ago
parent 02d9fdc836
commit f02ed8751e

@ -258,11 +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) {
return fastPutAsync(key, newValue)
.thenApply(rr -> newValue);
if (newValue == null) {
if (oldValue != null) {
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