|
| 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 | +package software.amazon.awssdk.http.urlconnection; |
| 16 | + |
| 17 | +import static org.assertj.core.api.Assertions.assertThat; |
| 18 | +import static software.amazon.awssdk.core.internal.util.ResponseHandlerTestUtils.combinedSyncResponseHandler; |
| 19 | +import static utils.HttpTestUtils.executionContext; |
| 20 | +import static utils.HttpTestUtils.testClientConfiguration; |
| 21 | + |
| 22 | +import java.io.ByteArrayInputStream; |
| 23 | +import java.io.ByteArrayOutputStream; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.InputStream; |
| 26 | +import java.io.OutputStream; |
| 27 | +import java.net.HttpURLConnection; |
| 28 | +import java.net.URI; |
| 29 | +import java.net.URL; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.concurrent.atomic.AtomicInteger; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | +import software.amazon.awssdk.core.client.config.SdkClientOption; |
| 36 | +import software.amazon.awssdk.core.http.NoopTestRequest; |
| 37 | +import software.amazon.awssdk.core.internal.http.AmazonSyncHttpClient; |
| 38 | +import software.amazon.awssdk.core.internal.http.response.NullErrorResponseHandler; |
| 39 | +import software.amazon.awssdk.core.retry.RetryPolicy; |
| 40 | +import software.amazon.awssdk.core.retry.backoff.BackoffStrategy; |
| 41 | +import software.amazon.awssdk.http.SdkHttpClient; |
| 42 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 43 | +import software.amazon.awssdk.http.SdkHttpMethod; |
| 44 | + |
| 45 | +public class UrlConnectionHttpClientRetryTest { |
| 46 | + @Test |
| 47 | + void execute_whenOutputStreamThrowsBareNpe_retriesRequest() { |
| 48 | + verifyOutputStreamFailureIsRetried(() -> { |
| 49 | + throw new NullPointerException("this.http is null"); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + void execute_whenOutputStreamThrowsWrappedNpe_retriesRequest() { |
| 55 | + verifyOutputStreamFailureIsRetried(() -> { |
| 56 | + throw new RuntimeException(new NullPointerException("this.http is null")); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void execute_whenOutputStreamThrowsIOException_retriesRequest() { |
| 62 | + verifyOutputStreamFailureIsRetried(() -> { |
| 63 | + throw new IOException("connection closed"); |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void execute_whenInputStreamThrowsBareNpe_retriesRequest() { |
| 69 | + AtomicInteger attempts = new AtomicInteger(); |
| 70 | + SdkHttpClient transport = UrlConnectionHttpClient.create(uri -> new StubHttpURLConnection(toUrl(uri)) { |
| 71 | + @Override |
| 72 | + public InputStream getInputStream() { |
| 73 | + if (attempts.incrementAndGet() == 1) { |
| 74 | + throw new NullPointerException("this.http is null"); |
| 75 | + } |
| 76 | + return new ByteArrayInputStream(new byte[0]); |
| 77 | + } |
| 78 | + |
| 79 | + // Ensure responseHasNoContent() proceeds to getInputStream(). |
| 80 | + @Override |
| 81 | + public String getHeaderField(String name) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + SdkHttpFullRequest request = SdkHttpFullRequest.builder() |
| 87 | + .uri(URI.create("http://localhost/test")) |
| 88 | + .method(SdkHttpMethod.GET) |
| 89 | + .build(); |
| 90 | + verifyFailureIsRetried(transport, request, attempts); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void execute_whenResponseCodeCheckBeforeInputStreamThrowsIOException_retriesRequest() { |
| 95 | + AtomicInteger attempts = new AtomicInteger(); |
| 96 | + AtomicInteger responseCodeCalls = new AtomicInteger(); |
| 97 | + SdkHttpClient transport = UrlConnectionHttpClient.create(uri -> { |
| 98 | + attempts.incrementAndGet(); |
| 99 | + return new StubHttpURLConnection(toUrl(uri)) { |
| 100 | + @Override |
| 101 | + public int getResponseCode() throws IOException { |
| 102 | + if (responseCodeCalls.incrementAndGet() == 2) { |
| 103 | + throw new IOException("connection closed"); |
| 104 | + } |
| 105 | + return HTTP_OK; |
| 106 | + } |
| 107 | + }; |
| 108 | + }); |
| 109 | + |
| 110 | + SdkHttpFullRequest request = SdkHttpFullRequest.builder() |
| 111 | + .uri(URI.create("http://localhost/test")) |
| 112 | + .method(SdkHttpMethod.GET) |
| 113 | + .build(); |
| 114 | + verifyFailureIsRetried(transport, request, attempts); |
| 115 | + } |
| 116 | + |
| 117 | + private void verifyOutputStreamFailureIsRetried(IoRunnable firstAttemptFailure) { |
| 118 | + AtomicInteger attempts = new AtomicInteger(); |
| 119 | + SdkHttpClient transport = UrlConnectionHttpClient.create(uri -> new StubHttpURLConnection(toUrl(uri)) { |
| 120 | + @Override |
| 121 | + public OutputStream getOutputStream() throws IOException { |
| 122 | + if (attempts.incrementAndGet() == 1) { |
| 123 | + firstAttemptFailure.run(); |
| 124 | + } |
| 125 | + return super.getOutputStream(); |
| 126 | + } |
| 127 | + }); |
| 128 | + |
| 129 | + SdkHttpFullRequest request = SdkHttpFullRequest.builder() |
| 130 | + .uri(URI.create("http://localhost/test")) |
| 131 | + .method(SdkHttpMethod.PUT) |
| 132 | + .putHeader("Content-Length", "1") |
| 133 | + .contentStreamProvider(() -> new ByteArrayInputStream(new byte[1])) |
| 134 | + .build(); |
| 135 | + verifyFailureIsRetried(transport, request, attempts); |
| 136 | + } |
| 137 | + |
| 138 | + private void verifyFailureIsRetried(SdkHttpClient transport, |
| 139 | + SdkHttpFullRequest request, |
| 140 | + AtomicInteger attempts) { |
| 141 | + RetryPolicy retryPolicy = RetryPolicy.builder() |
| 142 | + .numRetries(1) |
| 143 | + .backoffStrategy(BackoffStrategy.none()) |
| 144 | + .throttlingBackoffStrategy(BackoffStrategy.none()) |
| 145 | + .build(); |
| 146 | + AmazonSyncHttpClient client = new AmazonSyncHttpClient( |
| 147 | + testClientConfiguration().toBuilder() |
| 148 | + .option(SdkClientOption.SYNC_HTTP_CLIENT, transport) |
| 149 | + .option(SdkClientOption.RETRY_POLICY, retryPolicy) |
| 150 | + .build()); |
| 151 | + try { |
| 152 | + client.requestExecutionBuilder() |
| 153 | + .request(request) |
| 154 | + .originalRequest(NoopTestRequest.builder().build()) |
| 155 | + .executionContext(executionContext(request)) |
| 156 | + .execute(combinedSyncResponseHandler(null, new NullErrorResponseHandler())); |
| 157 | + } finally { |
| 158 | + client.close(); |
| 159 | + } |
| 160 | + |
| 161 | + assertThat(attempts.get()).isEqualTo(2); |
| 162 | + } |
| 163 | + |
| 164 | + private static URL toUrl(URI uri) { |
| 165 | + try { |
| 166 | + return uri.toURL(); |
| 167 | + } catch (IOException e) { |
| 168 | + throw new IllegalArgumentException(e); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + @FunctionalInterface |
| 173 | + private interface IoRunnable { |
| 174 | + void run() throws IOException; |
| 175 | + } |
| 176 | + |
| 177 | + private static class StubHttpURLConnection extends HttpURLConnection { |
| 178 | + private StubHttpURLConnection(URL url) { |
| 179 | + super(url); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void connect() { |
| 184 | + connected = true; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void disconnect() { |
| 189 | + connected = false; |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public boolean usingProxy() { |
| 194 | + return false; |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public OutputStream getOutputStream() throws IOException { |
| 199 | + return new ByteArrayOutputStream(); |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public InputStream getInputStream() { |
| 204 | + return null; |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public int getResponseCode() throws IOException { |
| 209 | + return HTTP_OK; |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public String getResponseMessage() { |
| 214 | + return "OK"; |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public String getHeaderField(String name) { |
| 219 | + return "Content-Length".equals(name) ? "0" : null; |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public Map<String, List<String>> getHeaderFields() { |
| 224 | + return Collections.emptyMap(); |
| 225 | + } |
| 226 | + } |
| 227 | +} |
0 commit comments