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 reactor.core.publisher.Mono;
import org.springframework.data.redis.connection.ReactiveRedisConnection;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
/** /**
* A {@link ReactiveHealthIndicator} for Redis. * A {@link ReactiveHealthIndicator} for Redis.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Mark Paluch
* @since 2.0.0 * @since 2.0.0
*/ */
public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicator { public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicator {
@ -37,10 +39,14 @@ public class RedisReactiveHealthIndicator extends AbstractReactiveHealthIndicato
@Override @Override
protected Mono<Health> doHealthCheck(Health.Builder builder) { protected Mono<Health> doHealthCheck(Health.Builder builder) {
return this.connectionFactory.getReactiveConnection().serverCommands().info() ReactiveRedisConnection connection = this.connectionFactory
.map(info -> builder.up().withDetail( .getReactiveConnection();
RedisHealthIndicator.VERSION, info.getProperty( return connection.serverCommands().info()
RedisHealthIndicator.REDIS_VERSION)).build()); .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.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/** /**
* Tests for {@link RedisReactiveHealthIndicator}. * Tests for {@link RedisReactiveHealthIndicator}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Mark Paluch
*/ */
public class RedisReactiveHealthIndicatorTests { public class RedisReactiveHealthIndicatorTests {
@ -42,15 +44,17 @@ public class RedisReactiveHealthIndicatorTests {
public void redisIsUp() throws Exception { public void redisIsUp() throws Exception {
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);
ReactiveServerCommands commands = mock(ReactiveServerCommands.class); ReactiveServerCommands commands = mock(ReactiveServerCommands.class);
given(commands.info()).willReturn(Mono.just(info)); given(commands.info()).willReturn(Mono.just(info));
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(commands); RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(redisConnection, commands);
Mono<Health> health = healthIndicator.health(); Mono<Health> health = healthIndicator.health();
StepVerifier.create(health).consumeNextWith(h -> { StepVerifier.create(health).consumeNextWith(h -> {
assertThat(h.getStatus()).isEqualTo(Status.UP); assertThat(h.getStatus()).isEqualTo(Status.UP);
assertThat(h.getDetails()).containsOnlyKeys("version"); assertThat(h.getDetails()).containsOnlyKeys("version");
assertThat(h.getDetails().get("version")).isEqualTo("2.8.9"); assertThat(h.getDetails().get("version")).isEqualTo("2.8.9");
}).verifyComplete(); }).verifyComplete();
verify(redisConnection).close();
} }
@Test @Test
@ -58,15 +62,18 @@ public class RedisReactiveHealthIndicatorTests {
ReactiveServerCommands commands = mock(ReactiveServerCommands.class); ReactiveServerCommands commands = mock(ReactiveServerCommands.class);
given(commands.info()).willReturn(Mono.error( given(commands.info()).willReturn(Mono.error(
new RedisConnectionFailureException("Connection failed"))); new RedisConnectionFailureException("Connection failed")));
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(commands); ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
RedisReactiveHealthIndicator healthIndicator = createHealthIndicator(redisConnection, commands);
Mono<Health> health = healthIndicator.health(); Mono<Health> health = healthIndicator.health();
StepVerifier.create(health) StepVerifier.create(health).consumeNextWith(h -> {
.expectError(RedisConnectionFailureException.class); assertThat(h.getStatus()).isEqualTo(Status.DOWN);
}).verifyComplete();
verify(redisConnection).close();
} }
private RedisReactiveHealthIndicator createHealthIndicator( private RedisReactiveHealthIndicator createHealthIndicator(
ReactiveServerCommands serverCommands) { ReactiveRedisConnection redisConnection, ReactiveServerCommands serverCommands) {
ReactiveRedisConnection redisConnection = mock(ReactiveRedisConnection.class);
ReactiveRedisConnectionFactory redisConnectionFactory = mock( ReactiveRedisConnectionFactory redisConnectionFactory = mock(
ReactiveRedisConnectionFactory.class); ReactiveRedisConnectionFactory.class);
given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection); given(redisConnectionFactory.getReactiveConnection()).willReturn(redisConnection);

Loading…
Cancel
Save