|
27 | 27 |
|
28 | 28 | import com.github.tomakehurst.wiremock.WireMockServer;
|
29 | 29 | import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
|
| 30 | +import com.github.tomakehurst.wiremock.common.FatalStartupException; |
30 | 31 | import com.github.tomakehurst.wiremock.http.RequestMethod;
|
31 | 32 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
32 | 33 | import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
|
|
35 | 36 | import java.net.HttpURLConnection;
|
36 | 37 | import java.net.URI;
|
37 | 38 | import java.nio.charset.StandardCharsets;
|
| 39 | +import java.util.Random; |
38 | 40 | import javax.net.ssl.SSLHandshakeException;
|
39 | 41 | import javax.net.ssl.TrustManager;
|
40 | 42 | import javax.net.ssl.TrustManagerFactory;
|
|
43 | 45 | import org.junit.runner.RunWith;
|
44 | 46 | import org.mockito.runners.MockitoJUnitRunner;
|
45 | 47 | import software.amazon.awssdk.utils.IoUtils;
|
| 48 | +import software.amazon.awssdk.utils.Logger; |
46 | 49 |
|
47 | 50 | /**
|
48 | 51 | * A set of tests validating that the functionality implemented by a {@link SdkHttpClient}.
|
|
52 | 55 | */
|
53 | 56 | @RunWith(MockitoJUnitRunner.class)
|
54 | 57 | public abstract class SdkHttpClientTestSuite {
|
| 58 | + private static final Logger LOG = Logger.loggerFor(SdkHttpClientTestSuite.class); |
| 59 | + |
55 | 60 | @Rule
|
56 |
| - public WireMockRule mockServer = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort()); |
| 61 | + public WireMockRule mockServer = createWireMockRule(); |
| 62 | + |
| 63 | + private final Random rng = new Random(); |
57 | 64 |
|
58 | 65 | @Test
|
59 | 66 | public void supportsResponseCode200() throws Exception {
|
@@ -267,4 +274,28 @@ public void trustAll(boolean trustAll) {
|
267 | 274 | this.trustAll = trustAll;
|
268 | 275 | }
|
269 | 276 | }
|
| 277 | + |
| 278 | + private WireMockRule createWireMockRule() { |
| 279 | + int maxAttempts = 5; |
| 280 | + for (int i = 0; i < maxAttempts; ++i) { |
| 281 | + try { |
| 282 | + return new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort()); |
| 283 | + } catch (FatalStartupException e) { |
| 284 | + int attemptNum = i + 1; |
| 285 | + LOG.debug(() -> "Was not able to start WireMock server. Attempt " + attemptNum, e); |
| 286 | + |
| 287 | + if (attemptNum != maxAttempts) { |
| 288 | + try { |
| 289 | + long sleepMillis = 1_000L + rng.nextInt(1_000); |
| 290 | + Thread.sleep(sleepMillis); |
| 291 | + } catch (InterruptedException ie) { |
| 292 | + Thread.currentThread().interrupt(); |
| 293 | + throw new RuntimeException("Backoff interrupted", ie); |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + } |
| 298 | + |
| 299 | + throw new RuntimeException("Unable to setup WireMock rule"); |
| 300 | + } |
270 | 301 | }
|
0 commit comments