|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.crt; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 19 | +import static software.amazon.awssdk.http.crt.CrtHttpClientTestUtils.createRequest; |
| 20 | + |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.net.ServerSocket; |
| 24 | +import java.net.Socket; |
| 25 | +import java.net.URI; |
| 26 | +import java.time.Duration; |
| 27 | +import java.util.concurrent.CompletionException; |
| 28 | +import java.util.concurrent.ExecutionException; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import java.util.concurrent.TimeoutException; |
| 31 | +import org.junit.jupiter.api.AfterEach; |
| 32 | +import org.junit.jupiter.api.BeforeEach; |
| 33 | +import org.junit.jupiter.api.Test; |
| 34 | +import software.amazon.awssdk.crt.Log; |
| 35 | +import software.amazon.awssdk.http.ExecutableHttpRequest; |
| 36 | +import software.amazon.awssdk.http.HttpExecuteRequest; |
| 37 | +import software.amazon.awssdk.http.RecordingResponseHandler; |
| 38 | +import software.amazon.awssdk.http.SdkHttpConfigurationOption; |
| 39 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 40 | +import software.amazon.awssdk.http.async.AsyncExecuteRequest; |
| 41 | +import software.amazon.awssdk.http.async.SdkAsyncHttpClient; |
| 42 | +import software.amazon.awssdk.http.SdkHttpClient; |
| 43 | +import software.amazon.awssdk.utils.AttributeMap; |
| 44 | + |
| 45 | +/** |
| 46 | + * Functional tests verifying that the default connection health configuration |
| 47 | + * (applied when no explicit {@link ConnectionHealthConfiguration} is set) |
| 48 | + * correctly terminates connections to non-responding servers. |
| 49 | + */ |
| 50 | +class NonResponsiveServerTest { |
| 51 | + |
| 52 | + private static final Duration SHORT_TIMEOUT = Duration.ofSeconds(2); |
| 53 | + private static final AttributeMap SHORT_TIMEOUTS = AttributeMap.builder() |
| 54 | + .put(SdkHttpConfigurationOption.READ_TIMEOUT, SHORT_TIMEOUT) |
| 55 | + .put(SdkHttpConfigurationOption.WRITE_TIMEOUT, SHORT_TIMEOUT) |
| 56 | + .build(); |
| 57 | + |
| 58 | + private ServerSocket serverSocket; |
| 59 | + |
| 60 | + @BeforeEach |
| 61 | + void setUp() throws IOException { |
| 62 | + Log.initLoggingToStdout(Log.LogLevel.Warn); |
| 63 | + serverSocket = new ServerSocket(0); |
| 64 | + // Accept connections in a daemon thread but never respond |
| 65 | + Thread acceptThread = new Thread(() -> { |
| 66 | + while (!serverSocket.isClosed()) { |
| 67 | + try { |
| 68 | + Socket socket = serverSocket.accept(); |
| 69 | + // Hold the connection open, never send a response |
| 70 | + Thread.sleep(Long.MAX_VALUE); |
| 71 | + } catch (Exception e) { |
| 72 | + // Server shutting down |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + acceptThread.setDaemon(true); |
| 77 | + acceptThread.start(); |
| 78 | + } |
| 79 | + |
| 80 | + @AfterEach |
| 81 | + void tearDown() throws IOException { |
| 82 | + if (serverSocket != null && !serverSocket.isClosed()) { |
| 83 | + serverSocket.close(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void syncClient_noExplicitHealthConfig_serverNeverResponds_shouldThrow() { |
| 89 | + try (SdkHttpClient client = AwsCrtHttpClient.builder().buildWithDefaults(SHORT_TIMEOUTS)) { |
| 90 | + URI uri = URI.create("http://localhost:" + serverSocket.getLocalPort()); |
| 91 | + SdkHttpRequest request = createRequest(uri); |
| 92 | + ExecutableHttpRequest executableRequest = client.prepareRequest( |
| 93 | + HttpExecuteRequest.builder().request(request) |
| 94 | + .contentStreamProvider(() -> new ByteArrayInputStream(new byte[0])) |
| 95 | + .build()); |
| 96 | + assertThatThrownBy(executableRequest::call).isInstanceOf(IOException.class) |
| 97 | + .hasMessageContaining("failure to meet throughput minimum"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void asyncClient_noExplicitHealthConfig_serverNeverResponds_shouldCompleteExceptionally() |
| 103 | + throws InterruptedException, TimeoutException { |
| 104 | + try (SdkAsyncHttpClient client = AwsCrtAsyncHttpClient.builder().buildWithDefaults(SHORT_TIMEOUTS)) { |
| 105 | + URI uri = URI.create("http://localhost:" + serverSocket.getLocalPort()); |
| 106 | + SdkHttpRequest request = createRequest(uri); |
| 107 | + RecordingResponseHandler recorder = new RecordingResponseHandler(); |
| 108 | + |
| 109 | + client.execute(AsyncExecuteRequest.builder() |
| 110 | + .request(request) |
| 111 | + .requestContentPublisher(new EmptyPublisher()) |
| 112 | + .responseHandler(recorder) |
| 113 | + .build()); |
| 114 | + |
| 115 | + assertThatThrownBy(() -> recorder.completeFuture().get(10, TimeUnit.SECONDS)) |
| 116 | + .hasCauseInstanceOf(IOException.class) |
| 117 | + .hasMessageContaining("failure to meet throughput minimum"); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments