Skip to content

Commit bf16fd5

Browse files
committed
Add test cases for UrlConnectionHttpClient
1 parent e2f16bf commit bf16fd5

File tree

2 files changed

+76
-20
lines changed

2 files changed

+76
-20
lines changed

test/protocol-tests/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@
194194
<version>${awsjavasdk.version}</version>
195195
<scope>test</scope>
196196
</dependency>
197+
<dependency>
198+
<groupId>software.amazon.awssdk</groupId>
199+
<artifactId>url-connection-client</artifactId>
200+
<version>${awsjavasdk.version}</version>
201+
<scope>test</scope>
202+
</dependency>
197203
</dependencies>
198204

199205
<build>

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

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,20 @@
3030
import java.util.concurrent.Executors;
3131
import java.util.concurrent.Future;
3232
import java.util.concurrent.atomic.AtomicLong;
33+
import java.util.stream.Stream;
3334
import org.junit.jupiter.api.BeforeEach;
3435
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.params.ParameterizedTest;
37+
import org.junit.jupiter.params.provider.Arguments;
38+
import org.junit.jupiter.params.provider.MethodSource;
3539
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
3640
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
41+
import software.amazon.awssdk.core.exception.AbortedException;
3742
import software.amazon.awssdk.core.exception.ApiCallAttemptTimeoutException;
3843
import software.amazon.awssdk.core.retry.RetryPolicy;
3944
import software.amazon.awssdk.http.SdkHttpClient;
4045
import software.amazon.awssdk.http.apache.ApacheHttpClient;
46+
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
4147
import software.amazon.awssdk.metrics.MetricCollection;
4248
import software.amazon.awssdk.metrics.MetricPublisher;
4349
import software.amazon.awssdk.metrics.MetricRecord;
@@ -53,6 +59,7 @@ class SyncClientConnectionInterruptionTest {
5359
+ "\":\"resultString\"}";
5460
private final WireMockServer mockServer = new WireMockServer(new WireMockConfiguration()
5561
.bindAddress("localhost")
62+
.dynamicPort());
5663
@BeforeEach
5764
public void setup() {
5865
mockServer.start();
@@ -109,51 +116,68 @@ void connectionPoolsGetsReusedWhenInterruptedWith_Multiple_MaxConnection() throw
109116
@Test
110117
void interruptionWhenWaitingForLease_AbortsImmediately() throws InterruptedException {
111118
Integer LONG_DELAY = 5000;
119+
ExceptionInThreadRun exceptionInThreadRun = new ExceptionInThreadRun();
112120
AtomicLong leaseWaitingTime = new AtomicLong(LONG_DELAY);
113121
stubPostRequest("/2016-03-11/allTypes", aResponse().withFixedDelay(LONG_DELAY), SAMPLE_BODY);
114122
SdkHttpClient httpClient = ApacheHttpClient.builder().maxConnections(1).build();
115123
ProtocolRestJsonClient client = getClient(httpClient, Duration.ofMillis(2L * LONG_DELAY)).build();
116124
ExecutorService executorService = Executors.newFixedThreadPool(5);
117125
executorService.submit(() -> client.allTypes());
118126
unInterruptedSleep(100);
119-
Thread leaseWaitingThread = new Thread(() ->
120-
client.allTypes(l -> l.overrideConfiguration(
121-
b -> b
122-
.apiCallAttemptTimeout(Duration.ofSeconds(10))
123-
.addMetricPublisher(new MetricPublisher() {
124-
@Override
125-
public void publish(MetricCollection metricCollection) {
126-
System.out.println(metricCollection);
127-
Optional<MetricRecord<?>> apiCallDuration =
128-
metricCollection.stream().filter(o -> "ApiCallDuration" .equals(o.metric().name())).findAny();
129-
leaseWaitingTime.set(Duration.parse(apiCallDuration.get().value().toString()).toMillis());
130-
}
131-
@Override
132-
public void close() {
133-
}
134-
})
135-
)));
127+
Thread leaseWaitingThread = new Thread(() -> {
128+
129+
try {
130+
client.allTypes(l -> l.overrideConfiguration(
131+
b -> b
132+
.apiCallAttemptTimeout(Duration.ofSeconds(10))
133+
.addMetricPublisher(new MetricPublisher() {
134+
@Override
135+
public void publish(MetricCollection metricCollection) {
136+
System.out.println(metricCollection);
137+
Optional<MetricRecord<?>> apiCallDuration =
138+
metricCollection.stream().filter(o -> "ApiCallDuration" .equals(o.metric().name())).findAny();
139+
leaseWaitingTime.set(Duration.parse(apiCallDuration.get().value().toString()).toMillis());
140+
}
141+
142+
@Override
143+
public void close() {
144+
}
145+
})
146+
));
147+
148+
} catch (Exception exception) {
149+
exceptionInThreadRun.setException(exception);
150+
151+
}
152+
});
153+
136154
leaseWaitingThread.start();
137155
unInterruptedSleep(100);
138156
leaseWaitingThread.interrupt();
139157
leaseWaitingThread.join();
140158
assertThat(leaseWaitingTime.get()).isNotEqualTo(LONG_DELAY.longValue());
141159
assertThat(leaseWaitingTime.get()).isLessThan(LONG_DELAY.longValue());
160+
assertThat(exceptionInThreadRun.getException()).isInstanceOf(AbortedException.class);
142161
client.close();
143162
}
144163

164+
private static Stream<Arguments> httpClientImplementation() {
165+
return Stream.of(Arguments.of(ApacheHttpClient.create()),
166+
Arguments.of(UrlConnectionHttpClient.create()));
167+
}
168+
145169
/**
146170
* Service Latency is set to high value say X.
147171
* Api timeout value id set to 1/3 of X.
148172
* And we interrupt the thread at 90% of X.
149173
* In this case since the ApiTimeOut first happened we should get ApiTimeOut Exception and not the interrupt.
150174
*/
151-
@Test
152-
void interruptionDueToApiTimeOut_followed_byInterruptCausesOnlyTimeOutException() throws InterruptedException {
175+
@ParameterizedTest
176+
@MethodSource("httpClientImplementation")
177+
void interruptionDueToApiTimeOut_followed_byInterruptCausesOnlyTimeOutException(SdkHttpClient httpClient) throws InterruptedException {
153178
Integer SERVER_RESPONSE_DELAY = 3000;
154179
stubPostRequest("/2016-03-11/allTypes", aResponse().withFixedDelay(SERVER_RESPONSE_DELAY), SAMPLE_BODY);
155180
ExceptionInThreadRun exception = new ExceptionInThreadRun();
156-
SdkHttpClient httpClient = ApacheHttpClient.builder().build();
157181
ProtocolRestJsonClient client =
158182
getClient(httpClient, Duration.ofMillis(10)).overrideConfiguration(o -> o.retryPolicy(RetryPolicy.none())).build();
159183
unInterruptedSleep(100);
@@ -172,6 +196,32 @@ void interruptionDueToApiTimeOut_followed_byInterruptCausesOnlyTimeOutException(
172196
assertThat(exception.getException()).isInstanceOf(ApiCallAttemptTimeoutException.class);
173197
client.close();
174198
}
199+
200+
@ParameterizedTest
201+
@MethodSource("httpClientImplementation")
202+
void sdkClientInterrupted_while_connectionIsInProgress(SdkHttpClient httpClient) throws InterruptedException {
203+
Integer SERVER_RESPONSE_DELAY = 3000;
204+
stubPostRequest("/2016-03-11/allTypes", aResponse().withFixedDelay(SERVER_RESPONSE_DELAY), SAMPLE_BODY);
205+
ExceptionInThreadRun exception = new ExceptionInThreadRun();
206+
ProtocolRestJsonClient client =
207+
getClient(httpClient, Duration.ofMillis(10)).overrideConfiguration(o -> o.retryPolicy(RetryPolicy.none())).build();
208+
unInterruptedSleep(100);
209+
// We need to creat a separate thread to interrupt it externally.
210+
Thread leaseWaitingThread = new Thread(() -> {
211+
try {
212+
client.allTypes(l -> l.overrideConfiguration(b -> b.apiCallAttemptTimeout(Duration.ofMillis(SERVER_RESPONSE_DELAY * 3))));
213+
} catch (Exception e) {
214+
exception.setException(e);
215+
}
216+
});
217+
leaseWaitingThread.start();
218+
unInterruptedSleep(SERVER_RESPONSE_DELAY - SERVER_RESPONSE_DELAY / 10);
219+
leaseWaitingThread.interrupt();
220+
leaseWaitingThread.join();
221+
assertThat(exception.getException()).isInstanceOf(AbortedException.class);
222+
client.close();
223+
}
224+
175225
private class ExceptionInThreadRun {
176226
private Exception exception;
177227
public Exception getException() {

0 commit comments

Comments
 (0)