Skip to content

Commit

Permalink
Add doctest profile and fix testable examples
Browse files Browse the repository at this point in the history
Co-authored-by: Tihomir Mateev <[email protected]>
  • Loading branch information
uglide and tishun committed Nov 22, 2024
1 parent ad18d2a commit 8954f8e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,25 @@
</build>
</profile>

<profile>
<id>doctests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/examples/reactive/*Example.java</include>
<include>**/examples/async/*Example.java</include>
</includes>
<failIfNoTests>true</failIfNoTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>

</profiles>

</project>
20 changes: 10 additions & 10 deletions src/test/java/io/redis/examples/async/StringExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
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 {
Expand All @@ -31,13 +31,13 @@ public void run() {
CompletableFuture<Void> setAndGet = asyncCommands.set("bike:1", "Deimos").thenCompose(v -> {
System.out.println(v); // OK
// REMOVE_START
assertEquals("OK", v);
assertThat(v).isEqualTo("OK");
// REMOVE_END
return asyncCommands.get("bike:1");
})
// REMOVE_START
.thenApply(res -> {
assertEquals("Deimos", res);
assertThat(res).isEqualTo("Deimos");
return res;
})
// REMOVE_END
Expand All @@ -49,13 +49,13 @@ public void run() {
CompletableFuture<Void> setnx = asyncCommands.setnx("bike:1", "bike").thenCompose(v -> {
System.out.println(v); // false (because key already exists)
// REMOVE_START
assertEquals(false, v);
assertThat(v).isFalse();
// REMOVE_END
return asyncCommands.get("bike:1");
})
// REMOVE_START
.thenApply(res -> {
assertEquals("Deimos", res);
assertThat(res).isEqualTo("Deimos");
return res;
})
// REMOVE_END
Expand All @@ -66,7 +66,7 @@ public void run() {
CompletableFuture<Void> setxx = asyncCommands.set("bike:1", "bike", SetArgs.Builder.xx())
// REMOVE_START
.thenApply(res -> {
assertEquals("OK", res);
assertThat(res).isEqualTo("OK");
return res;
})
// REMOVE_END
Expand All @@ -89,7 +89,7 @@ public void run() {
List<KeyValue<String, String>> expected = new ArrayList<>(
Arrays.asList(KeyValue.just("bike:1", "Deimos"), KeyValue.just("bike:2", "Ares"),
KeyValue.just("bike:3", "Vanth")));
assertEquals(expected, res);
assertThat(res).isEqualTo(expected);
return res;
})
// REMOVE_END
Expand All @@ -103,13 +103,13 @@ public void run() {
.thenCompose(v -> asyncCommands.incr("total_crashes")).thenCompose(v -> {
System.out.println(v); // 1
// REMOVE_START
assertEquals(1L, v.longValue());
assertThat(v).isEqualTo(1L);
// REMOVE_END
return asyncCommands.incrby("total_crashes", 10);
})
// REMOVE_START
.thenApply(res -> {
assertEquals(11L, res.longValue());
assertThat(res).isEqualTo(11L);
return res;
})
// REMOVE_END
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/io/redis/examples/reactive/StringExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
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 {
Expand All @@ -30,11 +30,11 @@ public void run() {
Mono<Void> setAndGet = reactiveCommands.set("bike:1", "Deimos").doOnNext(v -> {
System.out.println(v); // OK
// REMOVE_START
assertEquals("OK", v);
assertThat(v).isEqualTo("OK");
// REMOVE_END
}).flatMap(v -> reactiveCommands.get("bike:1")).doOnNext(res -> {
// REMOVE_START
assertEquals("Deimos", res);
assertThat(res).isEqualTo("Deimos");
// REMOVE_END
System.out.println(res); // Deimos
}).then();
Expand All @@ -44,18 +44,18 @@ public void run() {
Mono<Void> setnx = reactiveCommands.setnx("bike:1", "bike").doOnNext(v -> {
System.out.println(v); // false (because key already exists)
// REMOVE_START
assertEquals(false, v);
assertThat(v).isFalse();
// REMOVE_END
}).flatMap(v -> reactiveCommands.get("bike:1")).doOnNext(res -> {
// REMOVE_START
assertEquals("Deimos", res);
assertThat(res).isEqualTo("Deimos");
// REMOVE_END
System.out.println(res); // Deimos (value is unchanged)
}).then();

Mono<Void> setxx = reactiveCommands.set("bike:1", "bike", SetArgs.Builder.xx()).doOnNext(res -> {
// REMOVE_START
assertEquals("OK", res);
assertThat(res).isEqualTo("OK");
// REMOVE_END
System.out.println(res); // OK
}).then();
Expand All @@ -73,7 +73,7 @@ public void run() {
Arrays.asList(KeyValue.just("bike:1", "Deimos"), KeyValue.just("bike:2", "Ares"),
KeyValue.just("bike:3", "Vanth")));
// REMOVE_START
assertEquals(expected, res);
assertThat(res).isEqualTo(expected);
// REMOVE_END
System.out.println(res); // [KeyValue[bike:1, Deimos], KeyValue[bike:2, Ares], KeyValue[bike:3, Vanth]]
}).then();
Expand All @@ -84,11 +84,11 @@ public void run() {
.doOnNext(v -> {
System.out.println(v); // 1
// REMOVE_START
assertEquals(1L, v.longValue());
assertThat(v).isEqualTo(1L);
// REMOVE_END
}).flatMap(v -> reactiveCommands.incrby("total_crashes", 10)).doOnNext(res -> {
// REMOVE_START
assertEquals(11L, res.longValue());
assertThat(res).isEqualTo(11L);
// REMOVE_END
System.out.println(res); // 11
}).then();
Expand Down

0 comments on commit 8954f8e

Please sign in to comment.