Skip to content

Commit ecea40d

Browse files
committed
add functional test cases with wiremock and explicilty state that total attempts is 9
1 parent cbba880 commit ecea40d

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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.dynamodb;
17+
18+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19+
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
20+
import static com.github.tomakehurst.wiremock.client.WireMock.post;
21+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
24+
25+
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
26+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
27+
import java.net.URI;
28+
import org.junit.jupiter.api.AfterEach;
29+
import org.junit.jupiter.api.BeforeEach;
30+
import org.junit.jupiter.api.Test;
31+
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
32+
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
33+
import software.amazon.awssdk.core.SdkSystemSetting;
34+
import software.amazon.awssdk.regions.Region;
35+
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
36+
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;
37+
38+
/**
39+
* Functional tests to verify DynamoDB client retry behavior with different retry modes.
40+
*/
41+
@WireMockTest
42+
class DynamoDbDefaultRetryFunctionalTest {
43+
44+
private EnvironmentVariableHelper environmentVariableHelper;
45+
private DynamoDbClient dynamoDbClient;
46+
47+
@BeforeEach
48+
void setup(WireMockRuntimeInfo wm) {
49+
environmentVariableHelper = new EnvironmentVariableHelper();
50+
51+
dynamoDbClient = DynamoDbClient.builder()
52+
.endpointOverride(URI.create(wm.getHttpBaseUrl()))
53+
.credentialsProvider(StaticCredentialsProvider.create(
54+
AwsBasicCredentials.create("test", "test")))
55+
.region(Region.US_EAST_1)
56+
.build();
57+
}
58+
59+
@AfterEach
60+
void tearDown() {
61+
environmentVariableHelper.reset();
62+
if (dynamoDbClient != null) {
63+
dynamoDbClient.close();
64+
}
65+
}
66+
67+
@Test
68+
void listTables_whenNoRetryPolicySet_shouldAttempt9Times(WireMockRuntimeInfo wm) {
69+
stubFor(post(anyUrl())
70+
.willReturn(aResponse().withStatus(503)));
71+
assertThatExceptionOfType(DynamoDbException.class)
72+
.isThrownBy(() -> dynamoDbClient.listTables());
73+
int actualAttempts = wm.getWireMock().getAllServeEvents().size();
74+
assertThat(actualAttempts)
75+
.as("Default retry mode should result in 9 total attempts (1 initial + 8 retries)")
76+
.isEqualTo(9);
77+
}
78+
79+
@Test
80+
void listTables_whenRetryModeSetToStandard_shouldAttempt10Times(WireMockRuntimeInfo wm) {
81+
environmentVariableHelper.set(SdkSystemSetting.AWS_RETRY_MODE.environmentVariable(), "standard");
82+
dynamoDbClient.close();
83+
dynamoDbClient = DynamoDbClient.builder()
84+
.endpointOverride(URI.create(wm.getHttpBaseUrl()))
85+
.credentialsProvider(StaticCredentialsProvider.create(
86+
AwsBasicCredentials.create("test", "test")))
87+
.region(Region.US_EAST_1)
88+
.build();
89+
stubFor(post(anyUrl())
90+
.willReturn(aResponse().withStatus(503)));
91+
assertThatExceptionOfType(DynamoDbException.class)
92+
.isThrownBy(() -> dynamoDbClient.listTables());
93+
int actualAttempts = wm.getWireMock().getAllServeEvents().size();
94+
assertThat(actualAttempts)
95+
.as("Standard retry mode should result in 9 total attempts (1 initial + 8 retries)")
96+
.isEqualTo(9);
97+
}
98+
99+
@Test
100+
void listTables_whenUsingDefaultRetryMode_shouldAttempt9Times(WireMockRuntimeInfo wm) {
101+
stubFor(post(anyUrl())
102+
.willReturn(aResponse().withStatus(503)));
103+
assertThatExceptionOfType(DynamoDbException.class)
104+
.isThrownBy(() -> dynamoDbClient.listTables());
105+
int actualAttempts = wm.getWireMock().getAllServeEvents().size();
106+
assertThat(actualAttempts)
107+
.as("Default retry mode should result in 9 total attempts (1 initial + 8 retries)")
108+
.isEqualTo(9);
109+
}
110+
}

services/dynamodb/src/test/java/software/amazon/awssdk/services/dynamodb/DynamoDbRetryPolicyTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,10 @@ void resolve_retryModeNotSetWithEnvNorSupplier_resolvesFromSdkDefault() {
126126
assertThat(retryMode).isEqualTo(RetryMode.STANDARD);
127127
}
128128

129+
@Test
130+
void test_numRetries_with_defaultSettings() {
131+
SdkClientConfiguration sdkClientConfiguration = SdkClientConfiguration.builder().build();
132+
RetryStrategy retryStrategy = DynamoDbRetryPolicy.resolveRetryStrategy(sdkClientConfiguration);
133+
assertThat(retryStrategy.maxAttempts()).isEqualTo(9);
134+
}
129135
}

0 commit comments

Comments
 (0)