diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicator.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicator.java index 48e42d7bd4..6805f66045 100644 --- a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicator.java +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicator.java @@ -91,10 +91,6 @@ public PubSubHealthIndicator( this.acknowledgeMessages = acknowledgeMessages; } - void validateHealthCheck() { - doHealthCheck(() -> {}, this::validationFailed, this::validationFailed); - } - @Override protected void doHealthCheck(Health.Builder builder) { doHealthCheck(builder::up, builder::down, e -> builder.withException(e).unknown()); @@ -144,10 +140,6 @@ private boolean isHealthyResponseForUnspecifiedSubscription(ExecutionException e return false; } - private void validationFailed(Throwable e) { - throw new BeanInitializationException("Validation of health indicator failed", e); - } - boolean isSpecifiedSubscription() { return specifiedSubscription; } diff --git a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorAutoConfiguration.java b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorAutoConfiguration.java index b5a588d9e6..c7fc2ad76e 100644 --- a/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorAutoConfiguration.java +++ b/spring-cloud-gcp-autoconfigure/src/main/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorAutoConfiguration.java @@ -71,7 +71,6 @@ protected PubSubHealthIndicator createIndicator(PubSubTemplate pubSubTemplate) { this.pubSubHealthProperties.getSubscription(), this.pubSubHealthProperties.getTimeoutMillis(), this.pubSubHealthProperties.isAcknowledgeMessages()); - indicator.validateHealthCheck(); return indicator; } } diff --git a/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorAutoConfigurationTests.java b/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorAutoConfigurationTests.java index 85b6fc29f6..289edc43ff 100644 --- a/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorAutoConfigurationTests.java +++ b/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorAutoConfigurationTests.java @@ -71,10 +71,6 @@ class PubSubHealthIndicatorAutoConfigurationTests { @Test void healthIndicatorPresent_defaults() throws Exception { PubSubTemplate mockPubSubTemplate = mock(PubSubTemplate.class); - ListenableFuture> future = mock(ListenableFuture.class); - - when(future.get(anyLong(), any())).thenReturn(Collections.emptyList()); - when(mockPubSubTemplate.pullAsync(anyString(), anyInt(), anyBoolean())).thenReturn(future); this.baseContextRunner .withBean("pubSubTemplate", PubSubTemplate.class, () -> mockPubSubTemplate) @@ -86,19 +82,13 @@ void healthIndicatorPresent_defaults() throws Exception { assertThat(healthIndicator.getTimeoutMillis()).isEqualTo(2000); assertThat(healthIndicator.isAcknowledgeMessages()).isFalse(); assertThat(healthIndicator.isSpecifiedSubscription()).isFalse(); - verify(mockPubSubTemplate).pullAsync(healthIndicator.getSubscription(), 1, true); - verify(future).get(healthIndicator.getTimeoutMillis(), TimeUnit.MILLISECONDS); }); } @SuppressWarnings("unchecked") @Test - void healthIndicatorPresent_customConfig() throws Exception { + void healthIndicatorPresent_customConfig() { PubSubTemplate mockPubSubTemplate = mock(PubSubTemplate.class); - ListenableFuture> future = mock(ListenableFuture.class); - - when(future.get(anyLong(), any())).thenReturn(Collections.emptyList()); - when(mockPubSubTemplate.pullAsync(anyString(), anyInt(), anyBoolean())).thenReturn(future); this.baseContextRunner .withBean("pubSubTemplate", PubSubTemplate.class, () -> mockPubSubTemplate) @@ -115,8 +105,6 @@ void healthIndicatorPresent_customConfig() throws Exception { assertThat(healthIndicator.getTimeoutMillis()).isEqualTo(1500); assertThat(healthIndicator.isAcknowledgeMessages()).isTrue(); assertThat(healthIndicator.isSpecifiedSubscription()).isTrue(); - verify(mockPubSubTemplate).pullAsync(healthIndicator.getSubscription(), 1, true); - verify(future).get(healthIndicator.getTimeoutMillis(), TimeUnit.MILLISECONDS); }); } @@ -153,96 +141,6 @@ void compositeHealthIndicatorPresentMultiplePubSubTemplate() throws Exception { }); } - @SuppressWarnings("unchecked") - @Test - void apiExceptionWhenValidating_userSubscriptionSpecified_healthAutoConfigurationFails() - throws Exception { - PubSubHealthIndicatorProperties properties = new PubSubHealthIndicatorProperties(); - PubSubHealthIndicatorAutoConfiguration p = - new PubSubHealthIndicatorAutoConfiguration(properties); - properties.setSubscription("test"); - - PubSubTemplate mockPubSubTemplate = mock(PubSubTemplate.class); - ListenableFuture> future = mock(ListenableFuture.class); - Exception e = - new ApiException( - new IllegalStateException("Illegal State"), - GrpcStatusCode.of(io.grpc.Status.Code.NOT_FOUND), - false); - - when(mockPubSubTemplate.pullAsync(anyString(), anyInt(), anyBoolean())).thenReturn(future); - doThrow(new ExecutionException(e)).when(future).get(anyLong(), any()); - - Map pubSubTemplates = - Collections.singletonMap("pubSubTemplate", mockPubSubTemplate); - assertThatThrownBy(() -> p.pubSubHealthContributor(pubSubTemplates)) - .isInstanceOf(BeanInitializationException.class); - } - - @SuppressWarnings("unchecked") - @Test - void apiExceptionWhenValidating_userSubscriptionNotSpecified_healthAutoConfigurationSucceeds() - throws Exception { - PubSubHealthIndicatorProperties properties = new PubSubHealthIndicatorProperties(); - PubSubHealthIndicatorAutoConfiguration p = - new PubSubHealthIndicatorAutoConfiguration(properties); - - PubSubTemplate mockPubSubTemplate = mock(PubSubTemplate.class); - ListenableFuture> future = mock(ListenableFuture.class); - Exception e = - new ApiException( - new IllegalStateException("Illegal State"), - GrpcStatusCode.of(io.grpc.Status.Code.NOT_FOUND), - false); - - when(mockPubSubTemplate.pullAsync(anyString(), anyInt(), anyBoolean())).thenReturn(future); - doThrow(new ExecutionException(e)).when(future).get(anyLong(), any()); - - Map pubSubTemplates = - Collections.singletonMap("pubSubTemplate", mockPubSubTemplate); - assertThatCode(() -> p.pubSubHealthContributor(pubSubTemplates)).doesNotThrowAnyException(); - } - - @SuppressWarnings("unchecked") - @Test - void runtimeExceptionWhenValidating_healthAutoConfigurationFails() throws Exception { - PubSubHealthIndicatorProperties properties = new PubSubHealthIndicatorProperties(); - PubSubHealthIndicatorAutoConfiguration p = - new PubSubHealthIndicatorAutoConfiguration(properties); - - PubSubTemplate mockPubSubTemplate = mock(PubSubTemplate.class); - ListenableFuture> future = mock(ListenableFuture.class); - Exception e = new RuntimeException("Runtime exception"); - - when(mockPubSubTemplate.pullAsync(anyString(), anyInt(), anyBoolean())).thenReturn(future); - doThrow(e).when(future).get(anyLong(), any()); - - Map pubSubTemplates = - Collections.singletonMap("pubSubTemplate", mockPubSubTemplate); - assertThatThrownBy(() -> p.pubSubHealthContributor(pubSubTemplates)) - .isInstanceOf(BeanInitializationException.class); - } - - @SuppressWarnings("unchecked") - @Test - void interruptedExceptionWhenValidating_healthAutoConfigurationFails() throws Exception { - PubSubHealthIndicatorProperties properties = new PubSubHealthIndicatorProperties(); - PubSubHealthIndicatorAutoConfiguration p = - new PubSubHealthIndicatorAutoConfiguration(properties); - - PubSubTemplate mockPubSubTemplate = mock(PubSubTemplate.class); - ListenableFuture> future = mock(ListenableFuture.class); - InterruptedException e = new InterruptedException("interrupted"); - - when(mockPubSubTemplate.pullAsync(anyString(), anyInt(), anyBoolean())).thenReturn(future); - doThrow(e).when(future).get(anyLong(), any()); - - Map pubSubTemplates = - Collections.singletonMap("pubSubTemplate", mockPubSubTemplate); - assertThatThrownBy(() -> p.pubSubHealthContributor(pubSubTemplates)) - .isInstanceOf(BeanInitializationException.class); - } - @Test void healthCheckConfigurationBacksOffWhenHealthIndicatorBeanPresent() { PubSubHealthIndicator userHealthIndicator = mock(PubSubHealthIndicator.class); diff --git a/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorTests.java b/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorTests.java index 9459d4c92a..2ace28f1df 100644 --- a/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorTests.java +++ b/spring-cloud-gcp-autoconfigure/src/test/java/com/google/cloud/spring/autoconfigure/pubsub/health/PubSubHealthIndicatorTests.java @@ -157,14 +157,4 @@ void customSubscription_RuntimeException() throws Exception { testHealth(e, "testSubscription", Status.DOWN); } - @Test - void validateHealth() throws Exception { - doThrow(new RuntimeException()).when(future).get(anyLong(), any()); - when(pubSubTemplate.pullAsync(anyString(), anyInt(), anyBoolean())).thenReturn(future); - - PubSubHealthIndicator healthIndicator = - new PubSubHealthIndicator(pubSubTemplate, "test", 1000, true); - assertThatThrownBy(() -> healthIndicator.validateHealthCheck()) - .isInstanceOf(BeanInitializationException.class); - } }