diff options
Diffstat (limited to 'vespajlib/src/test/java')
4 files changed, 366 insertions, 0 deletions
diff --git a/vespajlib/src/test/java/ai/vespa/util/http/hc4/VespaHttpClientBuilderTest.java b/vespajlib/src/test/java/ai/vespa/util/http/hc4/VespaHttpClientBuilderTest.java new file mode 100644 index 00000000000..58aa70b69b1 --- /dev/null +++ b/vespajlib/src/test/java/ai/vespa/util/http/hc4/VespaHttpClientBuilderTest.java @@ -0,0 +1,42 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.util.http.hc4; + +import org.apache.http.HttpException; +import org.apache.http.HttpHost; +import org.apache.http.HttpRequest; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.conn.routing.HttpRoute; +import org.apache.http.conn.routing.HttpRoutePlanner; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + +/** + * @author bjorncs + */ +public class VespaHttpClientBuilderTest { + + @Test + public void route_planner_modifies_scheme_of_requests() throws HttpException { + verifyProcessedUriMatchesExpectedOutput("http://dummyhostname:8080", "https://dummyhostname:8080"); + } + + @Test + public void route_planer_handles_implicit_http_port() throws HttpException { + verifyProcessedUriMatchesExpectedOutput("http://dummyhostname", "https://dummyhostname:80"); + } + + @Test + public void route_planer_handles_https_port() throws HttpException { + verifyProcessedUriMatchesExpectedOutput("http://dummyhostname:443", "https://dummyhostname:443"); + } + + private static void verifyProcessedUriMatchesExpectedOutput(String inputHostString, String expectedHostString) throws HttpException { + HttpRoutePlanner routePlanner = new VespaHttpClientBuilder.HttpToHttpsRoutePlanner(); + HttpRoute newRoute = routePlanner.determineRoute(HttpHost.create(inputHostString), mock(HttpRequest.class), new HttpClientContext()); + HttpHost target = newRoute.getTargetHost(); + assertEquals(expectedHostString, target.toURI()); + } + +}
\ No newline at end of file diff --git a/vespajlib/src/test/java/ai/vespa/util/http/hc4/retry/DelayedConnectionLevelRetryHandlerTest.java b/vespajlib/src/test/java/ai/vespa/util/http/hc4/retry/DelayedConnectionLevelRetryHandlerTest.java new file mode 100644 index 00000000000..7330a91d75c --- /dev/null +++ b/vespajlib/src/test/java/ai/vespa/util/http/hc4/retry/DelayedConnectionLevelRetryHandlerTest.java @@ -0,0 +1,134 @@ +// Copyright 2020 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.util.http.hc4.retry; + +import org.apache.http.client.protocol.HttpClientContext; +import org.junit.Test; + +import javax.net.ssl.SSLException; +import java.io.IOException; +import java.net.ConnectException; +import java.time.Duration; +import java.util.Arrays; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * @author bjorncs + */ +public class DelayedConnectionLevelRetryHandlerTest { + + @SuppressWarnings("unchecked") + @Test + public void retry_consumers_are_invoked() { + RetryConsumer<IOException> retryConsumer = (RetryConsumer<IOException>) mock(RetryConsumer.class); + RetryFailedConsumer<IOException> retryFailedConsumer = (RetryFailedConsumer<IOException>) mock(RetryFailedConsumer.class); + + Duration delay = Duration.ofSeconds(10); + int maxRetries = 5; + + DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder + .withFixedDelay(delay, maxRetries) + .withSleeper(mock(Sleeper.class)) + .onRetry(retryConsumer) + .onRetryFailed(retryFailedConsumer) + .build(); + + IOException exception = new IOException(); + HttpClientContext ctx = new HttpClientContext(); + int lastExecutionCount = maxRetries + 1; + for (int i = 1; i <= lastExecutionCount; i++) { + handler.retryRequest(exception, i, ctx); + } + + verify(retryFailedConsumer).onRetryFailed(exception, lastExecutionCount, ctx); + for (int i = 1; i < lastExecutionCount; i++) { + verify(retryConsumer).onRetry(exception, delay, i, ctx); + } + } + + @Test + public void retry_with_fixed_delay_sleeps_for_expected_duration() { + Sleeper sleeper = mock(Sleeper.class); + + Duration delay = Duration.ofSeconds(2); + int maxRetries = 2; + + DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder + .withFixedDelay(delay, maxRetries) + .withSleeper(sleeper) + .build(); + + IOException exception = new IOException(); + HttpClientContext ctx = new HttpClientContext(); + int lastExecutionCount = maxRetries + 1; + for (int i = 1; i <= lastExecutionCount; i++) { + handler.retryRequest(exception, i, ctx); + } + + verify(sleeper, times(2)).sleep(delay); + } + + @Test + public void retry_with_fixed_backoff_sleeps_for_expected_durations() { + Sleeper sleeper = mock(Sleeper.class); + + Duration startDelay = Duration.ofMillis(500); + Duration maxDelay = Duration.ofSeconds(5); + int maxRetries = 10; + + DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder + .withExponentialBackoff(startDelay, maxDelay, maxRetries) + .withSleeper(sleeper) + .build(); + + IOException exception = new IOException(); + HttpClientContext ctx = new HttpClientContext(); + int lastExecutionCount = maxRetries + 1; + for (int i = 1; i <= lastExecutionCount; i++) { + handler.retryRequest(exception, i, ctx); + } + + verify(sleeper).sleep(startDelay); + verify(sleeper).sleep(Duration.ofSeconds(1)); + verify(sleeper).sleep(Duration.ofSeconds(2)); + verify(sleeper).sleep(Duration.ofSeconds(4)); + verify(sleeper, times(6)).sleep(Duration.ofSeconds(5)); + } + + @Test + public void retries_for_listed_exceptions_until_max_retries_exceeded() { + int maxRetries = 2; + + DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder + .withFixedDelay(Duration.ofSeconds(2), maxRetries) + .retryForExceptions(Arrays.asList(SSLException.class, ConnectException.class)) + .withSleeper(mock(Sleeper.class)) + .build(); + + SSLException sslException = new SSLException("ssl error"); + HttpClientContext ctx = new HttpClientContext(); + int lastExecutionCount = maxRetries + 1; + for (int i = 1; i < lastExecutionCount; i++) { + assertTrue(handler.retryRequest(sslException, i, ctx)); + } + assertFalse(handler.retryRequest(sslException, lastExecutionCount, ctx)); + } + + @Test + public void does_not_retry_for_non_listed_exception() { + DelayedConnectionLevelRetryHandler handler = DelayedConnectionLevelRetryHandler.Builder + .withFixedDelay(Duration.ofSeconds(2), 2) + .retryForExceptions(Arrays.asList(SSLException.class, ConnectException.class)) + .withSleeper(mock(Sleeper.class)) + .build(); + + IOException ioException = new IOException(); + HttpClientContext ctx = new HttpClientContext(); + assertFalse(handler.retryRequest(ioException, 1, ctx)); + } + +}
\ No newline at end of file diff --git a/vespajlib/src/test/java/ai/vespa/util/http/hc4/retry/DelayedResponseLevelRetryHandlerTest.java b/vespajlib/src/test/java/ai/vespa/util/http/hc4/retry/DelayedResponseLevelRetryHandlerTest.java new file mode 100644 index 00000000000..514eae56fe8 --- /dev/null +++ b/vespajlib/src/test/java/ai/vespa/util/http/hc4/retry/DelayedResponseLevelRetryHandlerTest.java @@ -0,0 +1,131 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.util.http.hc4.retry; + +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.HttpVersion; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.message.BasicStatusLine; +import org.junit.Test; + +import java.time.Duration; +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +/** + * @author bjorncs + */ +public class DelayedResponseLevelRetryHandlerTest { + + @Test + @SuppressWarnings("unchecked") + public void retry_consumers_are_invoked() { + RetryConsumer<HttpResponse> retryConsumer = mock(RetryConsumer.class); + RetryFailedConsumer<HttpResponse> retryFailedConsumer = mock(RetryFailedConsumer.class); + + Duration delay = Duration.ofSeconds(10); + int maxRetries = 5; + + DelayedResponseLevelRetryHandler handler = DelayedResponseLevelRetryHandler.Builder + .withFixedDelay(delay, maxRetries) + .onRetry(retryConsumer) + .onRetryFailed(retryFailedConsumer) + .build(); + + HttpResponse response = createResponse(HttpStatus.SC_SERVICE_UNAVAILABLE); + HttpClientContext ctx = new HttpClientContext(); + int lastExecutionCount = maxRetries + 1; + for (int i = 1; i <= lastExecutionCount; i++) { + handler.retryRequest(response, i, ctx); + } + + verify(retryFailedConsumer).onRetryFailed(response, lastExecutionCount, ctx); + for (int i = 1; i < lastExecutionCount; i++) { + verify(retryConsumer).onRetry(response, delay, i, ctx); + } + } + + @Test + public void retry_with_fixed_delay_sleeps_for_expected_duration() { + Duration delay = Duration.ofSeconds(2); + int maxRetries = 2; + + DelayedResponseLevelRetryHandler handler = DelayedResponseLevelRetryHandler.Builder + .withFixedDelay(delay, maxRetries) + .build(); + + HttpResponse response = createResponse(HttpStatus.SC_SERVICE_UNAVAILABLE); + HttpClientContext ctx = new HttpClientContext(); + int lastExecutionCount = maxRetries + 1; + for (int i = 1; i <= lastExecutionCount; i++) { + handler.retryRequest(response, i, ctx); + assertEquals(delay.toMillis(), handler.getRetryInterval()); + } + } + + @Test + public void retry_with_fixed_backoff_sleeps_for_expected_durations() { + Duration startDelay = Duration.ofMillis(500); + Duration maxDelay = Duration.ofSeconds(5); + int maxRetries = 10; + + DelayedResponseLevelRetryHandler handler = DelayedResponseLevelRetryHandler.Builder + .withExponentialBackoff(startDelay, maxDelay, maxRetries) + .build(); + + HttpResponse response = createResponse(HttpStatus.SC_SERVICE_UNAVAILABLE); + HttpClientContext ctx = new HttpClientContext(); + int lastExecutionCount = maxRetries + 1; + List<Duration> expectedIntervals = + Arrays.asList( + startDelay, Duration.ofSeconds(1), Duration.ofSeconds(2), Duration.ofSeconds(4), + Duration.ofSeconds(5), Duration.ofSeconds(5), Duration.ofSeconds(5), Duration.ofSeconds(5), + Duration.ofSeconds(5), Duration.ofSeconds(5), Duration.ofSeconds(5)); + for (int i = 1; i <= lastExecutionCount; i++) { + handler.retryRequest(response, i, ctx); + assertEquals(expectedIntervals.get(i-1).toMillis(), handler.getRetryInterval()); + } + } + + @Test + public void retries_for_listed_exceptions_until_max_retries_exceeded() { + int maxRetries = 2; + + DelayedResponseLevelRetryHandler handler = DelayedResponseLevelRetryHandler.Builder + .withFixedDelay(Duration.ofSeconds(2), maxRetries) + .retryForStatusCodes(Arrays.asList(HttpStatus.SC_SERVICE_UNAVAILABLE, HttpStatus.SC_BAD_GATEWAY)) + .build(); + + HttpResponse response = createResponse(HttpStatus.SC_SERVICE_UNAVAILABLE); + HttpClientContext ctx = new HttpClientContext(); + int lastExecutionCount = maxRetries + 1; + for (int i = 1; i < lastExecutionCount; i++) { + assertTrue(handler.retryRequest(response, i, ctx)); + } + assertFalse(handler.retryRequest(response, lastExecutionCount, ctx)); + } + + @Test + public void does_not_retry_for_non_listed_exception() { + DelayedResponseLevelRetryHandler handler = DelayedResponseLevelRetryHandler.Builder + .withFixedDelay(Duration.ofSeconds(2), 2) + .retryForStatusCodes(Arrays.asList(HttpStatus.SC_SERVICE_UNAVAILABLE, HttpStatus.SC_BAD_GATEWAY)) + .build(); + + HttpResponse response = createResponse(HttpStatus.SC_OK); + HttpClientContext ctx = new HttpClientContext(); + assertFalse(handler.retryRequest(response, 1, ctx)); + } + + private static HttpResponse createResponse(int statusCode) { + return new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, "reason phrase")); + } + +} diff --git a/vespajlib/src/test/java/ai/vespa/util/http/hc5/HttpToHttpsRoutePlannerTest.java b/vespajlib/src/test/java/ai/vespa/util/http/hc5/HttpToHttpsRoutePlannerTest.java new file mode 100644 index 00000000000..58dc25fdf1a --- /dev/null +++ b/vespajlib/src/test/java/ai/vespa/util/http/hc5/HttpToHttpsRoutePlannerTest.java @@ -0,0 +1,59 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package ai.vespa.util.http.hc5; + +import org.apache.hc.client5.http.HttpRoute; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.protocol.HttpClientContext; +import org.apache.hc.core5.http.HttpException; +import org.apache.hc.core5.http.HttpHost; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author jonmv + */ +public class HttpToHttpsRoutePlannerTest { + + final HttpToHttpsRoutePlanner planner = new HttpToHttpsRoutePlanner(); + + @Test + public void verifySchemeMustBeHttp() throws HttpException { + try { + planner.determineRoute(new HttpHost("https", "host", 1), new HttpClientContext()); + } + catch (IllegalArgumentException e) { + assertEquals("Scheme must be 'http' when using HttpToHttpsRoutePlanner", e.getMessage()); + } + } + + @Test + public void verifyPortMustBeSet() throws HttpException { + try { + planner.determineRoute(new HttpHost("http", "host", -1), new HttpClientContext()); + } + catch (IllegalArgumentException e) { + assertEquals("Port must be set when using HttpToHttpsRoutePlanner", e.getMessage()); + } + } + + + @Test + public void verifyProxyIsDisallowed() throws HttpException { + HttpClientContext context = new HttpClientContext(); + context.setRequestConfig(RequestConfig.custom().setProxy(new HttpHost("proxy")).build()); + try { + planner.determineRoute(new HttpHost("http", "host", 1), context); + } + catch (IllegalArgumentException e) { + assertEquals("Proxies are not supported with HttpToHttpsRoutePlanner", e.getMessage()); + } + } + + @Test + public void verifySchemeIsRewritten() throws HttpException { + assertEquals(new HttpRoute(new HttpHost("https", "host", 1)), + planner.determineRoute(new HttpHost("http", "host", 1), new HttpClientContext())); + } + +} |