/
WaitersAsyncFunctionalTest.java
229 lines (191 loc) · 13.7 KB
/
WaitersAsyncFunctionalTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.awssdk.services.waiters;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.awscore.exception.AwsErrorDetails;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkServiceException;
import software.amazon.awssdk.core.retry.backoff.BackoffStrategy;
import software.amazon.awssdk.core.waiters.WaiterOverrideConfiguration;
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.services.restjsonwithwaiters.RestJsonWithWaitersAsyncClient;
import software.amazon.awssdk.services.restjsonwithwaiters.model.AllTypesRequest;
import software.amazon.awssdk.services.restjsonwithwaiters.model.AllTypesResponse;
import software.amazon.awssdk.services.restjsonwithwaiters.model.EmptyModeledException;
import software.amazon.awssdk.services.restjsonwithwaiters.waiters.RestJsonWithWaitersAsyncWaiter;
import software.amazon.awssdk.utils.CompletableFutureUtils;
import software.amazon.awssdk.utils.builder.SdkBuilder;
public class WaitersAsyncFunctionalTest {
public RestJsonWithWaitersAsyncClient asyncClient;
public RestJsonWithWaitersAsyncWaiter asyncWaiter;
@BeforeEach
public void setup() {
asyncClient = mock(RestJsonWithWaitersAsyncClient.class);
asyncWaiter = RestJsonWithWaitersAsyncWaiter.builder()
.client(asyncClient)
.overrideConfiguration(WaiterOverrideConfiguration.builder()
.maxAttempts(3)
.backoffStrategy(BackoffStrategy.none())
.build())
.build();
}
@AfterEach
public void cleanup() {
asyncClient.close();
asyncWaiter.close();
}
@Test
public void allTypeOperation_withAsyncWaiter_shouldReturnResponse() throws Exception {
AllTypesResponse response = (AllTypesResponse) AllTypesResponse.builder()
.sdkHttpResponse(SdkHttpResponse.builder()
.statusCode(200)
.build())
.build();
CompletableFuture<AllTypesResponse> serviceFuture = new CompletableFuture<>();
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture);
CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder()
.integerMember(1)
.build());
serviceFuture.complete(response);
assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(1);
assertThat(responseFuture.get().matched().response()).hasValueSatisfying(r -> assertThat(r).isEqualTo(response));
}
@Test
public void allTypeOperationFailed_withAsyncWaiter_shouldReturnException() throws Exception {
CompletableFuture<AllTypesResponse> serviceFuture = new CompletableFuture<>();
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture);
CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder().build());
serviceFuture.completeExceptionally(SdkServiceException.builder().statusCode(200).build());
assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(1);
assertThat(responseFuture.get().matched().exception()).hasValueSatisfying(r -> assertThat(r).isInstanceOf(SdkServiceException.class));
}
@Test
public void allTypeOperationRetry_withAsyncWaiter_shouldReturnResponseAfterException() throws Exception {
AllTypesResponse response2 = (AllTypesResponse) AllTypesResponse.builder()
.sdkHttpResponse(SdkHttpResponse.builder()
.statusCode(200)
.build())
.build();
CompletableFuture<AllTypesResponse> serviceFuture1 =
CompletableFutureUtils.failedFuture(SdkServiceException.builder().statusCode(404).build());
CompletableFuture<AllTypesResponse> serviceFuture2 = new CompletableFuture<>();
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture1, serviceFuture2);
CompletableFuture<WaiterResponse<AllTypesResponse>> responseFuture = asyncWaiter.waitUntilAllTypesSuccess(AllTypesRequest.builder().build());
serviceFuture2.complete(response2);
assertThat(responseFuture.get().attemptsExecuted()).isEqualTo(2);
assertThat(responseFuture.get().matched().response()).hasValueSatisfying(r -> assertThat(r).isEqualTo(response2));
}
@Test
public void requestOverrideConfig_shouldTakePrecedence() {
AllTypesResponse response = (AllTypesResponse) AllTypesResponse.builder()
.sdkHttpResponse(SdkHttpResponse.builder()
.statusCode(200)
.build())
.build();
CompletableFuture<AllTypesResponse> serviceFuture1 = CompletableFutureUtils.failedFuture(SdkServiceException.builder().statusCode(404).build());
CompletableFuture<AllTypesResponse> serviceFuture2 = CompletableFuture.completedFuture(response);
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(serviceFuture1, serviceFuture2);
assertThatThrownBy(() ->
asyncWaiter.waitUntilAllTypesSuccess(b -> b.build(), o -> o.maxAttempts(1)).join())
.hasMessageContaining("exceeded the max retry attempts").hasCauseInstanceOf(SdkClientException.class);
}
@Test
public void unexpectedException_shouldNotRetry() {
CompletableFuture<AllTypesResponse> failedFuture = CompletableFutureUtils.failedFuture(new RuntimeException(""));
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(failedFuture);
assertThatThrownBy(() -> asyncWaiter.waitUntilAllTypesSuccess(b -> b.build()).join())
.hasMessageContaining("An exception was thrown and did not match any waiter acceptors")
.hasCauseInstanceOf(SdkClientException.class);
}
@Test
public void unexpectedResponse_shouldRetry() {
AllTypesResponse response = (AllTypesResponse) AllTypesResponse.builder()
.sdkHttpResponse(SdkHttpResponse.builder()
.statusCode(200)
.build()).build();
CompletableFuture<AllTypesResponse> future1 =
CompletableFuture.completedFuture((AllTypesResponse) AllTypesResponse.builder()
.sdkHttpResponse(SdkHttpResponse.builder()
.statusCode(202)
.build()).build());
CompletableFuture<AllTypesResponse> future2 =
CompletableFuture.completedFuture(response);
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(future1, future2);
WaiterResponse<AllTypesResponse> waiterResponse = asyncWaiter.waitUntilAllTypesSuccess(b -> b.build()).join();
assertThat(waiterResponse.attemptsExecuted()).isEqualTo(2);
assertThat(waiterResponse.matched().response()).hasValueSatisfying(r -> assertThat(r).isEqualTo(response));
}
@Test
public void failureResponse_shouldThrowException() {
CompletableFuture<AllTypesResponse> future =
CompletableFuture.completedFuture((AllTypesResponse) AllTypesResponse.builder()
.sdkHttpResponse(SdkHttpResponse.builder()
.statusCode(500)
.build())
.build());
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(future);
assertThatThrownBy(() -> asyncWaiter.waitUntilAllTypesSuccess(SdkBuilder::build).join())
.hasMessageContaining("transitioned the waiter to failure state")
.hasCauseInstanceOf(SdkClientException.class);
}
@Test
public void failureException_shouldThrowException() {
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(CompletableFutureUtils.failedFuture(EmptyModeledException.builder()
.awsErrorDetails(AwsErrorDetails.builder()
.errorCode("EmptyModeledException")
.build())
.build()));
assertThatThrownBy(() -> asyncWaiter.waitUntilAllTypesSuccess(SdkBuilder::build).join())
.hasMessageContaining("transitioned the waiter to failure state")
.hasCauseInstanceOf(SdkClientException.class);
}
@Test
void errorShouldNotBeWrapped() {
when(asyncClient.allTypes(any(AllTypesRequest.class))).thenReturn(CompletableFutureUtils.failedFuture(new OutOfMemoryError()));
assertThatThrownBy(() -> asyncWaiter.waitUntilAllTypesSuccess(SdkBuilder::build).join())
.hasCauseInstanceOf(Error.class);
}
@Test
public void closeWaiterCreatedWithClient_clientDoesNotClose() {
asyncWaiter.close();
verify(asyncClient, never()).close();
}
@Test
public void closeWaiterCreatedWithExecutorService_executorServiceDoesNotClose() {
ScheduledExecutorService executorService = mock(ScheduledExecutorService.class);
RestJsonWithWaitersAsyncWaiter newWaiter = RestJsonWithWaitersAsyncWaiter.builder()
.client(asyncClient)
.scheduledExecutorService(executorService)
.overrideConfiguration(WaiterOverrideConfiguration.builder()
.maxAttempts(3)
.backoffStrategy(BackoffStrategy.none())
.build())
.build();
newWaiter.close();
verify(executorService, never()).shutdown();
}
}