|
| 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.client; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.ArgumentMatchers.eq; |
| 21 | +import static org.mockito.Mockito.doThrow; |
| 22 | +import static org.mockito.Mockito.when; |
| 23 | +import static software.amazon.awssdk.core.internal.util.AsyncResponseHandlerTestUtils.noOpResponseHandler; |
| 24 | +import static utils.HttpTestUtils.testAsyncClientBuilder; |
| 25 | + |
| 26 | +import java.time.Duration; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.concurrent.CompletableFuture; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.mockito.Mock; |
| 33 | +import org.mockito.junit.MockitoJUnitRunner; |
| 34 | +import org.mockito.stubbing.Answer; |
| 35 | +import software.amazon.awssdk.core.Response; |
| 36 | +import software.amazon.awssdk.core.SdkResponse; |
| 37 | +import software.amazon.awssdk.core.async.EmptyPublisher; |
| 38 | +import software.amazon.awssdk.core.http.ExecutionContext; |
| 39 | +import software.amazon.awssdk.core.http.NoopTestRequest; |
| 40 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 41 | +import software.amazon.awssdk.core.interceptor.ExecutionInterceptorChain; |
| 42 | +import software.amazon.awssdk.core.interceptor.InterceptorContext; |
| 43 | +import software.amazon.awssdk.core.internal.http.AmazonAsyncHttpClient; |
| 44 | +import software.amazon.awssdk.core.metrics.CoreMetric; |
| 45 | +import software.amazon.awssdk.core.protocol.VoidSdkResponse; |
| 46 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 47 | +import software.amazon.awssdk.http.SdkHttpFullResponse; |
| 48 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 49 | +import software.amazon.awssdk.http.async.AsyncExecuteRequest; |
| 50 | +import software.amazon.awssdk.http.async.SdkAsyncHttpClient; |
| 51 | +import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler; |
| 52 | +import software.amazon.awssdk.metrics.MetricCollector; |
| 53 | +import software.amazon.awssdk.retries.DefaultRetryStrategy; |
| 54 | +import utils.ValidSdkObjects; |
| 55 | + |
| 56 | +/** |
| 57 | + * Tests to verify that exceptions thrown by the MetricCollector are reported through the returned future. |
| 58 | + * {@link java.util.concurrent.CompletableFuture}. |
| 59 | + * |
| 60 | + * @see AsyncClientHandlerExceptionTest |
| 61 | + */ |
| 62 | +@RunWith(MockitoJUnitRunner.class) |
| 63 | +public class AsyncClientMetricCollectorExceptionTest { |
| 64 | + |
| 65 | + public static final String MESSAGE = "test exception"; |
| 66 | + |
| 67 | + @Mock |
| 68 | + private MetricCollector metricCollector; |
| 69 | + |
| 70 | + @Mock |
| 71 | + private SdkAsyncHttpClient asyncHttpClient; |
| 72 | + |
| 73 | + @Test |
| 74 | + public void exceptionInReportMetricReportedInFuture() { |
| 75 | + when(metricCollector.createChild(any())).thenReturn(metricCollector); |
| 76 | + Exception exception = new RuntimeException(MESSAGE); |
| 77 | + doThrow(exception).when(metricCollector).reportMetric(eq(CoreMetric.API_CALL_DURATION), any(Duration.class)); |
| 78 | + |
| 79 | + CompletableFuture<SdkResponse> responseFuture = makeRequest(); |
| 80 | + |
| 81 | + assertThatThrownBy(() -> responseFuture.get(1, TimeUnit.SECONDS)).hasRootCause(exception); |
| 82 | + } |
| 83 | + |
| 84 | + private CompletableFuture<SdkResponse> makeRequest() { |
| 85 | + when(asyncHttpClient.execute(any(AsyncExecuteRequest.class))).thenAnswer((Answer<CompletableFuture<Void>>) invocationOnMock -> { |
| 86 | + SdkAsyncHttpResponseHandler handler = invocationOnMock.getArgument(0, AsyncExecuteRequest.class).responseHandler(); |
| 87 | + handler.onHeaders(SdkHttpFullResponse.builder() |
| 88 | + .statusCode(200) |
| 89 | + .build()); |
| 90 | + handler.onStream(new EmptyPublisher<>()); |
| 91 | + return CompletableFuture.completedFuture(null); |
| 92 | + }); |
| 93 | + |
| 94 | + AmazonAsyncHttpClient asyncClient = testAsyncClientBuilder() |
| 95 | + .retryStrategy(DefaultRetryStrategy.doNotRetry()) |
| 96 | + .asyncHttpClient(asyncHttpClient) |
| 97 | + .build(); |
| 98 | + |
| 99 | + SdkHttpFullRequest httpFullRequest = ValidSdkObjects.sdkHttpFullRequest().build(); |
| 100 | + NoopTestRequest sdkRequest = NoopTestRequest.builder().build(); |
| 101 | + InterceptorContext interceptorContext = InterceptorContext |
| 102 | + .builder() |
| 103 | + .request(sdkRequest) |
| 104 | + .httpRequest(httpFullRequest) |
| 105 | + .build(); |
| 106 | + |
| 107 | + Response<SdkResponse> response = |
| 108 | + Response.<SdkResponse>builder() |
| 109 | + .isSuccess(true) |
| 110 | + .response(VoidSdkResponse.builder().build()) |
| 111 | + .httpResponse(SdkHttpResponse.builder().statusCode(200).build()) |
| 112 | + .build(); |
| 113 | + |
| 114 | + return asyncClient |
| 115 | + .requestExecutionBuilder() |
| 116 | + .originalRequest(sdkRequest) |
| 117 | + .request(httpFullRequest) |
| 118 | + .executionContext( |
| 119 | + ExecutionContext |
| 120 | + .builder() |
| 121 | + .executionAttributes(new ExecutionAttributes()) |
| 122 | + .interceptorContext(interceptorContext) |
| 123 | + .metricCollector(metricCollector) |
| 124 | + .interceptorChain(new ExecutionInterceptorChain(Collections.emptyList())) |
| 125 | + .build() |
| 126 | + ) |
| 127 | + .execute(noOpResponseHandler(response)); |
| 128 | + } |
| 129 | +} |
0 commit comments