Skip to content

Commit cf9640b

Browse files
authored
Use mock HTTP clients in timeout tests (#3983)
* Use mock HTTP clients in timeout tests * Fix checkstyle errors * Use JUnit5 and address other feedback
1 parent c98aad9 commit cf9640b

File tree

15 files changed

+318
-435
lines changed

15 files changed

+318
-435
lines changed

test/protocol-tests/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@
188188
<artifactId>eventstream</artifactId>
189189
<scope>test</scope>
190190
</dependency>
191+
<dependency>
192+
<groupId>software.amazon.awssdk</groupId>
193+
<artifactId>service-test-utils</artifactId>
194+
<version>${awsjavasdk.version}</version>
195+
<scope>test</scope>
196+
</dependency>
191197
</dependencies>
192198

193199
<build>

test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/connection/SyncClientConnectionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@
3131
import software.amazon.awssdk.http.HttpExecuteResponse;
3232
import software.amazon.awssdk.http.SdkHttpResponse;
3333
import software.amazon.awssdk.protocol.tests.util.ClosableStringInputStream;
34-
import software.amazon.awssdk.protocol.tests.util.MockHttpClient;
3534
import software.amazon.awssdk.regions.Region;
3635
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;
3736
import software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest;
3837
import software.amazon.awssdk.services.protocolrestjson.model.ProtocolRestJsonException;
3938
import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse;
39+
import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient;
4040
import software.amazon.awssdk.utils.builder.SdkBuilder;
4141

4242
/**
4343
* Tests to verify the correction of connection closure.
4444
*/
4545
public class SyncClientConnectionTest {
4646
private ProtocolRestJsonClient client;
47-
private MockHttpClient mockHttpClient;
47+
private MockSyncHttpClient mockHttpClient;
4848

4949
@BeforeEach
5050
public void setupClient() {
51-
mockHttpClient = new MockHttpClient();
51+
mockHttpClient = new MockSyncHttpClient();
5252
client = ProtocolRestJsonClient.builder()
5353
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid",
5454
"skid")))

test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/timeout/BaseApiCallAttemptTimeoutTest.java

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -15,126 +15,74 @@
1515

1616
package software.amazon.awssdk.protocol.tests.timeout;
1717

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;
2218
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
23-
import static software.amazon.awssdk.protocol.wiremock.WireMockUtils.verifyRequestCount;
2419

25-
import com.github.tomakehurst.wiremock.stubbing.Scenario;
26-
import org.junit.Test;
20+
import java.time.Duration;
21+
import org.junit.jupiter.api.Test;
22+
import software.amazon.awssdk.utils.Pair;
2723

2824
public abstract class BaseApiCallAttemptTimeoutTest extends BaseTimeoutTest {
2925

30-
protected static final int API_CALL_ATTEMPT_TIMEOUT = 800;
31-
protected static final int DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT = 100;
32-
protected static final int DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT = 1000;
26+
protected static final Duration API_CALL_ATTEMPT_TIMEOUT = Duration.ofMillis(100);
27+
protected static final Duration DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT = Duration.ofMillis(50);
28+
protected static final Duration DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT = Duration.ofMillis(150);
3329

3430
@Test
3531
public void nonstreamingOperation200_finishedWithinTime_shouldSucceed() throws Exception {
36-
stubFor(post(anyUrl())
37-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
32+
stubSuccessResponse(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT);
3833
verifySuccessResponseNotTimedOut();
3934
}
4035

4136
@Test
4237
public void nonstreamingOperation200_notFinishedWithinTime_shouldTimeout() {
43-
stubFor(post(anyUrl())
44-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
38+
stubSuccessResponse(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT);
4539
verifyTimedOut();
4640
}
4741

4842
@Test
4943
public void nonstreamingOperation500_finishedWithinTime_shouldNotTimeout() throws Exception {
50-
stubFor(post(anyUrl())
51-
.willReturn(aResponse().withStatus(500)
52-
.withHeader("x-amzn-ErrorType", "EmptyModeledException")
53-
.withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
44+
stubErrorResponse(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT);
5445
verifyFailedResponseNotTimedOut();
5546
}
5647

5748
@Test
5849
public void nonstreamingOperation500_notFinishedWithinTime_shouldTimeout() {
59-
stubFor(post(anyUrl())
60-
.willReturn(aResponse().withStatus(500).withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
50+
stubErrorResponse(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT);
6151
verifyTimedOut();
6252
}
6353

6454
@Test
6555
public void streamingOperation_finishedWithinTime_shouldSucceed() throws Exception {
66-
stubFor(post(anyUrl())
67-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
68-
56+
stubSuccessResponse(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT);
6957
verifySuccessResponseNotTimedOut();
7058
}
7159

7260
@Test
7361
public void streamingOperation_notFinishedWithinTime_shouldTimeout() {
74-
stubFor(post(anyUrl())
75-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
76-
62+
stubSuccessResponse(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT);
7763
verifyTimedOut();
7864
}
7965

8066
@Test
8167
public void firstAttemptTimeout_retryFinishWithInTime_shouldNotTimeout() throws Exception {
82-
stubFor(post(anyUrl())
83-
.inScenario("timed out in the first attempt")
84-
.whenScenarioStateIs(Scenario.STARTED)
85-
.willSetStateTo("first attempt")
86-
.willReturn(aResponse()
87-
.withStatus(200).withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
88-
89-
stubFor(post(anyUrl())
90-
.inScenario("timed out in the first attempt")
91-
.whenScenarioStateIs("first attempt")
92-
.willSetStateTo("second attempt")
93-
.willReturn(aResponse()
94-
.withStatus(200)
95-
.withBody("{}")
96-
.withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
68+
mockHttpClient().stubResponses(Pair.of(mockResponse(200), DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT),
69+
Pair.of(mockResponse(200), DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT));
9770

9871
assertThat(retryableCallable().call()).isNotNull();
99-
verifyRequestCount(2, wireMock());
72+
verifyRequestCount(2);
10073
}
10174

10275
@Test
103-
public void firstAttemptTimeout_retryFinishWithInTime500_shouldNotTimeout() throws Exception {
104-
stubFor(post(anyUrl())
105-
.inScenario("timed out in the first attempt")
106-
.whenScenarioStateIs(Scenario.STARTED)
107-
.willSetStateTo("first attempt")
108-
.willReturn(aResponse()
109-
.withStatus(200).withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
110-
111-
stubFor(post(anyUrl())
112-
.inScenario("timed out in the first attempt")
113-
.whenScenarioStateIs("first attempt")
114-
.willSetStateTo("second attempt")
115-
.willReturn(aResponse()
116-
.withStatus(500)
117-
.withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
76+
public void firstAttemptTimeout_retryFinishWithInTime500_shouldNotTimeout() {
77+
mockHttpClient().stubResponses(Pair.of(mockResponse(200), DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT),
78+
Pair.of(mockResponse(500), DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT));
11879
verifyRetraybleFailedResponseNotTimedOut();
80+
verifyRequestCount(2);
11981
}
12082

12183
@Test
122-
public void allAttemtsNotFinishedWithinTime_shouldTimeout() {
123-
stubFor(post(anyUrl())
124-
.inScenario("timed out in both attempts")
125-
.whenScenarioStateIs(Scenario.STARTED)
126-
.willSetStateTo("first attempt")
127-
.willReturn(aResponse()
128-
.withStatus(200).withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
129-
130-
stubFor(post(anyUrl())
131-
.inScenario("timed out in both attempts")
132-
.whenScenarioStateIs("first attempt")
133-
.willSetStateTo("second attempt")
134-
.willReturn(aResponse()
135-
.withStatus(200)
136-
.withBody("{}")
137-
.withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
84+
public void allAttemptsNotFinishedWithinTime_shouldTimeout() {
85+
stubSuccessResponse(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT);
13886
verifyRetryableTimeout();
13987
}
14088
}

test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/timeout/BaseApiCallTimeoutTest.java

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -15,110 +15,73 @@
1515

1616
package software.amazon.awssdk.protocol.tests.timeout;
1717

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;
2218
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
23-
import static software.amazon.awssdk.protocol.wiremock.WireMockUtils.verifyRequestCount;
2419

25-
import com.github.tomakehurst.wiremock.stubbing.Scenario;
26-
import org.junit.Test;
20+
import java.time.Duration;
21+
import org.junit.jupiter.api.Test;
22+
import software.amazon.awssdk.utils.Pair;
2723

2824
/**
2925
* Contains common scenarios to test timeout feature.
3026
*/
3127
public abstract class BaseApiCallTimeoutTest extends BaseTimeoutTest {
3228

33-
protected static final int TIMEOUT = 1000;
34-
protected static final int DELAY_BEFORE_TIMEOUT = 100;
35-
protected static final int DELAY_AFTER_TIMEOUT = 1200;
29+
protected static final Duration TIMEOUT = Duration.ofMillis(300);
30+
31+
protected static final Duration DELAY_BEFORE_TIMEOUT = Duration.ofMillis(10);
32+
protected static final Duration DELAY_AFTER_TIMEOUT = Duration.ofMillis(500);
3633

3734
@Test
3835
public void nonstreamingOperation_finishedWithinTime_shouldNotTimeout() throws Exception {
39-
stubFor(post(anyUrl())
40-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_BEFORE_TIMEOUT)));
36+
stubSuccessResponse(DELAY_BEFORE_TIMEOUT);
4137
verifySuccessResponseNotTimedOut();
4238
}
4339

4440
@Test
4541
public void nonstreamingOperation_notFinishedWithinTime_shouldTimeout() {
46-
stubFor(post(anyUrl())
47-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_TIMEOUT)));
42+
stubSuccessResponse(DELAY_AFTER_TIMEOUT);
4843
verifyTimedOut();
4944
}
5045

5146
@Test
5247
public void nonstreamingOperation500_notFinishedWithinTime_shouldTimeout() {
53-
stubFor(post(anyUrl())
54-
.willReturn(aResponse().withStatus(500).withFixedDelay(DELAY_AFTER_TIMEOUT)));
48+
stubErrorResponse(DELAY_AFTER_TIMEOUT);
5549
verifyTimedOut();
5650
}
5751

5852
@Test
5953
public void nonstreamingOperation500_finishedWithinTime_shouldNotTimeout() throws Exception {
60-
stubFor(post(anyUrl())
61-
.willReturn(aResponse().withStatus(500).withFixedDelay(DELAY_BEFORE_TIMEOUT)));
54+
stubErrorResponse(DELAY_BEFORE_TIMEOUT);
6255
verifyFailedResponseNotTimedOut();
6356
}
6457

6558
@Test
66-
public void nonstreamingOperation_retrySucceeded_FinishedWithinTime_shouldNotTimeout() throws Exception {
67-
68-
stubFor(post(anyUrl())
69-
.inScenario("retry at 500")
70-
.whenScenarioStateIs(Scenario.STARTED)
71-
.willSetStateTo("first attempt")
72-
.willReturn(aResponse()
73-
.withStatus(500).withFixedDelay(DELAY_BEFORE_TIMEOUT)));
74-
75-
stubFor(post(anyUrl())
76-
.inScenario("retry at 500")
77-
.whenScenarioStateIs("first attempt")
78-
.willSetStateTo("second attempt")
79-
.willReturn(aResponse()
80-
.withStatus(200)
81-
.withBody("{}").withFixedDelay(DELAY_BEFORE_TIMEOUT)));
82-
83-
assertThat(retryableCallable().call()).isNotNull();
59+
public void streamingOperation_finishedWithinTime_shouldNotTimeout() throws Exception {
60+
stubSuccessResponse(DELAY_BEFORE_TIMEOUT);
61+
verifySuccessResponseNotTimedOut();
8462
}
8563

8664
@Test
87-
public void nonstreamingOperation_retryWouldSucceed_notFinishedWithinTime_shouldTimeout() {
88-
89-
stubFor(post(anyUrl())
90-
.inScenario("retry at 500")
91-
.whenScenarioStateIs(Scenario.STARTED)
92-
.willSetStateTo("first attempt")
93-
.willReturn(aResponse()
94-
.withStatus(500).withFixedDelay(DELAY_BEFORE_TIMEOUT)));
95-
96-
stubFor(post(anyUrl())
97-
.inScenario("retry at 500")
98-
.whenScenarioStateIs("first attempt")
99-
.willSetStateTo("second attempt")
100-
.willReturn(aResponse()
101-
.withStatus(200)
102-
.withBody("{}").withFixedDelay(DELAY_AFTER_TIMEOUT)));
103-
104-
105-
verifyRetryableTimeout();
106-
verifyRequestCount(2, wireMock());
65+
public void streamingOperation_notFinishedWithinTime_shouldTimeout() {
66+
stubSuccessResponse(DELAY_AFTER_TIMEOUT);
67+
verifyTimedOut();
10768
}
10869

10970
@Test
110-
public void streamingOperation_finishedWithinTime_shouldNotTimeout() throws Exception {
111-
stubFor(post(anyUrl())
112-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_BEFORE_TIMEOUT)));
71+
public void nonstreamingOperation_retrySucceeded_FinishedWithinTime_shouldNotTimeout() throws Exception {
72+
mockHttpClient().stubResponses(Pair.of(mockResponse(500), DELAY_BEFORE_TIMEOUT),
73+
Pair.of(mockResponse(200), DELAY_BEFORE_TIMEOUT));
11374

114-
verifySuccessResponseNotTimedOut();
75+
assertThat(retryableCallable().call()).isNotNull();
76+
verifyRequestCount(2);
11577
}
11678

11779
@Test
118-
public void streamingOperation_notFinishedWithinTime_shouldTimeout() {
119-
stubFor(post(anyUrl())
120-
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_TIMEOUT)));
80+
public void nonstreamingOperation_retryWouldSucceed_notFinishedWithinTime_shouldTimeout() {
81+
mockHttpClient().stubResponses(Pair.of(mockResponse(500), DELAY_BEFORE_TIMEOUT),
82+
Pair.of(mockResponse(200), DELAY_AFTER_TIMEOUT));
12183

122-
verifyTimedOut();
84+
verifyRetryableTimeout();
85+
verifyRequestCount(2);
12386
}
12487
}

test/protocol-tests/src/test/java/software/amazon/awssdk/protocol/tests/timeout/BaseTimeoutTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@
1818

1919
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
2020

21-
import com.github.tomakehurst.wiremock.junit.WireMockRule;
2221
import java.io.File;
2322
import java.io.IOException;
23+
import java.time.Duration;
2424
import java.util.concurrent.Callable;
2525
import java.util.function.Consumer;
2626
import org.assertj.core.api.ThrowableAssert;
2727
import software.amazon.awssdk.core.ResponseBytes;
2828
import software.amazon.awssdk.core.ResponseInputStream;
2929
import software.amazon.awssdk.core.sync.ResponseTransformer;
3030
import software.amazon.awssdk.http.AbortableInputStream;
31+
import software.amazon.awssdk.http.HttpExecuteResponse;
32+
import software.amazon.awssdk.http.SdkHttpResponse;
33+
import software.amazon.awssdk.testutils.service.http.MockHttpClient;
3134

3235
public abstract class BaseTimeoutTest {
3336

@@ -44,7 +47,9 @@ public abstract class BaseTimeoutTest {
4447

4548
protected abstract Callable streamingCallable();
4649

47-
protected abstract WireMockRule wireMock();
50+
protected abstract void stubSuccessResponse(Duration delayAfterTimeout);
51+
52+
protected abstract void stubErrorResponse(Duration delayAfterTimeout);
4853

4954
protected void verifySuccessResponseNotTimedOut() throws Exception {
5055
assertThat(callable().call()).isNotNull();
@@ -129,4 +134,18 @@ public Object transform(Object response, AbortableInputStream inputStream) throw
129134
public static void wastingTimeInterruptibly() throws InterruptedException {
130135
Thread.sleep(1200);
131136
}
137+
138+
public abstract MockHttpClient mockHttpClient();
139+
140+
public static HttpExecuteResponse mockResponse(int statusCode) {
141+
return HttpExecuteResponse.builder()
142+
.response(SdkHttpResponse.builder()
143+
.statusCode(statusCode)
144+
.build())
145+
.build();
146+
}
147+
148+
public void verifyRequestCount(int requestCount) {
149+
assertThat(mockHttpClient().getRequests().size()).isEqualTo(requestCount);
150+
}
132151
}

0 commit comments

Comments
 (0)