Skip to content

Commit

Permalink
Polishing #998
Browse files Browse the repository at this point in the history
Add author tags. Reformat. Add unit tests.

Original pull request: #1000
  • Loading branch information
mp911de committed Mar 14, 2019
1 parent 30cfd1d commit f677da6
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/main/java/io/lettuce/core/AbstractRedisClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
*
* @author Mark Paluch
* @author Jongyeol Choi
* @author Poorva Gokhale
* @since 3.0
* @see ClientResources
*/
Expand Down Expand Up @@ -433,16 +434,14 @@ public CompletableFuture<Void> shutdownAsync(long quietPeriod, long timeout, Tim
if (shutdown.compareAndSet(false, true)) {

logger.debug("Initiate shutdown ({}, {}, {})", quietPeriod, timeout, timeUnit);

return closeOtherResources().thenCompose((value) ->
closeClientResources(quietPeriod, timeout, timeUnit)
);
return closeResources().thenCompose((value) -> closeClientResources(quietPeriod, timeout, timeUnit));
}

return completedFuture(null);
}

private CompletableFuture<Void> closeOtherResources() {
private CompletableFuture<Void> closeResources() {

List<CompletableFuture<Void>> closeFutures = new ArrayList<>();

while (!closeableResources.isEmpty()) {
Expand Down
99 changes: 99 additions & 0 deletions src/test/java/io/lettuce/core/RedisClientUnitTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.lettuce.core;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.Closeable;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.util.ReflectionTestUtils;

import io.lettuce.core.internal.AsyncCloseable;
import io.lettuce.core.resource.ClientResources;
import io.netty.util.concurrent.ImmediateEventExecutor;

/**
* Unit tests for {@link RedisClient}.
*
* @author Mark Paluch
*/
@SuppressWarnings("unchecked")
@ExtendWith(MockitoExtension.class)
class RedisClientUnitTests {

@Mock
ClientResources clientResources;

@Mock(extraInterfaces = Closeable.class)
AsyncCloseable asyncCloseable;

@Test
void shutdownShouldDeferResourcesShutdown() {

when(clientResources.eventExecutorGroup()).thenReturn(ImmediateEventExecutor.INSTANCE);

CompletableFuture<Void> completableFuture = new CompletableFuture<>();
when(asyncCloseable.closeAsync()).thenReturn(completableFuture);

RedisClient redisClient = RedisClient.create(clientResources, "redis://foo");
ReflectionTestUtils.setField(redisClient, "sharedResources", 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();
}

@Test
void shutdownShutsDownResourcesAfterChannels() {

when(clientResources.eventExecutorGroup()).thenReturn(ImmediateEventExecutor.INSTANCE);

CompletableFuture<Void> completableFuture = new CompletableFuture<>();
when(asyncCloseable.closeAsync()).thenReturn(completableFuture);

RedisClient redisClient = RedisClient.create(clientResources, "redis://foo");
ReflectionTestUtils.setField(redisClient, "sharedResources", 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();
}
}

0 comments on commit f677da6

Please sign in to comment.