|
| 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.protocol.tests.retry; |
| 17 | + |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 22 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 23 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; |
| 24 | + |
| 25 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 26 | +import com.github.tomakehurst.wiremock.stubbing.Scenario; |
| 27 | +import java.net.URI; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Rule; |
| 30 | +import org.junit.Test; |
| 31 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 32 | +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 33 | +import software.amazon.awssdk.regions.Region; |
| 34 | +import software.amazon.awssdk.retries.DefaultRetryStrategy; |
| 35 | +import software.amazon.awssdk.services.protocoljsonrpc.ProtocolJsonRpcClient; |
| 36 | +import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesRequest; |
| 37 | +import software.amazon.awssdk.services.protocoljsonrpc.model.AllTypesResponse; |
| 38 | +import software.amazon.awssdk.services.protocoljsonrpc.model.ProtocolJsonRpcException; |
| 39 | + |
| 40 | +public class RetryStrategyBackfillsEmptyRetryConditions { |
| 41 | + |
| 42 | + private static final String PATH = "/"; |
| 43 | + private static final String JSON_BODY = "{\"StringMember\":\"foo\"}"; |
| 44 | + |
| 45 | + @Rule |
| 46 | + public WireMockRule wireMock = new WireMockRule(0); |
| 47 | + |
| 48 | + private ProtocolJsonRpcClient client; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void setupClient() { |
| 52 | + client = ProtocolJsonRpcClient.builder() |
| 53 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", |
| 54 | + "skid"))) |
| 55 | + .region(Region.US_EAST_1) |
| 56 | + .overrideConfiguration(o -> o.retryStrategy(DefaultRetryStrategy.standardStrategyBuilder().build())) |
| 57 | + .endpointOverride(URI.create("http://localhost:" + wireMock.port())) |
| 58 | + .build(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void shouldRetryOn500() { |
| 63 | + stubFor(post(urlEqualTo(PATH)) |
| 64 | + .inScenario("retry at 500") |
| 65 | + .whenScenarioStateIs(Scenario.STARTED) |
| 66 | + .willSetStateTo("first attempt") |
| 67 | + .willReturn(aResponse() |
| 68 | + .withStatus(500))); |
| 69 | + |
| 70 | + stubFor(post(urlEqualTo(PATH)) |
| 71 | + .inScenario("retry at 500") |
| 72 | + .whenScenarioStateIs("first attempt") |
| 73 | + .willSetStateTo("second attempt") |
| 74 | + .willReturn(aResponse() |
| 75 | + .withStatus(200) |
| 76 | + .withBody(JSON_BODY))); |
| 77 | + |
| 78 | + AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); |
| 79 | + assertThat(allTypesResponse).isNotNull(); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void shouldRetryOnRetryableAwsErrorCode() { |
| 84 | + stubFor(post(urlEqualTo(PATH)) |
| 85 | + .inScenario("retry at PriorRequestNotComplete") |
| 86 | + .whenScenarioStateIs(Scenario.STARTED) |
| 87 | + .willSetStateTo("first attempt") |
| 88 | + .willReturn(aResponse() |
| 89 | + .withStatus(400) |
| 90 | + .withHeader("x-amzn-ErrorType", "PriorRequestNotComplete") |
| 91 | + .withBody("\"{\"__type\":\"PriorRequestNotComplete\",\"message\":\"Blah " |
| 92 | + + "error\"}\""))); |
| 93 | + |
| 94 | + stubFor(post(urlEqualTo(PATH)) |
| 95 | + .inScenario("retry at PriorRequestNotComplete") |
| 96 | + .whenScenarioStateIs("first attempt") |
| 97 | + .willSetStateTo("second attempt") |
| 98 | + .willReturn(aResponse() |
| 99 | + .withStatus(200) |
| 100 | + .withBody(JSON_BODY))); |
| 101 | + |
| 102 | + AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); |
| 103 | + assertThat(allTypesResponse).isNotNull(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void shouldRetryOnAwsThrottlingErrorCode() { |
| 108 | + stubFor(post(urlEqualTo(PATH)) |
| 109 | + .inScenario("retry at SlowDown") |
| 110 | + .whenScenarioStateIs(Scenario.STARTED) |
| 111 | + .willSetStateTo("first attempt") |
| 112 | + .willReturn(aResponse() |
| 113 | + .withStatus(400) |
| 114 | + .withHeader("x-amzn-ErrorType", "SlowDown") |
| 115 | + .withBody("\"{\"__type\":\"SlowDown\",\"message\":\"Blah " |
| 116 | + + "error\"}\""))); |
| 117 | + |
| 118 | + stubFor(post(urlEqualTo(PATH)) |
| 119 | + .inScenario("retry at SlowDown") |
| 120 | + .whenScenarioStateIs("first attempt") |
| 121 | + .willSetStateTo("second attempt") |
| 122 | + .willReturn(aResponse() |
| 123 | + .withStatus(200) |
| 124 | + .withBody(JSON_BODY))); |
| 125 | + |
| 126 | + AllTypesResponse allTypesResponse = client.allTypes(AllTypesRequest.builder().build()); |
| 127 | + assertThat(allTypesResponse).isNotNull(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void retryStrategyNone_shouldNotRetry() { |
| 132 | + stubFor(post(urlEqualTo(PATH)) |
| 133 | + .inScenario("retry at 500") |
| 134 | + .whenScenarioStateIs(Scenario.STARTED) |
| 135 | + .willSetStateTo("first attempt") |
| 136 | + .willReturn(aResponse() |
| 137 | + .withStatus(500))); |
| 138 | + |
| 139 | + stubFor(post(urlEqualTo(PATH)) |
| 140 | + .inScenario("retry at 500") |
| 141 | + .whenScenarioStateIs("first attempt") |
| 142 | + .willSetStateTo("second attempt") |
| 143 | + .willReturn(aResponse() |
| 144 | + .withStatus(200) |
| 145 | + .withBody(JSON_BODY))); |
| 146 | + |
| 147 | + ProtocolJsonRpcClient clientWithNoRetry = |
| 148 | + ProtocolJsonRpcClient.builder() |
| 149 | + .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid", |
| 150 | + "skid"))) |
| 151 | + .region(Region.US_EAST_1) |
| 152 | + .endpointOverride(URI.create("http://localhost:" + wireMock.port())) |
| 153 | + // When max attempts is 1 (i.e., just the first attempt is allowed but no retries), |
| 154 | + // the back filling should not happen. |
| 155 | + .overrideConfiguration(c -> c.retryStrategy(DefaultRetryStrategy.standardStrategyBuilder() |
| 156 | + .maxAttempts(1) |
| 157 | + .build())) |
| 158 | + .build(); |
| 159 | + |
| 160 | + assertThatThrownBy(() -> clientWithNoRetry.allTypes(AllTypesRequest.builder().build())).isInstanceOf(ProtocolJsonRpcException.class); |
| 161 | + } |
| 162 | +} |
0 commit comments