Skip to content

Commit

Permalink
DOC-4802 fix string example concurrency (redis#3156)
Browse files Browse the repository at this point in the history
* DOC-4802 fixed example concurrency with join() calls

* DOC-4802 applied formatting
  • Loading branch information
andy-stark-redis authored Feb 7, 2025
1 parent 6ad045b commit c420392
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/test/java/io/redis/examples/async/StringExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@

// REMOVE_START
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
// REMOVE_END

import java.util.*;
import java.util.concurrent.CompletableFuture;

// REMOVE_START
import static org.assertj.core.api.Assertions.assertThat;
// REMOVE_END

public class StringExample {

// REMOVE_START
Expand All @@ -29,7 +26,7 @@ public void run() {

// STEP_START set_get
CompletableFuture<Void> setAndGet = asyncCommands.set("bike:1", "Deimos").thenCompose(v -> {
System.out.println(v); // OK
System.out.println(v); // >>> OK
// REMOVE_START
assertThat(v).isEqualTo("OK");
// REMOVE_END
Expand All @@ -41,13 +38,16 @@ public void run() {
return res;
})
// REMOVE_END
.thenAccept(System.out::println) // Deimos
.thenAccept(System.out::println) // >>> Deimos
.toCompletableFuture();
// STEP_END
// HIDE_START
setAndGet.join();
// HIDE_END

// STEP_START setnx_xx
CompletableFuture<Void> setnx = asyncCommands.setnx("bike:1", "bike").thenCompose(v -> {
System.out.println(v); // false (because key already exists)
System.out.println(v); // >>> false (because key already exists)
// REMOVE_START
assertThat(v).isFalse();
// REMOVE_END
Expand All @@ -59,8 +59,11 @@ public void run() {
return res;
})
// REMOVE_END
.thenAccept(System.out::println) // Deimos (value is unchanged)
.thenAccept(System.out::println) // >>> Deimos (value is unchanged)
.toCompletableFuture();
// HIDE_START
setnx.join();
// HIDE_END

// set the value to "bike" if it already exists
CompletableFuture<Void> setxx = asyncCommands.set("bike:1", "bike", SetArgs.Builder.xx())
Expand All @@ -70,8 +73,11 @@ public void run() {
return res;
})
// REMOVE_END
.thenAccept(System.out::println) // OK
.thenAccept(System.out::println) // >>> OK
.toCompletableFuture();
// HIDE_START
setxx.join();
// HIDE_END
// STEP_END

// STEP_START mset
Expand All @@ -81,7 +87,7 @@ public void run() {
bikeMap.put("bike:3", "Vanth");

CompletableFuture<Void> mset = asyncCommands.mset(bikeMap).thenCompose(v -> {
System.out.println(v); // OK
System.out.println(v); // >>> OK
return asyncCommands.mget("bike:1", "bike:2", "bike:3");
})
// REMOVE_START
Expand All @@ -93,15 +99,19 @@ public void run() {
return res;
})
// REMOVE_END
.thenAccept(System.out::println) // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3,
// Vanth]]
.thenAccept(System.out::println)
// >>> [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3,
// Vanth]]
.toCompletableFuture();
// STEP_END
// HIDE_START
mset.join();
// HIDE_END

// STEP_START incr
CompletableFuture<Void> incrby = asyncCommands.set("total_crashes", "0")
.thenCompose(v -> asyncCommands.incr("total_crashes")).thenCompose(v -> {
System.out.println(v); // 1
System.out.println(v); // >>> 1
// REMOVE_START
assertThat(v).isEqualTo(1L);
// REMOVE_END
Expand All @@ -113,12 +123,12 @@ public void run() {
return res;
})
// REMOVE_END
.thenAccept(System.out::println) // 11
.thenAccept(System.out::println) // >>> 11
.toCompletableFuture();
// STEP_END

CompletableFuture.allOf(setAndGet, setnx, setxx, mset, incrby).join();

// HIDE_START
incrby.join();
// HIDE_END
} finally {
redisClient.shutdown();
}
Expand Down

0 comments on commit c420392

Please sign in to comment.