|
| 1 | + |
| 2 | +package io.vrap.rmf.base.client.http; |
| 3 | + |
| 4 | +import static io.vrap.rmf.base.client.utils.ClientUtils.blockingWait; |
| 5 | +import static java.util.Collections.singletonList; |
| 6 | + |
| 7 | +import java.net.URI; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.time.Duration; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.concurrent.CompletableFuture; |
| 12 | +import java.util.concurrent.CompletionException; |
| 13 | +import java.util.concurrent.atomic.AtomicInteger; |
| 14 | + |
| 15 | +import io.vrap.rmf.base.client.*; |
| 16 | +import io.vrap.rmf.base.client.utils.json.JsonException; |
| 17 | + |
| 18 | +import org.assertj.core.api.Assertions; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import dev.failsafe.TimeoutBuilder; |
| 22 | + |
| 23 | +public class RequestPolicyMiddlewareTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testWithStatusCode() { |
| 27 | + PolicyMiddleware middleware = RequestPolicyBuilder.of() |
| 28 | + .withRequestMatching(request -> request.getMethod() == ApiHttpMethod.GET, |
| 29 | + request -> request.withRetry(builder -> builder.maxRetries(1))) |
| 30 | + .build(); |
| 31 | + |
| 32 | + final ApiHttpRequest request = new ApiHttpRequest().withMethod(ApiHttpMethod.GET); |
| 33 | + AtomicInteger count = new AtomicInteger(); |
| 34 | + |
| 35 | + final ApiHttpResponse<byte[]> apiHttpResponse = blockingWait(middleware.invoke(request, request1 -> { |
| 36 | + count.getAndIncrement(); |
| 37 | + return CompletableFuture.completedFuture(new ApiHttpResponse<>(503, null, null)); |
| 38 | + }), Duration.ofSeconds(1)); |
| 39 | + Assertions.assertThat(apiHttpResponse.getStatusCode()).isEqualTo(503); |
| 40 | + Assertions.assertThat(count.get()).isEqualTo(2); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void testWithCoveredException() { |
| 45 | + PolicyMiddleware middleware = RequestPolicyBuilder.of() |
| 46 | + .withRequestMatching(request -> request.getMethod() == ApiHttpMethod.GET, |
| 47 | + request -> request.withRetry(builder -> builder.maxRetries(1))) |
| 48 | + .build(); |
| 49 | + |
| 50 | + final URI uri = URI.create("https://www.commercetools.com/"); |
| 51 | + |
| 52 | + final ApiHttpRequest request = new ApiHttpRequest(ApiHttpMethod.GET, uri, null, null); |
| 53 | + AtomicInteger count = new AtomicInteger(); |
| 54 | + |
| 55 | + Assertions.assertThatExceptionOfType(ApiHttpException.class).isThrownBy(() -> { |
| 56 | + blockingWait(middleware.invoke(request, request1 -> { |
| 57 | + count.getAndIncrement(); |
| 58 | + return CompletableFuture.supplyAsync(() -> { |
| 59 | + throw new CompletionException(new ApiHttpException(503, null, null, null, null, request)); |
| 60 | + }); |
| 61 | + }), Duration.ofSeconds(1)); |
| 62 | + }).matches(e -> e.getStatusCode() == 503); |
| 63 | + Assertions.assertThat(count.get()).isEqualTo(2); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testWithCoveredExceptionResponse() { |
| 68 | + PolicyMiddleware middleware = RequestPolicyBuilder.of() |
| 69 | + .withRequestMatching(request -> request.getMethod() == ApiHttpMethod.GET, |
| 70 | + request -> request.withRetry(builder -> builder.maxRetries(1))) |
| 71 | + .build(); |
| 72 | + |
| 73 | + final URI uri = URI.create("https://www.commercetools.com/"); |
| 74 | + |
| 75 | + final ApiHttpRequest request = new ApiHttpRequest(ApiHttpMethod.GET, uri, null, null); |
| 76 | + AtomicInteger count = new AtomicInteger(); |
| 77 | + final ApiHttpResponse<byte[]> response = new ApiHttpResponse<>(503, new ApiHttpHeaders(), |
| 78 | + "{\"hello\": \"world\"}".getBytes(StandardCharsets.UTF_8), "Oops!"); |
| 79 | + Assertions.assertThatExceptionOfType(ApiHttpException.class).isThrownBy(() -> { |
| 80 | + blockingWait(middleware.invoke(request, request1 -> { |
| 81 | + count.getAndIncrement(); |
| 82 | + return CompletableFuture.supplyAsync(() -> { |
| 83 | + throw new CompletionException(new ApiHttpException(503, null, null, null, response, request)); |
| 84 | + }); |
| 85 | + }), Duration.ofSeconds(1)); |
| 86 | + }).matches(e -> e.getStatusCode() == 503); |
| 87 | + Assertions.assertThat(count.get()).isEqualTo(2); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testUncoveredException() { |
| 92 | + PolicyMiddleware middleware = RequestPolicyBuilder.of() |
| 93 | + .withRequestMatching(request -> request.getMethod() == ApiHttpMethod.GET, |
| 94 | + request -> request.withRetry(builder -> builder.maxRetries(1))) |
| 95 | + .build(); |
| 96 | + |
| 97 | + final ApiHttpRequest request = new ApiHttpRequest(); |
| 98 | + AtomicInteger count = new AtomicInteger(); |
| 99 | + |
| 100 | + Assertions.assertThatExceptionOfType(ApiHttpException.class).isThrownBy(() -> { |
| 101 | + blockingWait(middleware.invoke(request, request1 -> { |
| 102 | + count.getAndIncrement(); |
| 103 | + return CompletableFuture.supplyAsync(() -> { |
| 104 | + throw new CompletionException(new ApiHttpException(504, null, null, null, null, request)); |
| 105 | + }); |
| 106 | + }), Duration.ofSeconds(1)); |
| 107 | + }).matches(e -> e.getStatusCode() == 504); |
| 108 | + Assertions.assertThat(count.get()).isEqualTo(1); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testRetrySuccess() { |
| 113 | + PolicyMiddleware middleware = RequestPolicyBuilder.of() |
| 114 | + .withRequestMatching(request -> request.getMethod() == ApiHttpMethod.GET, |
| 115 | + request -> request.withRetry(builder -> builder.maxRetries(1))) |
| 116 | + .build(); |
| 117 | + |
| 118 | + final ApiHttpRequest request = new ApiHttpRequest(); |
| 119 | + AtomicInteger count = new AtomicInteger(); |
| 120 | + |
| 121 | + final ApiHttpResponse<byte[]> apiHttpResponse = blockingWait(middleware.invoke(request, request1 -> { |
| 122 | + count.getAndIncrement(); |
| 123 | + return CompletableFuture.completedFuture(new ApiHttpResponse<>(200, null, null)); |
| 124 | + }), Duration.ofSeconds(1)); |
| 125 | + Assertions.assertThat(apiHttpResponse.getStatusCode()).isEqualTo(200); |
| 126 | + Assertions.assertThat(count.get()).isEqualTo(1); |
| 127 | + } |
| 128 | + |
| 129 | + public void retryConfigurationStatusCodes() { |
| 130 | + final ApiHttpClient build = ClientBuilder.of() |
| 131 | + // ... |
| 132 | + .withRequestPolicies(policyBuilder -> policyBuilder |
| 133 | + .withAllOtherRequests(request -> request.withRetry(retry -> retry.maxRetries(3) |
| 134 | + .statusCodes(Arrays.asList(HttpStatusCode.SERVICE_UNAVAILABLE_503, |
| 135 | + HttpStatusCode.INTERNAL_SERVER_ERROR_500))))) |
| 136 | + .build(); |
| 137 | + } |
| 138 | + |
| 139 | + public void retryConfigurationExceptions() { |
| 140 | + final ApiHttpClient build = ClientBuilder.of() |
| 141 | + // ... |
| 142 | + .withRequestPolicies(policyBuilder -> policyBuilder.withAllOtherRequests(request -> request |
| 143 | + .withRetry(retry -> retry.maxRetries(3).failures(singletonList(JsonException.class))))) |
| 144 | + .build(); |
| 145 | + } |
| 146 | + |
| 147 | + public void queueConfiguration() { |
| 148 | + final ApiHttpClient build = ClientBuilder.of() |
| 149 | + // ... |
| 150 | + .withRequestPolicies(policyBuilder -> policyBuilder |
| 151 | + .withAllOtherRequests(request -> request.withBulkhead(64, Duration.ofSeconds(10)))) |
| 152 | + .build(); |
| 153 | + } |
| 154 | + |
| 155 | + public void timeoutConfiguration() { |
| 156 | + final ApiHttpClient build = ClientBuilder.of() |
| 157 | + // ... |
| 158 | + .withRequestPolicies(policyBuilder -> policyBuilder.withAllOtherRequests( |
| 159 | + request -> request.withTimeout(Duration.ofSeconds(10), TimeoutBuilder::withInterrupt))) |
| 160 | + .build(); |
| 161 | + } |
| 162 | +} |
0 commit comments