diff --git a/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClientSpiVerificationTest.java b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClientSpiVerificationTest.java index c2bf597814af..45de4ba201f0 100644 --- a/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClientSpiVerificationTest.java +++ b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtAsyncHttpClientSpiVerificationTest.java @@ -52,6 +52,7 @@ import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import software.amazon.awssdk.crt.CrtResource; +import software.amazon.awssdk.crt.Log; import software.amazon.awssdk.crt.http.HttpException; import software.amazon.awssdk.http.RecordingResponseHandler; import software.amazon.awssdk.http.SdkHttpMethod; @@ -73,6 +74,8 @@ public class AwsCrtAsyncHttpClientSpiVerificationTest { @BeforeClass public static void setup() throws Exception { + System.setProperty("aws.crt.debugnative", "true"); + Log.initLoggingToStdout(Log.LogLevel.Warn); client = AwsCrtAsyncHttpClient.builder() .connectionHealthConfiguration(b -> b.minimumThroughputInBps(4068L) .minimumThroughputTimeout(Duration.ofSeconds(3))) diff --git a/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtHttpClientSpiVerificationTest.java b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtHttpClientSpiVerificationTest.java index 6e596b5d2bc5..0777525c83ca 100644 --- a/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtHttpClientSpiVerificationTest.java +++ b/http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/AwsCrtHttpClientSpiVerificationTest.java @@ -43,6 +43,7 @@ import org.junit.Rule; import org.junit.Test; import software.amazon.awssdk.crt.CrtResource; +import software.amazon.awssdk.crt.Log; import software.amazon.awssdk.crt.http.HttpException; import software.amazon.awssdk.http.ExecutableHttpRequest; import software.amazon.awssdk.http.HttpExecuteRequest; @@ -63,6 +64,8 @@ public class AwsCrtHttpClientSpiVerificationTest { @BeforeClass public static void setup() throws Exception { + System.setProperty("aws.crt.debugnative", "true"); + Log.initLoggingToStdout(Log.LogLevel.Warn); client = AwsCrtHttpClient.builder() .connectionHealthConfiguration(b -> b.minimumThroughputInBps(4068L) .minimumThroughputTimeout(Duration.ofSeconds(3))) diff --git a/test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java b/test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java index 53c15cd27be4..540c38129d36 100644 --- a/test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java +++ b/test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java @@ -109,12 +109,12 @@ public void supportsResponseCode500() throws Exception { @Test public void validatesHttpsCertificateIssuer() { - SdkHttpClient client = createSdkHttpClient(); + try (SdkHttpClient client = createSdkHttpClient()) { + SdkHttpFullRequest request = mockSdkRequest("https://localhost:" + mockServer.httpsPort(), SdkHttpMethod.POST); - SdkHttpFullRequest request = mockSdkRequest("https://localhost:" + mockServer.httpsPort(), SdkHttpMethod.POST); - - assertThatThrownBy(client.prepareRequest(HttpExecuteRequest.builder().request(request).build())::call) + assertThatThrownBy(client.prepareRequest(HttpExecuteRequest.builder().request(request).build())::call) .isInstanceOf(SSLHandshakeException.class); + } } @Test @@ -123,27 +123,26 @@ public void connectionPoolingWorks() throws Exception { SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions(); httpClientOptions.trustAll(true); - SdkHttpClient client = createSdkHttpClient(httpClientOptions); - - stubForMockRequest(200); - - for (int i = 0; i < 5; i++) { - SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), SdkHttpMethod.POST); - HttpExecuteResponse response = - client.prepareRequest(HttpExecuteRequest.builder() - .request(req) - .contentStreamProvider(req.contentStreamProvider().orElse(null)) - .build()) - .call(); - response.responseBody().ifPresent(IoUtils::drainInputStream); - } - - // connection pool growth strategies vary across client implementations. Some, such as the CRT grow connection counts - // by a factor of 2, while some grow strictly as requested. Mainly we want to test that it kicks in at some point and - // doesn't create a new connection for all 5 requests. This proves that while allowing variance in this behavior. - assertThat(CONNECTION_COUNTER.openedConnections()).isGreaterThanOrEqualTo(initialOpenedConnections + 1); - assertThat(CONNECTION_COUNTER.openedConnections()).isLessThanOrEqualTo(initialOpenedConnections + 2); + try (SdkHttpClient client = createSdkHttpClient(httpClientOptions)) { + stubForMockRequest(200); + + for (int i = 0; i < 5; i++) { + SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), SdkHttpMethod.POST); + HttpExecuteResponse response = + client.prepareRequest(HttpExecuteRequest.builder() + .request(req) + .contentStreamProvider(req.contentStreamProvider().orElse(null)) + .build()) + .call(); + response.responseBody().ifPresent(IoUtils::drainInputStream); + } + // connection pool growth strategies vary across client implementations. Some, such as the CRT grow connection counts + // by a factor of 2, while some grow strictly as requested. Mainly we want to test that it kicks in at some point and + // doesn't create a new connection for all 5 requests. This proves that while allowing variance in this behavior. + assertThat(CONNECTION_COUNTER.openedConnections()).isGreaterThanOrEqualTo(initialOpenedConnections + 1); + assertThat(CONNECTION_COUNTER.openedConnections()).isLessThanOrEqualTo(initialOpenedConnections + 2); + } } @Test @@ -152,25 +151,26 @@ public void connectionsAreNotReusedOn5xxErrors() throws Exception { SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions(); httpClientOptions.trustAll(true); - SdkHttpClient client = createSdkHttpClient(httpClientOptions); - - stubForMockRequest(503); - - for (int i = 0; i < 5; i++) { - SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), SdkHttpMethod.POST); - HttpExecuteResponse response = - client.prepareRequest(HttpExecuteRequest.builder() - .request(req) - .contentStreamProvider(req.contentStreamProvider().orElse(null)) - .build()) - .call(); - response.responseBody().ifPresent(IoUtils::drainInputStream); - } + try (SdkHttpClient client = createSdkHttpClient(httpClientOptions)) { + stubForMockRequest(503); + + for (int i = 0; i < 5; i++) { + SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), SdkHttpMethod.POST); + HttpExecuteResponse response = + client.prepareRequest(HttpExecuteRequest.builder() + .request(req) + .contentStreamProvider(req.contentStreamProvider().orElse(null)) + .build()) + .call(); + response.responseBody().ifPresent(IoUtils::drainInputStream); + } - // don't couple this test to connection manager behaviors we don't have to. We want to make sure that the connection count - // increased by at least as many connections as we got 5xx errors back on. But the connection manager also predictively - // creates connections and we need to take those into account in a way that lets it remain a dynamic behavior. - assertThat(CONNECTION_COUNTER.openedConnections()).isGreaterThanOrEqualTo(initialOpenedConnections + 5); + // don't couple this test to connection manager behaviors we don't have to. We want to make sure that the + // connection count increased by at least as many connections as we got 5xx errors back on. But the connection + // manager also predictively creates connections and we need to take those into account in a way that lets it + // remain a dynamic behavior. + assertThat(CONNECTION_COUNTER.openedConnections()).isGreaterThanOrEqualTo(initialOpenedConnections + 5); + } } @Test @@ -185,8 +185,7 @@ public void testCustomTlsTrustManager() throws Exception { selfSignedServer.start(); - try { - SdkHttpClient client = createSdkHttpClient(httpClientOptions); + try (SdkHttpClient client = createSdkHttpClient(httpClientOptions)) { SdkHttpFullRequest request = mockSdkRequest("https://localhost:" + selfSignedServer.httpsPort(), SdkHttpMethod.POST); client.prepareRequest(HttpExecuteRequest.builder() @@ -204,7 +203,10 @@ public void testTrustAllWorks() throws Exception { SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions(); httpClientOptions.trustAll(true); - testForResponseCodeUsingHttps(createSdkHttpClient(httpClientOptions), HttpURLConnection.HTTP_OK); + try (SdkHttpClient client = createSdkHttpClient(httpClientOptions)) { + testForResponseCodeUsingHttps(client, HttpURLConnection.HTTP_OK); + } + } @Test @@ -221,19 +223,19 @@ protected void testForResponseCode(int returnCode) throws Exception { } private void testForResponseCode(int returnCode, SdkHttpMethod method) throws Exception { - SdkHttpClient client = createSdkHttpClient(); - - stubForMockRequest(returnCode); - - SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), method); - HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder() - .request(req) - .contentStreamProvider(req.contentStreamProvider() - .orElse(null)) - .build()) - .call(); - - validateResponse(rsp, returnCode, method); + try (SdkHttpClient client = createSdkHttpClient()) { + stubForMockRequest(returnCode); + + SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), method); + HttpExecuteResponse rsp = client.prepareRequest(HttpExecuteRequest.builder() + .request(req) + .contentStreamProvider(req.contentStreamProvider() + .orElse(null)) + .build()) + .call(); + + validateResponse(rsp, returnCode, method); + } } protected void testForResponseCodeUsingHttps(SdkHttpClient client, int returnCode) throws Exception {