Search in sources :

Example 1 with AsyncCloseable

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);
}
Also used : ConnectionWatchdog(io.lettuce.core.protocol.ConnectionWatchdog) AsyncCloseable(io.lettuce.core.internal.AsyncCloseable) AsyncCloseable(io.lettuce.core.internal.AsyncCloseable) Closeable(java.io.Closeable) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) CompletionStage(java.util.concurrent.CompletionStage) CancellationException(java.util.concurrent.CancellationException) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 2 with AsyncCloseable

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();
}
Also used : Field(java.lang.reflect.Field) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncCloseable(io.lettuce.core.internal.AsyncCloseable) Set(java.util.Set) Test(org.junit.jupiter.api.Test)

Example 3 with AsyncCloseable

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();
}
Also used : Field(java.lang.reflect.Field) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncCloseable(io.lettuce.core.internal.AsyncCloseable) Set(java.util.Set) Test(org.junit.jupiter.api.Test)

Aggregations

AsyncCloseable (io.lettuce.core.internal.AsyncCloseable)3 Field (java.lang.reflect.Field)2 Set (java.util.Set)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Test (org.junit.jupiter.api.Test)2 ConnectionWatchdog (io.lettuce.core.protocol.ConnectionWatchdog)1 Channel (io.netty.channel.Channel)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 Closeable (java.io.Closeable)1 ArrayList (java.util.ArrayList)1 CancellationException (java.util.concurrent.CancellationException)1 CompletionStage (java.util.concurrent.CompletionStage)1