|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.protocol.tests.timeout;
|
17 | 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 | 18 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
23 |
| -import static software.amazon.awssdk.protocol.wiremock.WireMockUtils.verifyRequestCount; |
24 | 19 |
|
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; |
27 | 23 |
|
28 | 24 | public abstract class BaseApiCallAttemptTimeoutTest extends BaseTimeoutTest {
|
29 | 25 |
|
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); |
33 | 29 |
|
34 | 30 | @Test
|
35 | 31 | 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); |
38 | 33 | verifySuccessResponseNotTimedOut();
|
39 | 34 | }
|
40 | 35 |
|
41 | 36 | @Test
|
42 | 37 | 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); |
45 | 39 | verifyTimedOut();
|
46 | 40 | }
|
47 | 41 |
|
48 | 42 | @Test
|
49 | 43 | 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); |
54 | 45 | verifyFailedResponseNotTimedOut();
|
55 | 46 | }
|
56 | 47 |
|
57 | 48 | @Test
|
58 | 49 | 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); |
61 | 51 | verifyTimedOut();
|
62 | 52 | }
|
63 | 53 |
|
64 | 54 | @Test
|
65 | 55 | 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); |
69 | 57 | verifySuccessResponseNotTimedOut();
|
70 | 58 | }
|
71 | 59 |
|
72 | 60 | @Test
|
73 | 61 | 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); |
77 | 63 | verifyTimedOut();
|
78 | 64 | }
|
79 | 65 |
|
80 | 66 | @Test
|
81 | 67 | 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)); |
97 | 70 |
|
98 | 71 | assertThat(retryableCallable().call()).isNotNull();
|
99 |
| - verifyRequestCount(2, wireMock()); |
| 72 | + verifyRequestCount(2); |
100 | 73 | }
|
101 | 74 |
|
102 | 75 | @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)); |
118 | 79 | verifyRetraybleFailedResponseNotTimedOut();
|
| 80 | + verifyRequestCount(2); |
119 | 81 | }
|
120 | 82 |
|
121 | 83 | @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); |
138 | 86 | verifyRetryableTimeout();
|
139 | 87 | }
|
140 | 88 | }
|
0 commit comments