|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.core.internal.http.pipeline.stages; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static org.mockito.ArgumentMatchers.any; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | + |
| 24 | +import java.net.URI; |
| 25 | +import java.time.Duration; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.Executors; |
| 28 | +import java.util.concurrent.ScheduledExecutorService; |
| 29 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 30 | +import java.util.stream.Stream; |
| 31 | +import org.junit.jupiter.api.AfterAll; |
| 32 | +import org.junit.jupiter.api.BeforeAll; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.params.ParameterizedTest; |
| 35 | +import org.junit.jupiter.params.provider.MethodSource; |
| 36 | +import software.amazon.awssdk.core.Response; |
| 37 | +import software.amazon.awssdk.core.SdkRequest; |
| 38 | +import software.amazon.awssdk.core.SdkResponse; |
| 39 | +import software.amazon.awssdk.core.client.config.SdkClientConfiguration; |
| 40 | +import software.amazon.awssdk.core.client.config.SdkClientOption; |
| 41 | +import software.amazon.awssdk.core.exception.SdkException; |
| 42 | +import software.amazon.awssdk.core.http.ExecutionContext; |
| 43 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 44 | +import software.amazon.awssdk.core.internal.http.HttpClientDependencies; |
| 45 | +import software.amazon.awssdk.core.internal.http.RequestExecutionContext; |
| 46 | +import software.amazon.awssdk.core.internal.http.TransformingAsyncResponseHandler; |
| 47 | +import software.amazon.awssdk.core.internal.http.pipeline.RequestPipeline; |
| 48 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 49 | +import software.amazon.awssdk.http.SdkHttpFullResponse; |
| 50 | +import software.amazon.awssdk.http.SdkHttpMethod; |
| 51 | +import software.amazon.awssdk.metrics.NoOpMetricCollector; |
| 52 | +import software.amazon.awssdk.retries.api.AcquireInitialTokenResponse; |
| 53 | +import software.amazon.awssdk.retries.api.RefreshRetryTokenResponse; |
| 54 | +import software.amazon.awssdk.retries.api.RetryStrategy; |
| 55 | +import software.amazon.awssdk.retries.api.RetryToken; |
| 56 | +import software.amazon.awssdk.retries.api.TokenAcquisitionFailedException; |
| 57 | + |
| 58 | +public class AsyncRetryableStageTest { |
| 59 | + private RetryStrategy mockRetryStrategy; |
| 60 | + private AcquireInitialTokenResponse mockAcquireInitialTokenResponse; |
| 61 | + private RetryToken mockRetryToken; |
| 62 | + |
| 63 | + private RequestPipeline<SdkHttpFullRequest, CompletableFuture<Response<SdkResponse>>> mockDelegatePipeline; |
| 64 | + |
| 65 | + private static ScheduledExecutorService executorService; |
| 66 | + |
| 67 | + @BeforeAll |
| 68 | + static void setup() { |
| 69 | + executorService = Executors.newScheduledThreadPool(1); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterAll |
| 73 | + static void teardown() { |
| 74 | + executorService.shutdownNow(); |
| 75 | + } |
| 76 | + |
| 77 | + @BeforeEach |
| 78 | + void methodSetup() { |
| 79 | + mockRetryStrategy = mock(RetryStrategy.class); |
| 80 | + mockAcquireInitialTokenResponse = mock(AcquireInitialTokenResponse.class); |
| 81 | + mockRetryToken = mock(RetryToken.class); |
| 82 | + |
| 83 | + when(mockAcquireInitialTokenResponse.token()).thenReturn(mockRetryToken); |
| 84 | + when(mockAcquireInitialTokenResponse.delay()).thenReturn(Duration.ZERO); |
| 85 | + |
| 86 | + when(mockRetryStrategy.acquireInitialToken(any())).thenReturn(mockAcquireInitialTokenResponse); |
| 87 | + |
| 88 | + mockDelegatePipeline = mock(RequestPipeline.class); |
| 89 | + } |
| 90 | + |
| 91 | + @ParameterizedTest |
| 92 | + @MethodSource("acquireDelayTestCases") |
| 93 | + void execute_acquireDelay_behavesCorrectly(AcquireDelayTestCase testCase) throws Exception { |
| 94 | + SdkClientConfiguration clientConfig = SdkClientConfiguration.builder() |
| 95 | + .option(SdkClientOption.RETRY_STRATEGY, mockRetryStrategy) |
| 96 | + .option(SdkClientOption.SCHEDULED_EXECUTOR_SERVICE, |
| 97 | + executorService) |
| 98 | + .build(); |
| 99 | + |
| 100 | + HttpClientDependencies deps = HttpClientDependencies.builder() |
| 101 | + .clientConfiguration(clientConfig) |
| 102 | + .build(); |
| 103 | + |
| 104 | + AsyncRetryableStage<SdkResponse> retryableStage = new AsyncRetryableStage<>(mock(TransformingAsyncResponseHandler.class), |
| 105 | + deps, mockDelegatePipeline); |
| 106 | + |
| 107 | + SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder() |
| 108 | + .method(SdkHttpMethod.GET) |
| 109 | + .uri(URI.create("https://my-service.amazonaws.com")) |
| 110 | + .build(); |
| 111 | + |
| 112 | + ExecutionAttributes execAttrs = ExecutionAttributes.builder() |
| 113 | + .build(); |
| 114 | + |
| 115 | + ExecutionContext execCtx = ExecutionContext.builder() |
| 116 | + .metricCollector(NoOpMetricCollector.create()) |
| 117 | + .executionAttributes(execAttrs) |
| 118 | + .build(); |
| 119 | + |
| 120 | + RequestExecutionContext ctx = RequestExecutionContext.builder() |
| 121 | + .originalRequest(mock(SdkRequest.class)) |
| 122 | + .executionContext(execCtx) |
| 123 | + .build(); |
| 124 | + |
| 125 | + SdkHttpFullResponse.Builder httpResponse = SdkHttpFullResponse.builder() |
| 126 | + .statusCode(502); |
| 127 | + |
| 128 | + Response<SdkResponse> response = Response.<SdkResponse>builder() |
| 129 | + .httpResponse(httpResponse.build()) |
| 130 | + .isSuccess(false) |
| 131 | + .exception(SdkException.builder().build()) |
| 132 | + .build(); |
| 133 | + |
| 134 | + when(mockDelegatePipeline.execute(any(), any())).thenReturn(CompletableFuture.completedFuture(response)); |
| 135 | + |
| 136 | + if (testCase.failure) { |
| 137 | + when(mockRetryStrategy.refreshRetryToken(any())).thenThrow( |
| 138 | + new TokenAcquisitionFailedException("Acquire failed", mockRetryToken, null, testCase.failureDelay) |
| 139 | + ); |
| 140 | + } else { |
| 141 | + // only retry once, otherwise we'll get into an infinite loop |
| 142 | + AtomicBoolean first = new AtomicBoolean(); |
| 143 | + when(mockRetryStrategy.refreshRetryToken(any())).thenAnswer(i -> { |
| 144 | + if (first.compareAndSet(false, true)) { |
| 145 | + return RefreshRetryTokenResponse.create(mockRetryToken, testCase.successDelay); |
| 146 | + } |
| 147 | + throw new TokenAcquisitionFailedException("Acquire failed", mockRetryToken, null, Duration.ZERO); |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + long start = System.nanoTime(); |
| 152 | + CompletableFuture<Response<SdkResponse>> execute = retryableStage.execute(httpRequest, ctx); |
| 153 | + // exception thrown doesn't matter, just results in exception because we mock just enough... |
| 154 | + assertThatThrownBy(execute::join); |
| 155 | + long end = System.nanoTime(); |
| 156 | + |
| 157 | + Duration lowerBound = testCase.expectedDelay(); |
| 158 | + assertThat(Duration.ofNanos(end - start)).isBetween(lowerBound, lowerBound.plusMillis(250)); |
| 159 | + } |
| 160 | + |
| 161 | + private static Stream<AcquireDelayTestCase> acquireDelayTestCases() { |
| 162 | + return Stream.of( |
| 163 | + new AcquireDelayTestCase(true, Duration.ofDays(1), Duration.ZERO), |
| 164 | + new AcquireDelayTestCase(true, Duration.ofDays(1), Duration.ofMillis(100)), |
| 165 | + |
| 166 | + |
| 167 | + new AcquireDelayTestCase(false, Duration.ZERO, Duration.ofDays(1)), |
| 168 | + new AcquireDelayTestCase(false, Duration.ofMillis(100), Duration.ofDays(1)) |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + private static class AcquireDelayTestCase { |
| 173 | + private boolean failure; |
| 174 | + private Duration successDelay; |
| 175 | + private Duration failureDelay; |
| 176 | + |
| 177 | + public AcquireDelayTestCase(boolean failure, Duration successDelay, Duration failureDelay) { |
| 178 | + this.failure = failure; |
| 179 | + this.successDelay = successDelay; |
| 180 | + this.failureDelay = failureDelay; |
| 181 | + } |
| 182 | + |
| 183 | + public Duration expectedDelay() { |
| 184 | + if (failure) { |
| 185 | + return failureDelay; |
| 186 | + } |
| 187 | + return successDelay; |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public String toString() { |
| 192 | + return (failure ? "Failure" : "Success") + " with delay " + expectedDelay(); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments