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,
1515 */
1616package 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+
1823import java .nio .ByteBuffer ;
1924import java .time .Duration ;
2025import java .util .Collections ;
2126import java .util .UUID ;
22- import java .util .concurrent .ExecutorService ;
23- import java .util .concurrent .Executors ;
2427
25- import example .springdata .redis .RedisTestConfiguration ;
2628import org .junit .jupiter .api .BeforeEach ;
2729import org .junit .jupiter .api .Test ;
30+
2831import org .springframework .beans .factory .annotation .Autowired ;
2932import org .springframework .boot .test .context .SpringBootTest ;
3033import org .springframework .data .redis .connection .ReactiveRedisConnection ;
3336import org .springframework .data .redis .serializer .RedisSerializer ;
3437import org .springframework .data .redis .serializer .StringRedisSerializer ;
3538import 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