use of io.lettuce.core.internal.AsyncCloseable in project lettuce-core by lettuce-io.
the class AbstractRedisClient method closeResources.
private CompletableFuture<Void> closeResources() {
List<CompletionStage<Void>> closeFutures = new ArrayList<>();
List<Closeable> closeableResources = new ArrayList<>(this.closeableResources);
for (Closeable closeableResource : closeableResources) {
if (closeableResource instanceof AsyncCloseable) {
closeFutures.add(((AsyncCloseable) closeableResource).closeAsync());
} else {
try {
closeableResource.close();
} catch (Exception e) {
logger.debug("Exception on Close: " + e.getMessage(), e);
}
}
this.closeableResources.remove(closeableResource);
}
for (Channel c : channels.toArray(new Channel[0])) {
if (c == null) {
continue;
}
ChannelPipeline pipeline = c.pipeline();
ConnectionWatchdog commandHandler = pipeline.get(ConnectionWatchdog.class);
if (commandHandler != null) {
commandHandler.setListenOnChannelInactive(false);
}
}
try {
closeFutures.add(Futures.toCompletionStage(channels.close()));
} catch (Exception e) {
logger.debug("Cannot close channels", e);
}
return Futures.allOf(closeFutures);
}
use of io.lettuce.core.internal.AsyncCloseable in project lettuce-core by lettuce-io.
the class RedisClientUnitTests method shutdownShouldDeferResourcesShutdown.
@Test
void shutdownShouldDeferResourcesShutdown() throws Exception {
when(clientResources.eventExecutorGroup()).thenReturn(ImmediateEventExecutor.INSTANCE);
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
when(asyncCloseable.closeAsync()).thenReturn(completableFuture);
RedisClient redisClient = RedisClient.create(clientResources, "redis://foo");
Field field = AbstractRedisClient.class.getDeclaredField("sharedResources");
field.setAccessible(true);
field.set(redisClient, false);
Set<AsyncCloseable> closeableResources = (Set) ReflectionTestUtils.getField(redisClient, "closeableResources");
closeableResources.add(asyncCloseable);
CompletableFuture<Void> future = redisClient.shutdownAsync();
verify(asyncCloseable).closeAsync();
verify(clientResources, never()).shutdown(anyLong(), anyLong(), any());
assertThat(future).isNotDone();
}
use of io.lettuce.core.internal.AsyncCloseable in project lettuce-core by lettuce-io.
the class RedisClientUnitTests method shutdownShutsDownResourcesAfterChannels.
@Test
void shutdownShutsDownResourcesAfterChannels() throws Exception {
when(clientResources.eventExecutorGroup()).thenReturn(ImmediateEventExecutor.INSTANCE);
CompletableFuture<Void> completableFuture = new CompletableFuture<>();
when(asyncCloseable.closeAsync()).thenReturn(completableFuture);
RedisClient redisClient = RedisClient.create(clientResources, "redis://foo");
Field field = AbstractRedisClient.class.getDeclaredField("sharedResources");
field.setAccessible(true);
field.set(redisClient, false);
Set<AsyncCloseable> closeableResources = (Set) ReflectionTestUtils.getField(redisClient, "closeableResources");
closeableResources.add(asyncCloseable);
CompletableFuture<Void> future = redisClient.shutdownAsync();
verify(asyncCloseable).closeAsync();
verify(clientResources, never()).shutdown(anyLong(), anyLong(), any());
completableFuture.complete(null);
verify(clientResources).shutdown(anyLong(), anyLong(), any());
assertThat(future).isDone();
}
Aggregations