Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all articles
Browse latest Browse all 2204

How to test exceptionally here and how to improve this code?

$
0
0

I am new to CompletableFuture and Redis. I am using Lettuce Client to connect to Redis (in Java). I want some guidance on how to test the if exception is thrown correctly.

private final BoundedAsyncPool<StatefulRedisClusterConnection<String, ResponseValue >> connectionPool;public CompletableFuture<List<ResponseValue>> read(final Request request) {    if (CollectionUtils.isNullOrEmpty(request.getKeys())) {        return CompletableFuture.supplyAsync(() -> ImmutableList.of(), executorService);    }    return connectionPool            .acquire()            .thenComposeAsync(                    connection -> executeAsync(connection, request.getKeys()),                    executorService);}private CompletionStage<List<ResponseValue>> executeAsync(        final StatefulRedisClusterConnection<String, ResponseValue> connection,        final Set<String> keys) {    final RedisFuture<List<KeyValue<String, ResponseValue>>> readFuture = connection.async().mget(keys.toArray(String[]::new));    return readFuture.toCompletableFuture()            .exceptionally( ex -> {                connectionPool.release(connection);                log.warn("Error executing Redis command: " + ex.getMessage());                return ImmutableList.of();            })            .thenApplyAsync(                    value -> {                        connectionPool.release(connection);                        return value.stream()                                .filter(KeyValue::hasValue)                                .map(response -> response.getValue())                                .collect(Collectors.toUnmodifiableList());                        }, executorService).toCompletableFuture();}`

My questionss:

  1. should I release connectionPool twice?
  2. How to test the exceptionally? I tried adding the test case but it is just returning empty list and releasing the connectionPool twice but I am not able to assert if exception is getting thrown or not.
  3. Is there any improvement/suggestions for my code

Viewing all articles
Browse latest Browse all 2204

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>