|
| 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.services.waiters; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.Matchers.any; |
| 20 | +import static org.mockito.Mockito.mock; |
| 21 | +import static org.mockito.Mockito.never; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | +import static org.mockito.Mockito.when; |
| 24 | + |
| 25 | +import java.util.concurrent.CompletableFuture; |
| 26 | +import java.util.concurrent.ExecutionException; |
| 27 | +import java.util.concurrent.ScheduledExecutorService; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import software.amazon.awssdk.core.exception.SdkServiceException; |
| 32 | +import software.amazon.awssdk.core.retry.backoff.BackoffStrategy; |
| 33 | +import software.amazon.awssdk.core.waiters.WaiterOverrideConfiguration; |
| 34 | +import software.amazon.awssdk.core.waiters.WaiterResponse; |
| 35 | +import software.amazon.awssdk.http.SdkHttpResponse; |
| 36 | +import software.amazon.awssdk.services.restjsonwithwaiters.RestJsonWithWaitersAsyncClient; |
| 37 | +import software.amazon.awssdk.services.restjsonwithwaiters.model.AllTypesRequest; |
| 38 | +import software.amazon.awssdk.services.restjsonwithwaiters.model.AllTypesResponse; |
| 39 | +import software.amazon.awssdk.services.restjsonwithwaiters.waiters.RestJsonWithWaitersAsyncWaiter; |
| 40 | + |
| 41 | +public class WaitersAsyncFunctionalTest { |
| 42 | + |
| 43 | + public RestJsonWithWaitersAsyncClient asyncClient; |
| 44 | + public RestJsonWithWaitersAsyncWaiter asyncWaiter; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setup() { |
| 48 | + asyncClient = mock(RestJsonWithWaitersAsyncClient.class); |
| 49 | + asyncWaiter = RestJsonWithWaitersAsyncWaiter.builder() |
| 50 | + .client(asyncClient) |
| 51 | + .overrideConfiguration(WaiterOverrideConfiguration.builder() |
| 52 | + .maxAttempts(3) |
| 53 | + .backoffStrategy(BackoffStrategy.none()) |
| 54 | + .build()) |
| 55 | + .build(); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void cleanup() { |
| 60 | + asyncClient.close(); |
| 61 | + asyncWaiter.close(); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void allTypeOperation_withAsyncWaiter_shouldReturnResponse() throws ExecutionException, InterruptedException { |
| 66 | + AllTypesResponse response = (AllTypesResponse) AllTypesResponse.builder() |
| 67 | + .sdkHttpResponse(SdkHttpResponse.builder() |
| 68 | + .statusCode(200) |
| 69 | + .build()) |
| 70 | + .build(); |
| 71 | + |
| 72 | + CompletableFuture<AllTypesResponse> serviceFuture = new CompletableFuture<>(); |
| 73 | + |
| 74 | + |
| 75 | + when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture); |
| 76 | + CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder() |
| 77 | + .integerMember(1) |
| 78 | + .build()); |
| 79 | + serviceFuture.complete(response); |
| 80 | + |
| 81 | + assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(1); |
| 82 | + assertThat(responseFuture.get().matched().response()).hasValueSatisfying(r -> assertThat(r).isEqualTo(response)); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void allTypeOperationFailed_withAsyncWaiter_shouldReturnException() throws ExecutionException, InterruptedException { |
| 87 | + CompletableFuture<AllTypesResponse> serviceFuture = new CompletableFuture<>(); |
| 88 | + |
| 89 | + when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture); |
| 90 | + CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder().build()); |
| 91 | + |
| 92 | + serviceFuture.completeExceptionally(SdkServiceException.builder().statusCode(200).build()); |
| 93 | + |
| 94 | + assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(1); |
| 95 | + assertThat(responseFuture.get().matched().exception()).hasValueSatisfying(r -> assertThat(r).isInstanceOf(SdkServiceException.class)); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void allTypeOperationRetry_withAsyncWaiter_shouldReturnResponseAfterException() throws ExecutionException, InterruptedException { |
| 100 | + AllTypesResponse response1 = (AllTypesResponse) AllTypesResponse.builder() |
| 101 | + .sdkHttpResponse(SdkHttpResponse.builder() |
| 102 | + .statusCode(404) |
| 103 | + .build()) |
| 104 | + .build(); |
| 105 | + AllTypesResponse response2 = (AllTypesResponse) AllTypesResponse.builder() |
| 106 | + .sdkHttpResponse(SdkHttpResponse.builder() |
| 107 | + .statusCode(200) |
| 108 | + .build()) |
| 109 | + .build(); |
| 110 | + |
| 111 | + CompletableFuture<AllTypesResponse> serviceFuture1 = new CompletableFuture<>(); |
| 112 | + CompletableFuture<AllTypesResponse> serviceFuture2 = new CompletableFuture<>(); |
| 113 | + |
| 114 | + when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture1, serviceFuture2); |
| 115 | + |
| 116 | + CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder().build()); |
| 117 | + |
| 118 | + serviceFuture1.complete(response1); |
| 119 | + serviceFuture2.complete(response2); |
| 120 | + |
| 121 | + assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(2); |
| 122 | + assertThat(responseFuture.get().matched().response()).hasValueSatisfying(r -> assertThat(r).isEqualTo(response2)); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void closeWaiterCreatedWithClient_clientDoesNotClose() { |
| 127 | + asyncWaiter.close(); |
| 128 | + verify(asyncClient, never()).close(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void closeWaiterCreatedWithExecutorService_executorServiceDoesNotClose() { |
| 133 | + ScheduledExecutorService executorService = mock(ScheduledExecutorService.class); |
| 134 | + RestJsonWithWaitersAsyncWaiter newWaiter = RestJsonWithWaitersAsyncWaiter.builder() |
| 135 | + .scheduledExecutorService(executorService) |
| 136 | + .overrideConfiguration(WaiterOverrideConfiguration.builder() |
| 137 | + .maxAttempts(3) |
| 138 | + .backoffStrategy(BackoffStrategy.none()) |
| 139 | + .build()) |
| 140 | + .build(); |
| 141 | + |
| 142 | + newWaiter.close(); |
| 143 | + verify(executorService, never()).shutdown(); |
| 144 | + } |
| 145 | +} |
0 commit comments