diff --git a/pom.xml b/pom.xml index 2838145a7..de4d78e59 100644 --- a/pom.xml +++ b/pom.xml @@ -86,8 +86,8 @@ scm:git:https://github.com/lettuce-io/lettuce-core.git http://github.com/lettuce-io/lettuce-core - HEAD - + HEAD + @@ -883,6 +883,7 @@ **/*UnitTests **/*Tests + **/*Example **/*Test @@ -1351,6 +1352,24 @@ + + doctests + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*Example + + true + + + + + + diff --git a/src/test/java/io/redis/examples/async/StringExample.java b/src/test/java/io/redis/examples/async/StringExample.java index bb566b2f4..9be29395b 100644 --- a/src/test/java/io/redis/examples/async/StringExample.java +++ b/src/test/java/io/redis/examples/async/StringExample.java @@ -6,125 +6,122 @@ import io.lettuce.core.api.StatefulRedisConnection; // REMOVE_START -import org.junit.Test; +import org.junit.jupiter.api.Test; // REMOVE_END import java.util.*; import java.util.concurrent.CompletableFuture; // REMOVE_START -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; // REMOVE_END public class StringExample { - // REMOVE_START - @Test - // REMOVE_END - public void run() { - RedisClient redisClient = RedisClient.create("redis://localhost:6379"); - - try (StatefulRedisConnection connection = redisClient.connect()) { - RedisAsyncCommands asyncCommands = connection.async(); - - // STEP_START set_get - CompletableFuture setAndGet = asyncCommands.set("bike:1", "Deimos") - .thenCompose(v -> { - System.out.println(v); // OK - // REMOVE_START - assertEquals("OK", v); - // REMOVE_END - return asyncCommands.get("bike:1"); - }) - // REMOVE_START - .thenApply(res -> { - assertEquals("Deimos", res); - return res; - }) - // REMOVE_END - .thenAccept(System.out::println) // Deimos - .toCompletableFuture(); - // STEP_END - - // STEP_START setnx_xx - CompletableFuture setnx = asyncCommands.setnx("bike:1", "bike") - .thenCompose(v -> { - System.out.println(v); // false (because key already exists) - // REMOVE_START - assertEquals(false, v); - // REMOVE_END - return asyncCommands.get("bike:1"); - }) - // REMOVE_START - .thenApply(res -> { - assertEquals("Deimos", res); - return res; - }) - // REMOVE_END - .thenAccept(System.out::println) // Deimos (value is unchanged) - .toCompletableFuture(); - - // set the value to "bike" if it already exists - CompletableFuture setxx = asyncCommands.set("bike:1", "bike", SetArgs.Builder.xx()) - //REMOVE_START - .thenApply(res -> { - assertEquals("OK", res); - return res; - }) - // REMOVE_END - .thenAccept(System.out::println) // OK - .toCompletableFuture(); - // STEP_END - - // STEP_START mset - Map bikeMap = new HashMap<>(); - bikeMap.put("bike:1", "Deimos"); - bikeMap.put("bike:2", "Ares"); - bikeMap.put("bike:3", "Vanth"); - - CompletableFuture mset = asyncCommands.mset(bikeMap) - .thenCompose(v -> { - System.out.println(v); // OK - return asyncCommands.mget("bike:1", "bike:2", "bike:3"); - }) - // REMOVE_START - .thenApply(res -> { - List> expected = new ArrayList<>(Arrays.asList( - KeyValue.just("bike:1", "Deimos"), - KeyValue.just("bike:2", "Ares"), - KeyValue.just("bike:3", "Vanth") - )); - assertEquals(expected, res); - return res; - }) - // REMOVE_END - .thenAccept(System.out::println) // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3, Vanth]] - .toCompletableFuture(); - // STEP_END - - // STEP_START incr - CompletableFuture incrby = asyncCommands.set("total_crashes", "0") - .thenCompose(v -> asyncCommands.incr("total_crashes")) - .thenCompose(v -> { - System.out.println(v); // 1 - // REMOVE_START - assertEquals(1L, v.longValue()); - // REMOVE_END - return asyncCommands.incrby("total_crashes", 10); - }) - // REMOVE_START - .thenApply(res -> { - assertEquals(11L, res.longValue()); - return res; - }) - // REMOVE_END - .thenAccept(System.out::println) // 11 - .toCompletableFuture(); - // STEP_END - - CompletableFuture.allOf(setAndGet, setnx, setxx, mset, incrby).join(); - - } finally { - redisClient.shutdown(); + + // REMOVE_START + @Test + // REMOVE_END + public void run() { + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); + + try (StatefulRedisConnection connection = redisClient.connect()) { + RedisAsyncCommands asyncCommands = connection.async(); + + // STEP_START set_get + CompletableFuture setAndGet = asyncCommands.set("bike:1", "Deimos").thenCompose(v -> { + System.out.println(v); // OK + // REMOVE_START + assertThat(v).isEqualTo("OK"); + // REMOVE_END + return asyncCommands.get("bike:1"); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo("Deimos"); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // Deimos + .toCompletableFuture(); + // STEP_END + + // STEP_START setnx_xx + CompletableFuture setnx = asyncCommands.setnx("bike:1", "bike").thenCompose(v -> { + System.out.println(v); // false (because key already exists) + // REMOVE_START + assertThat(v).isFalse(); + // REMOVE_END + return asyncCommands.get("bike:1"); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo("Deimos"); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // Deimos (value is unchanged) + .toCompletableFuture(); + + // set the value to "bike" if it already exists + CompletableFuture setxx = asyncCommands.set("bike:1", "bike", SetArgs.Builder.xx()) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo("OK"); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // OK + .toCompletableFuture(); + // STEP_END + + // STEP_START mset + Map bikeMap = new HashMap<>(); + bikeMap.put("bike:1", "Deimos"); + bikeMap.put("bike:2", "Ares"); + bikeMap.put("bike:3", "Vanth"); + + CompletableFuture mset = asyncCommands.mset(bikeMap).thenCompose(v -> { + System.out.println(v); // OK + return asyncCommands.mget("bike:1", "bike:2", "bike:3"); + }) + // REMOVE_START + .thenApply(res -> { + List> expected = new ArrayList<>( + Arrays.asList(KeyValue.just("bike:1", "Deimos"), KeyValue.just("bike:2", "Ares"), + KeyValue.just("bike:3", "Vanth"))); + assertThat(res).isEqualTo(expected); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3, + // Vanth]] + .toCompletableFuture(); + // STEP_END + + // STEP_START incr + CompletableFuture incrby = asyncCommands.set("total_crashes", "0") + .thenCompose(v -> asyncCommands.incr("total_crashes")).thenCompose(v -> { + System.out.println(v); // 1 + // REMOVE_START + assertThat(v).isEqualTo(1L); + // REMOVE_END + return asyncCommands.incrby("total_crashes", 10); + }) + // REMOVE_START + .thenApply(res -> { + assertThat(res).isEqualTo(11L); + return res; + }) + // REMOVE_END + .thenAccept(System.out::println) // 11 + .toCompletableFuture(); + // STEP_END + + CompletableFuture.allOf(setAndGet, setnx, setxx, mset, incrby).join(); + + } finally { + redisClient.shutdown(); + } } - } + } diff --git a/src/test/java/io/redis/examples/reactive/StringExample.java b/src/test/java/io/redis/examples/reactive/StringExample.java index 16c2d45cb..24d2443e4 100644 --- a/src/test/java/io/redis/examples/reactive/StringExample.java +++ b/src/test/java/io/redis/examples/reactive/StringExample.java @@ -5,117 +5,100 @@ import io.lettuce.core.api.reactive.RedisReactiveCommands; import io.lettuce.core.api.StatefulRedisConnection; // REMOVE_START -import org.junit.Test; +import org.junit.jupiter.api.Test; // REMOVE_END import reactor.core.publisher.Mono; import java.util.*; // REMOVE_START -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; // REMOVE_END public class StringExample { - // REMOVE_START - @Test - // REMOVE_END - public void run() { - RedisClient redisClient = RedisClient.create("redis://localhost:6379"); - try (StatefulRedisConnection connection = redisClient.connect()) { - RedisReactiveCommands reactiveCommands = connection.reactive(); + // REMOVE_START + @Test + // REMOVE_END + public void run() { + RedisClient redisClient = RedisClient.create("redis://localhost:6379"); - // STEP_START set_get - Mono setAndGet = reactiveCommands.set("bike:1", "Deimos") - .doOnNext(v -> { - System.out.println(v); // OK - // REMOVE_START - assertEquals("OK", v); - // REMOVE_END - }) - .flatMap(v -> reactiveCommands.get("bike:1")) - .doOnNext(res -> { - // REMOVE_START - assertEquals("Deimos", res); - // REMOVE_END - System.out.println(res); // Deimos - }) - .then(); - // STEP_END + try (StatefulRedisConnection connection = redisClient.connect()) { + RedisReactiveCommands reactiveCommands = connection.reactive(); - // STEP_START setnx_xx - Mono setnx = reactiveCommands.setnx("bike:1", "bike") - .doOnNext(v -> { - System.out.println(v); // false (because key already exists) - // REMOVE_START - assertEquals(false, v); - // REMOVE_END - }) - .flatMap(v -> reactiveCommands.get("bike:1")) - .doOnNext(res -> { - // REMOVE_START - assertEquals("Deimos", res); - // REMOVE_END - System.out.println(res); // Deimos (value is unchanged) - }) - .then(); + // STEP_START set_get + Mono setAndGet = reactiveCommands.set("bike:1", "Deimos").doOnNext(v -> { + System.out.println(v); // OK + // REMOVE_START + assertThat(v).isEqualTo("OK"); + // REMOVE_END + }).flatMap(v -> reactiveCommands.get("bike:1")).doOnNext(res -> { + // REMOVE_START + assertThat(res).isEqualTo("Deimos"); + // REMOVE_END + System.out.println(res); // Deimos + }).then(); + // STEP_END - Mono setxx = reactiveCommands.set("bike:1", "bike", SetArgs.Builder.xx()) - .doOnNext(res -> { - // REMOVE_START - assertEquals("OK", res); - // REMOVE_END - System.out.println(res); // OK - }) - .then(); - // STEP_END + // STEP_START setnx_xx + Mono setnx = reactiveCommands.setnx("bike:1", "bike").doOnNext(v -> { + System.out.println(v); // false (because key already exists) + // REMOVE_START + assertThat(v).isFalse(); + // REMOVE_END + }).flatMap(v -> reactiveCommands.get("bike:1")).doOnNext(res -> { + // REMOVE_START + assertThat(res).isEqualTo("Deimos"); + // REMOVE_END + System.out.println(res); // Deimos (value is unchanged) + }).then(); - // STEP_START mset - Map bikeMap = new HashMap<>(); - bikeMap.put("bike:1", "Deimos"); - bikeMap.put("bike:2", "Ares"); - bikeMap.put("bike:3", "Vanth"); + Mono setxx = reactiveCommands.set("bike:1", "bike", SetArgs.Builder.xx()).doOnNext(res -> { + // REMOVE_START + assertThat(res).isEqualTo("OK"); + // REMOVE_END + System.out.println(res); // OK + }).then(); + // STEP_END - Mono mset = reactiveCommands.mset(bikeMap) - .doOnNext(System.out::println) // OK - .flatMap(v -> reactiveCommands.mget("bike:1", "bike:2", "bike:3").collectList()) - .doOnNext(res -> { - List> expected = new ArrayList<>(Arrays.asList( - KeyValue.just("bike:1", "Deimos"), - KeyValue.just("bike:2", "Ares"), - KeyValue.just("bike:3", "Vanth") - )); - // REMOVE_START - assertEquals(expected, res); - // REMOVE_END - System.out.println(res); // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3, Vanth]] - }) - .then(); - // STEP_END + // STEP_START mset + Map bikeMap = new HashMap<>(); + bikeMap.put("bike:1", "Deimos"); + bikeMap.put("bike:2", "Ares"); + bikeMap.put("bike:3", "Vanth"); - // STEP_START incr - Mono incrby = reactiveCommands.set("total_crashes", "0") - .flatMap(v -> reactiveCommands.incr("total_crashes")) - .doOnNext(v -> { - System.out.println(v); // 1 - // REMOVE_START - assertEquals(1L, v.longValue()); - // REMOVE_END - }) - .flatMap(v -> reactiveCommands.incrby("total_crashes", 10)) - .doOnNext(res -> { - // REMOVE_START - assertEquals(11L, res.longValue()); - // REMOVE_END - System.out.println(res); // 11 - }) - .then(); - // STEP_END + Mono mset = reactiveCommands.mset(bikeMap).doOnNext(System.out::println) // OK + .flatMap(v -> reactiveCommands.mget("bike:1", "bike:2", "bike:3").collectList()).doOnNext(res -> { + List> expected = new ArrayList<>( + Arrays.asList(KeyValue.just("bike:1", "Deimos"), KeyValue.just("bike:2", "Ares"), + KeyValue.just("bike:3", "Vanth"))); + // REMOVE_START + assertThat(res).isEqualTo(expected); + // REMOVE_END + System.out.println(res); // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3, Vanth]] + }).then(); + // STEP_END - Mono.when(setAndGet, setnx, setxx, mset, incrby).block(); + // STEP_START incr + Mono incrby = reactiveCommands.set("total_crashes", "0").flatMap(v -> reactiveCommands.incr("total_crashes")) + .doOnNext(v -> { + System.out.println(v); // 1 + // REMOVE_START + assertThat(v).isEqualTo(1L); + // REMOVE_END + }).flatMap(v -> reactiveCommands.incrby("total_crashes", 10)).doOnNext(res -> { + // REMOVE_START + assertThat(res).isEqualTo(11L); + // REMOVE_END + System.out.println(res); // 11 + }).then(); + // STEP_END - } finally { - redisClient.shutdown(); + Mono.when(setAndGet, setnx, setxx, mset, incrby).block(); + + } finally { + redisClient.shutdown(); + } } - } -} \ No newline at end of file + +}