Merge pull request #10153 from mp911de:close-redis-connection

* pr/10153:
  Close reactive Redis connection after health check
pull/10153/merge
Stephane Nicoll 7 years ago
commit 6975e1b783

@ -18,12 +18,14 @@ package org.springframework.boot.actuate.health;
import reactor.core.publisher.Mono;
import org.springframework.data.redis.connection.ReactiveRedisConnection;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
/**
* A {@link ReactiveHealthIndicator} for Redis.
*
* @author Stephane Nicoll
* @author Mark Paluch
* @since 2.0.0
*/
public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
@ -37,10 +39,14 @@ public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicato
@Override
protected Mono<Health> doHealthCheck(Health.Builder builder) {
return this.connectionFactory.getReactiveConnection().serverCommands().info()
.map(info -> builder.up().withDetail(
RedisHealthIndicator.VERSION, info.getProperty(
RedisHealthIndicator.REDIS_VERSION)).build());
ReactiveRedisConnection connection = this.connectionFactory
.getReactiveConnection();
return connection.serverCommands().info()
.map(info -> builder.up()
.withDetail(RedisHealthIndicator.VERSION,
info.getProperty(RedisHealthIndicator.REDIS_VERSION))
.build())
.doFinally(signal -> connection.close());
}
}

@ -30,11 +30,13 @@ import org.springframework.data.redis.connection.ReactiveServerCommands;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link RedisReactiveHealthIndicator}.
*
* @author Stephane Nicoll
* @author Mark Paluch
*/
public class RedisReactiveHealthIndicatorTests {
@ -42,15 +44,17 @@ public class RedisReactiveHealthIndicatorTests {
public void redisIsUp() throws Exception {
Properties info = new Properties();
info.put("redis_version", "2.8.9");
ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
ReactiveServerCommands commands = mock(ReactiveServerCommands.class);
given(commands.info()).willReturn(Mono.just(info));
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(commands);
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(redisConnection, commands);
Mono<Health> health = healthIndicator.health();
StepVerifier.create(health).consumeNextWith(h -> {
assertThat(h.getStatus()).isEqualTo(Status.UP);
assertThat(h.getDetails()).containsOnlyKeys("version");
assertThat(h.getDetails().get("version")).isEqualTo("2.8.9");
}).verifyComplete();
verify(redisConnection).close();
}
@Test
@ -58,15 +62,18 @@ public class RedisReactiveHealthIndicatorTests {
ReactiveServerCommands commands = mock(ReactiveServerCommands.class);
given(commands.info()).willReturn(Mono.error(
new RedisConnectionFailureException("Connection failed")));
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(commands);
ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(redisConnection, commands);
Mono<Health> health = healthIndicator.health();
StepVerifier.create(health)
.expectError(RedisConnectionFailureException.class);
StepVerifier.create(health).consumeNextWith(h -> {
assertThat(h.getStatus()).isEqualTo(Status.DOWN);
}).verifyComplete();
verify(redisConnection).close();
}
private RedisReactiveHealthIndicator createHealthIndicator(
ReactiveServerCommands serverCommands) {
ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
ReactiveRedisConnection redisConnection, ReactiveServerCommands serverCommands) {
ReactiveRedisConnectionFactory redisConnectionFactory = mock(
ReactiveRedisConnectionFactory.class);
given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection);

Loading…
Cancel
Save