Merge pull request #10822 from GreyTeardrop:reactive-redis-heath-connection-failure

* pr/10822:
  Polish "Handle AbstractReactiveHealthIndicator.doHealthCheck exception"
  Handle AbstractReactiveHealthIndicator.doHealthCheck exception
pull/10830/head
Stephane Nicoll 7 years ago
commit 1225a6a05e

@ -23,14 +23,24 @@ import reactor.core.publisher.Mono;
* {@link Health} instance and error handling. * {@link Health} instance and error handling.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Nikolay Rybak
* @since 2.0.0 * @since 2.0.0
*/ */
public abstract class AbstractReactiveHealthIndicator implements ReactiveHealthIndicator { public abstract class AbstractReactiveHealthIndicator implements ReactiveHealthIndicator {
@Override @Override
public final Mono<Health> health() { public final Mono<Health> health() {
try {
return doHealthCheck(new Health.Builder()) return doHealthCheck(new Health.Builder())
.onErrorResume((ex) -> Mono.just(new Health.Builder().down(ex).build())); .onErrorResume(this::handleFailure);
}
catch (Exception ex) {
return handleFailure(ex);
}
}
private Mono<Health> handleFailure(Throwable ex) {
return Mono.just(new Health.Builder().down(ex).build());
} }
/** /**

@ -18,6 +18,7 @@ package org.springframework.boot.actuate.redis;
import java.util.Properties; import java.util.Properties;
import io.lettuce.core.RedisConnectionException;
import org.junit.Test; import org.junit.Test;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
@ -39,11 +40,12 @@ import static org.mockito.Mockito.verify;
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Mark Paluch * @author Mark Paluch
* @author Nikolay Rybak
*/ */
public class RedisReactiveHealthIndicatorTests { public class RedisReactiveHealthIndicatorTests {
@Test @Test
public void redisIsUp() throws Exception { public void redisIsUp() {
Properties info = new Properties(); Properties info = new Properties();
info.put("redis_version", "2.8.9"); info.put("redis_version", "2.8.9");
ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class); ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
@ -61,7 +63,7 @@ public class RedisReactiveHealthIndicatorTests {
} }
@Test @Test
public void redisIsDown() throws Exception { public void redisCommandIsDown() {
ReactiveServerCommands commands = mock(ReactiveServerCommands.class); ReactiveServerCommands commands = mock(ReactiveServerCommands.class);
given(commands.info()).willReturn( given(commands.info()).willReturn(
Mono.error(new RedisConnectionFailureException("Connection failed"))); Mono.error(new RedisConnectionFailureException("Connection failed")));
@ -75,6 +77,20 @@ public class RedisReactiveHealthIndicatorTests {
verify(redisConnection).close(); verify(redisConnection).close();
} }
@Test
public void redisConnectionIsDown() {
ReactiveRedisConnectionFactory redisConnectionFactory = mock(
ReactiveRedisConnectionFactory.class);
given(redisConnectionFactory.getReactiveConnection()).willThrow(
new RedisConnectionException("Unable to connect to localhost:6379"));
RedisReactiveHealthIndicator healthIndicator = new RedisReactiveHealthIndicator(
redisConnectionFactory);
Mono<Health> health = healthIndicator.health();
StepVerifier.create(health)
.consumeNextWith((h) -> assertThat(h.getStatus()).isEqualTo(Status.DOWN))
.verifyComplete();
}
private RedisReactiveHealthIndicator createHealthIndicator( private RedisReactiveHealthIndicator createHealthIndicator(
ReactiveRedisConnection redisConnection, ReactiveRedisConnection redisConnection,
ReactiveServerCommands serverCommands) { ReactiveServerCommands serverCommands) {

Loading…
Cancel
Save