Skip to content

Commit 2fafa08

Browse files
author
Bennett Lynch
authored
Make API Gateway integ test's deleteRestApi more reliable & best-effort (#3083)
1 parent 5a55c3a commit 2fafa08

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

services/apigateway/src/it/java/software/amazon/awssdk/services/apigateway/ServiceIntegrationTest.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@
4848
import software.amazon.awssdk.services.apigateway.model.PutMethodResponse;
4949
import software.amazon.awssdk.services.apigateway.model.Resource;
5050
import software.amazon.awssdk.services.apigateway.model.RestApi;
51+
import software.amazon.awssdk.services.apigateway.model.TooManyRequestsException;
5152
import software.amazon.awssdk.services.apigateway.model.UpdateApiKeyRequest;
5253
import software.amazon.awssdk.services.apigateway.model.UpdateResourceRequest;
5354
import software.amazon.awssdk.services.apigateway.model.UpdateRestApiRequest;
55+
import software.amazon.awssdk.utils.Lazy;
5456
import software.amazon.awssdk.utils.Logger;
5557

5658
public class ServiceIntegrationTest extends IntegrationTestBase {
@@ -61,6 +63,10 @@ public class ServiceIntegrationTest extends IntegrationTestBase {
6163

6264
private static final String DESCRIPTION = "fooDesc";
6365

66+
// Limit deletes to once every 31 seconds
67+
// https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-control-service-limits-table
68+
private static final Lazy<RateLimiter> DELETE_RATE_LIMITER = new Lazy<>(() -> RateLimiter.create(1.0 / 31));
69+
6470
private static String restApiId = null;
6571

6672
@BeforeClass
@@ -84,7 +90,14 @@ public static void createRestApi() {
8490
@AfterClass
8591
public static void deleteRestApiKey() {
8692
if (restApiId != null) {
87-
apiGateway.deleteRestApi(DeleteRestApiRequest.builder().restApiId(restApiId).build());
93+
DELETE_RATE_LIMITER.getValue().acquire();
94+
try {
95+
apiGateway.deleteRestApi(DeleteRestApiRequest.builder().restApiId(restApiId).build());
96+
} catch (TooManyRequestsException e) {
97+
log.warn(() -> String.format("Failed to delete REST API %s (%s). This API should be deleted automatically in a "
98+
+ "future 'deleteStaleRestApis' execution.",
99+
NAME, restApiId), e);
100+
}
88101
}
89102
}
90103

@@ -95,10 +108,6 @@ private static void deleteStaleRestApis() {
95108
AtomicInteger success = new AtomicInteger();
96109
AtomicInteger failure = new AtomicInteger();
97110

98-
// Limit deletes to once every 31 seconds
99-
// https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#api-gateway-control-service-limits-table
100-
RateLimiter rateLimiter = RateLimiter.create(1.0 / 31);
101-
102111
log.info(() -> String.format("Searching for stale REST APIs older than %s days...", maxApiAge.toDays()));
103112
for (RestApi api : apiGateway.getRestApisPaginator().items()) {
104113
if (Instant.now().isAfter(startTime.plus(maxRunTime))) {
@@ -109,7 +118,7 @@ private static void deleteStaleRestApis() {
109118
}
110119
Duration apiAge = Duration.between(api.createdDate(), Instant.now());
111120
if (api.name().startsWith(NAME_PREFIX) && apiAge.compareTo(maxApiAge) > 0) {
112-
rateLimiter.acquire();
121+
DELETE_RATE_LIMITER.getValue().acquire();
113122
try {
114123
apiGateway.deleteRestApi(r -> r.restApiId(api.id()));
115124
log.info(() -> String.format("Successfully deleted REST API %s (%s) which was %s days old.",

0 commit comments

Comments
 (0)