Skip to content

Commit c420392

Browse files
DOC-4802 fix string example concurrency (#3156)
* DOC-4802 fixed example concurrency with join() calls * DOC-4802 applied formatting
1 parent 6ad045b commit c420392

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

src/test/java/io/redis/examples/async/StringExample.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77

88
// REMOVE_START
99
import org.junit.jupiter.api.Test;
10+
import static org.assertj.core.api.Assertions.assertThat;
1011
// REMOVE_END
1112

1213
import java.util.*;
1314
import java.util.concurrent.CompletableFuture;
1415

15-
// REMOVE_START
16-
import static org.assertj.core.api.Assertions.assertThat;
17-
// REMOVE_END
18-
1916
public class StringExample {
2017

2118
// REMOVE_START
@@ -29,7 +26,7 @@ public void run() {
2926

3027
// STEP_START set_get
3128
CompletableFuture<Void> setAndGet = asyncCommands.set("bike:1", "Deimos").thenCompose(v -> {
32-
System.out.println(v); // OK
29+
System.out.println(v); // >>> OK
3330
// REMOVE_START
3431
assertThat(v).isEqualTo("OK");
3532
// REMOVE_END
@@ -41,13 +38,16 @@ public void run() {
4138
return res;
4239
})
4340
// REMOVE_END
44-
.thenAccept(System.out::println) // Deimos
41+
.thenAccept(System.out::println) // >>> Deimos
4542
.toCompletableFuture();
4643
// STEP_END
44+
// HIDE_START
45+
setAndGet.join();
46+
// HIDE_END
4747

4848
// STEP_START setnx_xx
4949
CompletableFuture<Void> setnx = asyncCommands.setnx("bike:1", "bike").thenCompose(v -> {
50-
System.out.println(v); // false (because key already exists)
50+
System.out.println(v); // >>> false (because key already exists)
5151
// REMOVE_START
5252
assertThat(v).isFalse();
5353
// REMOVE_END
@@ -59,8 +59,11 @@ public void run() {
5959
return res;
6060
})
6161
// REMOVE_END
62-
.thenAccept(System.out::println) // Deimos (value is unchanged)
62+
.thenAccept(System.out::println) // >>> Deimos (value is unchanged)
6363
.toCompletableFuture();
64+
// HIDE_START
65+
setnx.join();
66+
// HIDE_END
6467

6568
// set the value to "bike" if it already exists
6669
CompletableFuture<Void> setxx = asyncCommands.set("bike:1", "bike", SetArgs.Builder.xx())
@@ -70,8 +73,11 @@ public void run() {
7073
return res;
7174
})
7275
// REMOVE_END
73-
.thenAccept(System.out::println) // OK
76+
.thenAccept(System.out::println) // >>> OK
7477
.toCompletableFuture();
78+
// HIDE_START
79+
setxx.join();
80+
// HIDE_END
7581
// STEP_END
7682

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

8389
CompletableFuture<Void> mset = asyncCommands.mset(bikeMap).thenCompose(v -> {
84-
System.out.println(v); // OK
90+
System.out.println(v); // >>> OK
8591
return asyncCommands.mget("bike:1", "bike:2", "bike:3");
8692
})
8793
// REMOVE_START
@@ -93,15 +99,19 @@ public void run() {
9399
return res;
94100
})
95101
// REMOVE_END
96-
.thenAccept(System.out::println) // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3,
97-
// Vanth]]
102+
.thenAccept(System.out::println)
103+
// >>> [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3,
104+
// Vanth]]
98105
.toCompletableFuture();
99106
// STEP_END
107+
// HIDE_START
108+
mset.join();
109+
// HIDE_END
100110

101111
// STEP_START incr
102112
CompletableFuture<Void> incrby = asyncCommands.set("total_crashes", "0")
103113
.thenCompose(v -> asyncCommands.incr("total_crashes")).thenCompose(v -> {
104-
System.out.println(v); // 1
114+
System.out.println(v); // >>> 1
105115
// REMOVE_START
106116
assertThat(v).isEqualTo(1L);
107117
// REMOVE_END
@@ -113,12 +123,12 @@ public void run() {
113123
return res;
114124
})
115125
// REMOVE_END
116-
.thenAccept(System.out::println) // 11
126+
.thenAccept(System.out::println) // >>> 11
117127
.toCompletableFuture();
118128
// STEP_END
119-
120-
CompletableFuture.allOf(setAndGet, setnx, setxx, mset, incrby).join();
121-
129+
// HIDE_START
130+
incrby.join();
131+
// HIDE_END
122132
} finally {
123133
redisClient.shutdown();
124134
}

0 commit comments

Comments
 (0)