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:
- should I release connectionPool twice?
- 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.
- Is there any improvement/suggestions for my code