Skip to content

Commit d60231e

Browse files
committed
Add retry attempts for creating wiremock server
This attempts to guard against multiple subclasses of SdkHttpClientTestSuite running parallel attempting to bind to the same port on startup.
1 parent cd72a77 commit d60231e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

test/http-client-tests/src/main/java/software/amazon/awssdk/http/SdkHttpClientTestSuite.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.github.tomakehurst.wiremock.WireMockServer;
2929
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
30+
import com.github.tomakehurst.wiremock.common.FatalStartupException;
3031
import com.github.tomakehurst.wiremock.http.RequestMethod;
3132
import com.github.tomakehurst.wiremock.junit.WireMockRule;
3233
import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder;
@@ -35,6 +36,7 @@
3536
import java.net.HttpURLConnection;
3637
import java.net.URI;
3738
import java.nio.charset.StandardCharsets;
39+
import java.util.Random;
3840
import javax.net.ssl.SSLHandshakeException;
3941
import javax.net.ssl.TrustManager;
4042
import javax.net.ssl.TrustManagerFactory;
@@ -43,6 +45,7 @@
4345
import org.junit.runner.RunWith;
4446
import org.mockito.runners.MockitoJUnitRunner;
4547
import software.amazon.awssdk.utils.IoUtils;
48+
import software.amazon.awssdk.utils.Logger;
4649

4750
/**
4851
* A set of tests validating that the functionality implemented by a {@link SdkHttpClient}.
@@ -52,8 +55,12 @@
5255
*/
5356
@RunWith(MockitoJUnitRunner.class)
5457
public abstract class SdkHttpClientTestSuite {
58+
private static final Logger LOG = Logger.loggerFor(SdkHttpClientTestSuite.class);
59+
5560
@Rule
56-
public WireMockRule mockServer = new WireMockRule(wireMockConfig().dynamicPort().dynamicHttpsPort());
61+
public WireMockRule mockServer = createWireMockRule();
62+
63+
private final Random rng = new Random();
5764

5865
@Test
5966
public void supportsResponseCode200() throws Exception {
@@ -267,4 +274,28 @@ public void trustAll(boolean trustAll) {
267274
this.trustAll = trustAll;
268275
}
269276
}
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+
}
270301
}

0 commit comments

Comments
 (0)