|
| 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.stream.Stream; |
| 30 | +import org.junit.jupiter.api.AfterAll; |
| 31 | +import org.junit.jupiter.api.BeforeAll; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.params.ParameterizedTest; |
| 34 | +import org.junit.jupiter.params.provider.MethodSource; |
| 35 | +import software.amazon.awssdk.core.Response; |
| 36 | +import software.amazon.awssdk.core.SdkRequest; |
| 37 | +import software.amazon.awssdk.core.SdkResponse; |
| 38 | +import software.amazon.awssdk.core.client.config.SdkClientConfiguration; |
| 39 | +import software.amazon.awssdk.core.client.config.SdkClientOption; |
| 40 | +import software.amazon.awssdk.core.exception.SdkException; |
| 41 | +import software.amazon.awssdk.core.http.ExecutionContext; |
| 42 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 43 | +import software.amazon.awssdk.core.internal.http.HttpClientDependencies; |
| 44 | +import software.amazon.awssdk.core.internal.http.RequestExecutionContext; |
| 45 | +import software.amazon.awssdk.core.internal.http.pipeline.RequestPipeline; |
| 46 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 47 | +import software.amazon.awssdk.http.SdkHttpFullResponse; |
| 48 | +import software.amazon.awssdk.http.SdkHttpMethod; |
| 49 | +import software.amazon.awssdk.metrics.NoOpMetricCollector; |
| 50 | +import software.amazon.awssdk.retries.api.AcquireInitialTokenResponse; |
| 51 | +import software.amazon.awssdk.retries.api.RetryStrategy; |
| 52 | +import software.amazon.awssdk.retries.api.RetryToken; |
| 53 | +import software.amazon.awssdk.retries.api.TokenAcquisitionFailedException; |
| 54 | + |
| 55 | +public class AsyncRetryableStageTest { |
| 56 | + private RetryStrategy mockRetryStrategy; |
| 57 | + private AcquireInitialTokenResponse mockAcquireInitialTokenResponse; |
| 58 | + private RetryToken mockRetryToken; |
| 59 | + |
| 60 | + private RequestPipeline<SdkHttpFullRequest, CompletableFuture<Response<SdkResponse>>> mockDelegatePipeline; |
| 61 | + |
| 62 | + private static ScheduledExecutorService executorService; |
| 63 | + |
| 64 | + @BeforeAll |
| 65 | + static void setup() { |
| 66 | + executorService = Executors.newScheduledThreadPool(1); |
| 67 | + } |
| 68 | + |
| 69 | + @AfterAll |
| 70 | + static void teardown() { |
| 71 | + executorService.shutdownNow(); |
| 72 | + } |
| 73 | + |
| 74 | + @BeforeEach |
| 75 | + void methodSetup() { |
| 76 | + mockRetryStrategy = mock(RetryStrategy.class); |
| 77 | + mockAcquireInitialTokenResponse = mock(AcquireInitialTokenResponse.class); |
| 78 | + mockRetryToken = mock(RetryToken.class); |
| 79 | + |
| 80 | + when(mockAcquireInitialTokenResponse.token()).thenReturn(mockRetryToken); |
| 81 | + when(mockAcquireInitialTokenResponse.delay()).thenReturn(Duration.ZERO); |
| 82 | + |
| 83 | + when(mockRetryStrategy.acquireInitialToken(any())).thenReturn(mockAcquireInitialTokenResponse); |
| 84 | + |
| 85 | + mockDelegatePipeline = mock(RequestPipeline.class); |
| 86 | + } |
| 87 | + |
| 88 | + @ParameterizedTest(name = "Retry disallowed, delays {0}") |
| 89 | + @MethodSource("acquireFailureTestCases") |
| 90 | + void execute_acquireFailure_backoffBehavesCorrectly(Duration acquireFailureBackoff) throws Exception { |
| 91 | + SdkClientConfiguration clientConfig = SdkClientConfiguration.builder() |
| 92 | + .option(SdkClientOption.RETRY_STRATEGY, mockRetryStrategy) |
| 93 | + .option(SdkClientOption.SCHEDULED_EXECUTOR_SERVICE, |
| 94 | + executorService) |
| 95 | + .build(); |
| 96 | + |
| 97 | + HttpClientDependencies deps = HttpClientDependencies.builder() |
| 98 | + .clientConfiguration(clientConfig) |
| 99 | + .build(); |
| 100 | + |
| 101 | + AsyncRetryableStage<SdkResponse> retryableStage = new AsyncRetryableStage<>(null, deps, mockDelegatePipeline); |
| 102 | + |
| 103 | + SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder() |
| 104 | + .method(SdkHttpMethod.GET) |
| 105 | + .uri(URI.create("https://my-service.amazonaws.com")) |
| 106 | + .build(); |
| 107 | + |
| 108 | + ExecutionAttributes execAttrs = ExecutionAttributes.builder() |
| 109 | + .build(); |
| 110 | + |
| 111 | + ExecutionContext execCtx = ExecutionContext.builder() |
| 112 | + .metricCollector(NoOpMetricCollector.create()) |
| 113 | + .executionAttributes(execAttrs) |
| 114 | + .build(); |
| 115 | + |
| 116 | + RequestExecutionContext ctx = RequestExecutionContext.builder() |
| 117 | + .originalRequest(mock(SdkRequest.class)) |
| 118 | + .executionContext(execCtx) |
| 119 | + .build(); |
| 120 | + |
| 121 | + SdkHttpFullResponse.Builder httpResponse = SdkHttpFullResponse.builder() |
| 122 | + .statusCode(502); |
| 123 | + |
| 124 | + Response<SdkResponse> response = Response.<SdkResponse>builder() |
| 125 | + .httpResponse(httpResponse.build()) |
| 126 | + .isSuccess(false) |
| 127 | + .exception(SdkException.builder().build()) |
| 128 | + .build(); |
| 129 | + |
| 130 | + when(mockDelegatePipeline.execute(any(), any())).thenReturn(CompletableFuture.completedFuture(response)); |
| 131 | + |
| 132 | + when(mockRetryStrategy.refreshRetryToken(any())).thenThrow( |
| 133 | + new TokenAcquisitionFailedException("Acquire failed", mockRetryToken, null, acquireFailureBackoff) |
| 134 | + ); |
| 135 | + |
| 136 | + long start = System.nanoTime(); |
| 137 | + CompletableFuture<Response<SdkResponse>> execute = retryableStage.execute(httpRequest, ctx); |
| 138 | + // exception thrown doesn't matter, just results in exception because we mock just enough... |
| 139 | + assertThatThrownBy(execute::join); |
| 140 | + long end = System.nanoTime(); |
| 141 | + |
| 142 | + Duration lowerBound = acquireFailureBackoff != null ? acquireFailureBackoff : Duration.ZERO; |
| 143 | + assertThat(Duration.ofNanos(end - start)).isBetween(lowerBound, lowerBound.plusMillis(250)); |
| 144 | + } |
| 145 | + |
| 146 | + private static Stream<Duration> acquireFailureTestCases() { |
| 147 | + return Stream.of( |
| 148 | + Duration.ZERO, |
| 149 | + null, |
| 150 | + Duration.ofMillis(100) |
| 151 | + ); |
| 152 | + } |
| 153 | +} |
0 commit comments