Skip to content

Commit

Permalink
remove startup pubsub validation (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
elefeint committed Mar 22, 2022
1 parent 6d22a8a commit 5c5ae46
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ protected PubSubHealthIndicator createIndicator(PubSubTemplate pubSubTemplate) {
this.pubSubHealthProperties.getSubscription(),
this.pubSubHealthProperties.getTimeoutMillis(),
this.pubSubHealthProperties.isAcknowledgeMessages());
indicator.validateHealthCheck();
return indicator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ class PubSubHealthIndicatorAutoConfigurationTests {
@Test
void healthIndicatorPresent_defaults() throws Exception {
PubSubTemplate mockPubSubTemplate = mock(PubSubTemplate.class);
ListenableFuture<List<AcknowledgeablePubsubMessage>> 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)
Expand All @@ -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<List<AcknowledgeablePubsubMessage>> 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)
Expand All @@ -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);
});
}

Expand Down Expand Up @@ -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<List<AcknowledgeablePubsubMessage>> 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<String, PubSubTemplate> 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<List<AcknowledgeablePubsubMessage>> 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<String, PubSubTemplate> 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<List<AcknowledgeablePubsubMessage>> 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<String, PubSubTemplate> 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<List<AcknowledgeablePubsubMessage>> 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<String, PubSubTemplate> pubSubTemplates =
Collections.singletonMap("pubSubTemplate", mockPubSubTemplate);
assertThatThrownBy(() -> p.pubSubHealthContributor(pubSubTemplates))
.isInstanceOf(BeanInitializationException.class);
}

@Test
void healthCheckConfigurationBacksOffWhenHealthIndicatorBeanPresent() {
PubSubHealthIndicator userHealthIndicator = mock(PubSubHealthIndicator.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 5c5ae46

Please sign in to comment.