Skip to content

Commit

Permalink
restart the mock server after a connection failure is detected to spe…
Browse files Browse the repository at this point in the history
…ed up tests.

Rather than waiting ~20s to give up retrying.
  • Loading branch information
SamBarker committed Jul 22, 2024
1 parent 0b17eda commit 99cacc3
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

public abstract class AbstractInterceptorTest {

private static final Duration FUTURE_COMPLETION_TIME = Duration.of(10, ChronoUnit.SECONDS);
private static DefaultMockServer server;

@BeforeEach
Expand Down Expand Up @@ -179,6 +180,7 @@ public CompletableFuture<Boolean> afterFailure(BasicBuilder builder, HttpRespons
@DisplayName("afterConnectionFailure, invoked when remote server offline")
public void afterConnectionFailureRemoteOffline() {
// Given
final int originalPort = server.getPort();
server.shutdown();
final CountDownLatch connectionFailureCallbackInvoked = new CountDownLatch(1);
final HttpClient.Builder builder = getHttpClientFactory().newBuilder()
Expand All @@ -187,6 +189,8 @@ public void afterConnectionFailureRemoteOffline() {
@Override
public void afterConnectionFailure(HttpRequest request, Throwable failure) {
connectionFailureCallbackInvoked.countDown();
server = newMockServer();
server.start(originalPort); // Need to restart on the original port as we can't alter the request during retry.
}
});
// When
Expand All @@ -196,7 +200,7 @@ public void afterConnectionFailure(HttpRequest request, Throwable failure) {
.uri(server.url("/not-found")).build(), String.class);

// Then
assertThat(response).failsWithin(Duration.of(30, ChronoUnit.SECONDS));
assertThat(response).succeedsWithin(FUTURE_COMPLETION_TIME);
assertThat(connectionFailureCallbackInvoked).extracting(CountDownLatch::getCount).isEqualTo(0L);
}
}
Expand All @@ -214,8 +218,8 @@ public void afterConnectionFailureRetry() {
@Override
public void afterConnectionFailure(HttpRequest request, Throwable failure) {
server = newMockServer();
server.expect().withPath("/intercepted-url").andReturn(200, "This works").once();
server.start(originalPort); // Need to restart on the original port as we can't alter the request during retry.
server.expect().withPath("/intercepted-url").andReturn(200, "This works").once();
}

@Override
Expand All @@ -230,7 +234,7 @@ public void after(HttpRequest request, HttpResponse<?> response, Consumer<List<B
.uri(server.url("/intercepted-url")).build(), String.class);

// Then
assertThat(response).succeedsWithin(Duration.of(30, ChronoUnit.SECONDS));
assertThat(response).succeedsWithin(FUTURE_COMPLETION_TIME);
assertThat(afterInvoked).extracting(CountDownLatch::getCount).isEqualTo(0L);
}
}
Expand Down Expand Up @@ -261,7 +265,7 @@ public int read() {
}, 1L).build(), String.class);

// Then
assertThat(response).failsWithin(Duration.of(30, ChronoUnit.SECONDS));
assertThat(response).failsWithin(FUTURE_COMPLETION_TIME);
assertThat(connectionFailureCallbackInvoked).extracting(CountDownLatch::getCount).isEqualTo(0L);
}
}
Expand Down

0 comments on commit 99cacc3

Please sign in to comment.