Skip to content

Commit b95928c

Browse files
committed
Polishing.
Refactor UUID generation offloading and ensure the creation completes before running subsequent actions. See #666
1 parent 392c3e4 commit b95928c

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

redis/reactive/src/test/java/example/springdata/redis/commands/KeyCommandsTests.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -15,16 +15,19 @@
1515
*/
1616
package example.springdata.redis.commands;
1717

18+
import example.springdata.redis.RedisTestConfiguration;
19+
import reactor.core.publisher.Flux;
20+
import reactor.core.scheduler.Schedulers;
21+
import reactor.test.StepVerifier;
22+
1823
import java.nio.ByteBuffer;
1924
import java.time.Duration;
2025
import java.util.Collections;
2126
import java.util.UUID;
22-
import java.util.concurrent.ExecutorService;
23-
import java.util.concurrent.Executors;
2427

25-
import example.springdata.redis.RedisTestConfiguration;
2628
import org.junit.jupiter.api.BeforeEach;
2729
import org.junit.jupiter.api.Test;
30+
2831
import org.springframework.beans.factory.annotation.Autowired;
2932
import org.springframework.boot.test.context.SpringBootTest;
3033
import org.springframework.data.redis.connection.ReactiveRedisConnection;
@@ -33,8 +36,6 @@
3336
import org.springframework.data.redis.serializer.RedisSerializer;
3437
import org.springframework.data.redis.serializer.StringRedisSerializer;
3538
import org.springframework.data.redis.util.ByteUtils;
36-
import reactor.core.publisher.Flux;
37-
import reactor.test.StepVerifier;
3839

3940
/**
4041
* Show usage of reactive operations on Redis keys using low level API provided by
@@ -48,7 +49,6 @@ class KeyCommandsTests {
4849

4950
private static final String PREFIX = KeyCommandsTests.class.getSimpleName();
5051
private static final String KEY_PATTERN = PREFIX + "*";
51-
private final ExecutorService executor = Executors.newSingleThreadExecutor();
5252

5353
@Autowired ReactiveRedisConnectionFactory connectionFactory;
5454

@@ -75,7 +75,7 @@ void iterateOverKeysMatchingPrefixUsingKeysCommand() {
7575
.flatMapMany(Flux::fromIterable) //
7676
.doOnNext(byteBuffer -> System.out.println(toString(byteBuffer))) //
7777
.count() //
78-
.doOnSuccess(count -> System.out.println(String.format("Total No. found: %s", count)));
78+
.doOnSuccess(count -> System.out.printf("Total No. found: %s%n", count));
7979

8080
keyCount.as(StepVerifier::create).expectNext(50L).verifyComplete();
8181
}
@@ -86,8 +86,8 @@ void iterateOverKeysMatchingPrefixUsingKeysCommand() {
8686
@Test
8787
void storeToListAndPop() {
8888

89-
var popResult = connection.listCommands()
90-
.brPop(Collections.singletonList(ByteBuffer.wrap("list".getBytes())), Duration.ofSeconds(5));
89+
var popResult = connection.listCommands().brPop(Collections.singletonList(ByteBuffer.wrap("list".getBytes())),
90+
Duration.ofSeconds(5));
9191

9292
var llen = connection.listCommands().lLen(ByteBuffer.wrap("list".getBytes()));
9393

@@ -96,23 +96,21 @@ void storeToListAndPop() {
9696
.flatMap(l -> popResult) //
9797
.doOnNext(result -> System.out.println(toString(result.getValue()))) //
9898
.flatMap(result -> llen) //
99-
.doOnNext(count -> System.out.println(String.format("Total items in list left: %s", count)));//
99+
.doOnNext(count -> System.out.printf("Total items in list left: %s%n", count));//
100100

101101
popAndLlen.as(StepVerifier::create).expectNext(0L).verifyComplete();
102102
}
103103

104104
private void generateRandomKeys(int nrKeys) {
105105

106-
executor.execute(() -> {
107-
var keyFlux = Flux.range(0, nrKeys).map(i -> (PREFIX + "-" + i));
108-
109-
var generator = keyFlux.map(String::getBytes).map(ByteBuffer::wrap) //
110-
.map(key -> SetCommand.set(key) //
111-
.value(ByteBuffer.wrap(UUID.randomUUID().toString().getBytes())));
106+
var keyFlux = Flux.range(0, nrKeys).map(i -> (PREFIX + "-" + i)) //
107+
.publishOn(Schedulers.single()) //
108+
.map(it -> SetCommand.set(ByteBuffer.wrap(it.getBytes())) //
109+
.value(ByteBuffer.wrap(UUID.randomUUID().toString().getBytes())));
112110

113-
connection.stringCommands().set(generator).as(StepVerifier::create) //
111+
connection.stringCommands().set(keyFlux).as(StepVerifier::create) //
114112
.expectNextCount(nrKeys) //
115-
.verifyComplete();});
113+
.verifyComplete();
116114

117115
}
118116

0 commit comments

Comments
 (0)