Skip to content

Use mock HTTP clients in timeout tests #3983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions test/protocol-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@
<artifactId>eventstream</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>service-test-utils</artifactId>
<version>${awsjavasdk.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,24 @@
import software.amazon.awssdk.http.HttpExecuteResponse;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.protocol.tests.util.ClosableStringInputStream;
import software.amazon.awssdk.protocol.tests.util.MockHttpClient;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.protocolrestjson.ProtocolRestJsonClient;
import software.amazon.awssdk.services.protocolrestjson.model.AllTypesRequest;
import software.amazon.awssdk.services.protocolrestjson.model.ProtocolRestJsonException;
import software.amazon.awssdk.services.protocolrestjson.model.StreamingOutputOperationResponse;
import software.amazon.awssdk.testutils.service.http.MockSyncHttpClient;
import software.amazon.awssdk.utils.builder.SdkBuilder;

/**
* Tests to verify the correction of connection closure.
*/
public class SyncClientConnectionTest {
private ProtocolRestJsonClient client;
private MockHttpClient mockHttpClient;
private MockSyncHttpClient mockHttpClient;

@BeforeEach
public void setupClient() {
mockHttpClient = new MockHttpClient();
mockHttpClient = new MockSyncHttpClient();
client = ProtocolRestJsonClient.builder()
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("akid",
"skid")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,126 +15,74 @@

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

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static software.amazon.awssdk.protocol.wiremock.WireMockUtils.verifyRequestCount;

import com.github.tomakehurst.wiremock.stubbing.Scenario;
import org.junit.Test;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.utils.Pair;

public abstract class BaseApiCallAttemptTimeoutTest extends BaseTimeoutTest {

protected static final int API_CALL_ATTEMPT_TIMEOUT = 800;
protected static final int DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT = 100;
protected static final int DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT = 1000;
protected static final Duration API_CALL_ATTEMPT_TIMEOUT = Duration.ofMillis(100);
protected static final Duration DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT = Duration.ofMillis(50);
protected static final Duration DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT = Duration.ofMillis(150);

@Test
public void nonstreamingOperation200_finishedWithinTime_shouldSucceed() throws Exception {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
stubSuccessResponse(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT);
verifySuccessResponseNotTimedOut();
}

@Test
public void nonstreamingOperation200_notFinishedWithinTime_shouldTimeout() {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
stubSuccessResponse(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT);
verifyTimedOut();
}

@Test
public void nonstreamingOperation500_finishedWithinTime_shouldNotTimeout() throws Exception {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(500)
.withHeader("x-amzn-ErrorType", "EmptyModeledException")
.withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
stubErrorResponse(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT);
verifyFailedResponseNotTimedOut();
}

@Test
public void nonstreamingOperation500_notFinishedWithinTime_shouldTimeout() {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(500).withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
stubErrorResponse(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT);
verifyTimedOut();
}

@Test
public void streamingOperation_finishedWithinTime_shouldSucceed() throws Exception {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));

stubSuccessResponse(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT);
verifySuccessResponseNotTimedOut();
}

@Test
public void streamingOperation_notFinishedWithinTime_shouldTimeout() {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));

stubSuccessResponse(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT);
verifyTimedOut();
}

@Test
public void firstAttemptTimeout_retryFinishWithInTime_shouldNotTimeout() throws Exception {
stubFor(post(anyUrl())
.inScenario("timed out in the first attempt")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first attempt")
.willReturn(aResponse()
.withStatus(200).withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));

stubFor(post(anyUrl())
.inScenario("timed out in the first attempt")
.whenScenarioStateIs("first attempt")
.willSetStateTo("second attempt")
.willReturn(aResponse()
.withStatus(200)
.withBody("{}")
.withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
mockHttpClient().stubResponses(Pair.of(mockResponse(200), DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT),
Pair.of(mockResponse(200), DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT));

assertThat(retryableCallable().call()).isNotNull();
verifyRequestCount(2, wireMock());
verifyRequestCount(2);
}

@Test
public void firstAttemptTimeout_retryFinishWithInTime500_shouldNotTimeout() throws Exception {
stubFor(post(anyUrl())
.inScenario("timed out in the first attempt")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first attempt")
.willReturn(aResponse()
.withStatus(200).withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));

stubFor(post(anyUrl())
.inScenario("timed out in the first attempt")
.whenScenarioStateIs("first attempt")
.willSetStateTo("second attempt")
.willReturn(aResponse()
.withStatus(500)
.withFixedDelay(DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT)));
public void firstAttemptTimeout_retryFinishWithInTime500_shouldNotTimeout() {
mockHttpClient().stubResponses(Pair.of(mockResponse(200), DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT),
Pair.of(mockResponse(500), DELAY_BEFORE_API_CALL_ATTEMPT_TIMEOUT));
verifyRetraybleFailedResponseNotTimedOut();
verifyRequestCount(2);
}

@Test
public void allAttemtsNotFinishedWithinTime_shouldTimeout() {
stubFor(post(anyUrl())
.inScenario("timed out in both attempts")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first attempt")
.willReturn(aResponse()
.withStatus(200).withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));

stubFor(post(anyUrl())
.inScenario("timed out in both attempts")
.whenScenarioStateIs("first attempt")
.willSetStateTo("second attempt")
.willReturn(aResponse()
.withStatus(200)
.withBody("{}")
.withFixedDelay(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT)));
public void allAttemptsNotFinishedWithinTime_shouldTimeout() {
stubSuccessResponse(DELAY_AFTER_API_CALL_ATTEMPT_TIMEOUT);
verifyRetryableTimeout();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,110 +15,73 @@

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

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static software.amazon.awssdk.protocol.wiremock.WireMockUtils.verifyRequestCount;

import com.github.tomakehurst.wiremock.stubbing.Scenario;
import org.junit.Test;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import software.amazon.awssdk.utils.Pair;

/**
* Contains common scenarios to test timeout feature.
*/
public abstract class BaseApiCallTimeoutTest extends BaseTimeoutTest {

protected static final int TIMEOUT = 1000;
protected static final int DELAY_BEFORE_TIMEOUT = 100;
protected static final int DELAY_AFTER_TIMEOUT = 1200;
protected static final Duration TIMEOUT = Duration.ofMillis(300);

protected static final Duration DELAY_BEFORE_TIMEOUT = Duration.ofMillis(10);
protected static final Duration DELAY_AFTER_TIMEOUT = Duration.ofMillis(500);

@Test
public void nonstreamingOperation_finishedWithinTime_shouldNotTimeout() throws Exception {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_BEFORE_TIMEOUT)));
stubSuccessResponse(DELAY_BEFORE_TIMEOUT);
verifySuccessResponseNotTimedOut();
}

@Test
public void nonstreamingOperation_notFinishedWithinTime_shouldTimeout() {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_TIMEOUT)));
stubSuccessResponse(DELAY_AFTER_TIMEOUT);
verifyTimedOut();
}

@Test
public void nonstreamingOperation500_notFinishedWithinTime_shouldTimeout() {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(500).withFixedDelay(DELAY_AFTER_TIMEOUT)));
stubErrorResponse(DELAY_AFTER_TIMEOUT);
verifyTimedOut();
}

@Test
public void nonstreamingOperation500_finishedWithinTime_shouldNotTimeout() throws Exception {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(500).withFixedDelay(DELAY_BEFORE_TIMEOUT)));
stubErrorResponse(DELAY_BEFORE_TIMEOUT);
verifyFailedResponseNotTimedOut();
}

@Test
public void nonstreamingOperation_retrySucceeded_FinishedWithinTime_shouldNotTimeout() throws Exception {

stubFor(post(anyUrl())
.inScenario("retry at 500")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first attempt")
.willReturn(aResponse()
.withStatus(500).withFixedDelay(DELAY_BEFORE_TIMEOUT)));

stubFor(post(anyUrl())
.inScenario("retry at 500")
.whenScenarioStateIs("first attempt")
.willSetStateTo("second attempt")
.willReturn(aResponse()
.withStatus(200)
.withBody("{}").withFixedDelay(DELAY_BEFORE_TIMEOUT)));

assertThat(retryableCallable().call()).isNotNull();
public void streamingOperation_finishedWithinTime_shouldNotTimeout() throws Exception {
stubSuccessResponse(DELAY_BEFORE_TIMEOUT);
verifySuccessResponseNotTimedOut();
}

@Test
public void nonstreamingOperation_retryWouldSucceed_notFinishedWithinTime_shouldTimeout() {

stubFor(post(anyUrl())
.inScenario("retry at 500")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first attempt")
.willReturn(aResponse()
.withStatus(500).withFixedDelay(DELAY_BEFORE_TIMEOUT)));

stubFor(post(anyUrl())
.inScenario("retry at 500")
.whenScenarioStateIs("first attempt")
.willSetStateTo("second attempt")
.willReturn(aResponse()
.withStatus(200)
.withBody("{}").withFixedDelay(DELAY_AFTER_TIMEOUT)));


verifyRetryableTimeout();
verifyRequestCount(2, wireMock());
public void streamingOperation_notFinishedWithinTime_shouldTimeout() {
stubSuccessResponse(DELAY_AFTER_TIMEOUT);
verifyTimedOut();
}

@Test
public void streamingOperation_finishedWithinTime_shouldNotTimeout() throws Exception {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_BEFORE_TIMEOUT)));
public void nonstreamingOperation_retrySucceeded_FinishedWithinTime_shouldNotTimeout() throws Exception {
mockHttpClient().stubResponses(Pair.of(mockResponse(500), DELAY_BEFORE_TIMEOUT),
Pair.of(mockResponse(200), DELAY_BEFORE_TIMEOUT));

verifySuccessResponseNotTimedOut();
assertThat(retryableCallable().call()).isNotNull();
verifyRequestCount(2);
}

@Test
public void streamingOperation_notFinishedWithinTime_shouldTimeout() {
stubFor(post(anyUrl())
.willReturn(aResponse().withStatus(200).withBody("{}").withFixedDelay(DELAY_AFTER_TIMEOUT)));
public void nonstreamingOperation_retryWouldSucceed_notFinishedWithinTime_shouldTimeout() {
mockHttpClient().stubResponses(Pair.of(mockResponse(500), DELAY_BEFORE_TIMEOUT),
Pair.of(mockResponse(200), DELAY_AFTER_TIMEOUT));

verifyTimedOut();
verifyRetryableTimeout();
verifyRequestCount(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,19 @@

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

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.Callable;
import java.util.function.Consumer;
import org.assertj.core.api.ThrowableAssert;
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.http.AbortableInputStream;
import software.amazon.awssdk.http.HttpExecuteResponse;
import software.amazon.awssdk.http.SdkHttpResponse;
import software.amazon.awssdk.testutils.service.http.MockHttpClient;

public abstract class BaseTimeoutTest {

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

protected abstract Callable streamingCallable();

protected abstract WireMockRule wireMock();
protected abstract void stubSuccessResponse(Duration delayAfterTimeout);

protected abstract void stubErrorResponse(Duration delayAfterTimeout);

protected void verifySuccessResponseNotTimedOut() throws Exception {
assertThat(callable().call()).isNotNull();
Expand Down Expand Up @@ -129,4 +134,18 @@ public Object transform(Object response, AbortableInputStream inputStream) throw
public static void wastingTimeInterruptibly() throws InterruptedException {
Thread.sleep(1200);
}

public abstract MockHttpClient mockHttpClient();

public static HttpExecuteResponse mockResponse(int statusCode) {
return HttpExecuteResponse.builder()
.response(SdkHttpResponse.builder()
.statusCode(statusCode)
.build())
.build();
}

public void verifyRequestCount(int requestCount) {
assertThat(mockHttpClient().getRequests().size()).isEqualTo(requestCount);
}
}
Loading