Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions src/test/java/io/redis/examples/async/CmdsSetExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// EXAMPLE: cmds_set
package io.redis.examples.async;

import io.lettuce.core.*;
import io.lettuce.core.api.async.RedisAsyncCommands;
import io.lettuce.core.api.StatefulRedisConnection;

// REMOVE_START
import org.junit.jupiter.api.Test;
// REMOVE_END
import java.util.concurrent.CompletableFuture;
import static java.util.stream.Collectors.toList;
// REMOVE_START
import static org.assertj.core.api.Assertions.assertThat;
// REMOVE_END

public class CmdsSetExample {

@Test
public void run() {
RedisClient redisClient = RedisClient.create("redis://localhost:6379");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RedisClient is AutoClosable and can be put inside try() instead calling shutdown() manually.


try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
RedisAsyncCommands<String, String> asyncCommands = connection.async();
// REMOVE_START
CompletableFuture<Long> delResult = asyncCommands.del("myset").toCompletableFuture();
delResult.join();
// REMOVE_END

// STEP_START sadd
CompletableFuture<Void> sadd = asyncCommands.sadd("myset", "Hello").thenCompose(r -> {
System.out.println(r); // >>> 1
// REMOVE_START
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it sounds dramatic! :-) It just means that this line shouldn't appear in the published example on the docs page (we avoid showing the users the asserts that we test the examples with).

assertThat(r).isEqualTo(1);
// REMOVE_END
return asyncCommands.sadd("myset", "World");
}).thenCompose(r -> {
System.out.println(r); // >>> 1
// REMOVE_START
assertThat(r).isEqualTo(1);
// REMOVE_END
return asyncCommands.sadd("myset", "World");
}).thenCompose(r -> {
System.out.println(r); // >>> 0
// REMOVE_START
assertThat(r).isEqualTo(0);
// REMOVE_END
return asyncCommands.smembers("myset");
})
// REMOVE_START
.thenApply(r -> {
assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[Hello, World]");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be replaced with assertThat(r).containsExactlyInAnyOrder("Hello", "World")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, that's a great tip, thanks! The streams thing borrows from what we were doing with the Jedis examples, but your suggestion is much neater :-)

return r;
})
// REMOVE_END
.thenAccept(System.out::println)
// >>> [Hello, World]
.toCompletableFuture();
// STEP_END
// HIDE_START
sadd.join();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead joins i would prefer the approach with CompletableFuture.allOf() like in HashExample

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We originally did this to avoid the code snippets getting executed out-of-order during testing (some of them use results from previous snippets and so tests fail if a snippet executes before another snippet that it depends on). If this is no longer a problem, or doesn't apply in this case then I'll use allOf, as you suggest.

TBH, we might have to revisit this stuff carefully now we've got Jupyter notebooks for some of the examples (via the "Run in browser" link). Dependencies between the cells can be a problem with the notebooks too.

// HIDE_END
// REMOVE_START
CompletableFuture<Long> delSaddResult = asyncCommands.del("myset").toCompletableFuture();
delSaddResult.join();
// REMOVE_END

// STEP_START smembers
CompletableFuture<Void> smembers = asyncCommands.sadd("myset", "Hello", "World").thenCompose(r -> {
return asyncCommands.smembers("myset");
})
// REMOVE_START
.thenApply(r -> {
assertThat(r.stream().sorted().collect(toList()).toString()).isEqualTo("[Hello, World]");
return r;
})
// REMOVE_END
.thenAccept(System.out::println)
// >>> [Hello, World]
.toCompletableFuture();
// STEP_END
// HIDE_START
smembers.join();
// HIDE_END
} finally {
redisClient.shutdown();
}
}

}
80 changes: 80 additions & 0 deletions src/test/java/io/redis/examples/reactive/CmdsSetExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// EXAMPLE: cmds_set
package io.redis.examples.reactive;

import io.lettuce.core.*;
import io.lettuce.core.api.reactive.RedisReactiveCommands;
import io.lettuce.core.api.StatefulRedisConnection;

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

public class CmdsSetExample {

@Test
public void run() {
RedisClient redisClient = RedisClient.create("redis://localhost:6379");

try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
RedisReactiveCommands<String, String> reactiveCommands = connection.reactive();
// REMOVE_START
// Clean up any existing data
Mono<Void> cleanup = reactiveCommands.del("myset").then();
cleanup.block();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like in async we can wrap the block operations in Mono.when()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was the out-of-order execution during testing again, but as I said for async, I'll certainly change this if it's not a problem here.

// REMOVE_END

// STEP_START sadd
Mono<Void> sadd = reactiveCommands.sadd("myset", "Hello").doOnNext(r -> {
System.out.println(r); // >>> 1
// REMOVE_START
assertThat(r).isEqualTo(1);
// REMOVE_END
}).flatMap(r -> reactiveCommands.sadd("myset", "World")).doOnNext(r -> {
System.out.println(r); // >>> 1
// REMOVE_START
assertThat(r).isEqualTo(1);
// REMOVE_END
}).flatMap(r -> reactiveCommands.sadd("myset", "World")).doOnNext(r -> {
System.out.println(r); // >>> 0
// REMOVE_START
assertThat(r).isEqualTo(0);
// REMOVE_END
}).flatMap(r -> reactiveCommands.smembers("myset").collectList()).doOnNext(r -> {
System.out.println(r); // >>> [Hello, World]
// REMOVE_START
assertThat(r).containsExactlyInAnyOrder("Hello", "World");
// REMOVE_END
}).then();
// STEP_END
// HIDE_START
sadd.block();
// HIDE_END
// REMOVE_START
Mono<Long> delSaddResult = reactiveCommands.del("myset");
delSaddResult.block();
// REMOVE_END

// STEP_START smembers
Mono<Void> smembers = reactiveCommands.sadd("myset", "Hello", "World").doOnNext(r -> {
System.out.println(r); // >>> 2
// REMOVE_START
assertThat(r).isEqualTo(2);
// REMOVE_END
}).flatMap(r -> reactiveCommands.smembers("myset").collectList()).doOnNext(r -> {
System.out.println(r); // >>> [Hello, World]
// REMOVE_START
assertThat(r).containsExactlyInAnyOrder("Hello", "World");
// REMOVE_END
}).then();
// STEP_END
// HIDE_START
smembers.block();
// HIDE_END
} finally {
redisClient.shutdown();
}
}

}
Loading