Skip to content

Commit f755c3f

Browse files
authored
fix(test): update AlpnHttpTest to use available port instead of fixed port 8080 that can be occupied by parallel tests (#6824)
1 parent 7d1830b commit f755c3f

File tree

1 file changed

+15
-1
lines changed
  • test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/h2

1 file changed

+15
-1
lines changed

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/h2/AlpnHttpTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import static org.assertj.core.api.Assertions.assertThat;
1919
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

21+
import java.io.IOException;
22+
import java.io.UncheckedIOException;
2123
import java.lang.invoke.MethodHandle;
2224
import java.lang.invoke.MethodHandles;
2325
import java.lang.invoke.MethodType;
2426
import java.net.ConnectException;
27+
import java.net.ServerSocket;
2528
import java.net.URI;
2629
import java.security.AccessController;
2730
import java.security.PrivilegedExceptionAction;
@@ -47,6 +50,7 @@ public void clientWithDefaultSettingAlpn_httpRequest_doesNotThrowUnsupportedOper
4750
assertThat(e.getCause()).hasCauseInstanceOf(ConnectException.class);
4851
assertThat(e.getMessage()).contains("Connection refused");
4952
assertThat(e.getMessage()).doesNotContain("ALPN can only be used with HTTPS, not HTTP. Use ProtocolNegotiation.ASSUME_PROTOCOL instead.");
53+
client.close();
5054
}
5155

5256
@Test
@@ -62,11 +66,12 @@ public void clientWithUserConfiguredAlpn_httpRequest_throwsUnsupportedOperationE
6266
assertThat(e).hasCauseInstanceOf(SdkClientException.class);
6367
assertThat(e.getCause()).hasCauseInstanceOf(UnsupportedOperationException.class);
6468
assertThat(e.getMessage()).contains("ALPN can only be used with HTTPS, not HTTP. Use ProtocolNegotiation.ASSUME_PROTOCOL instead.");
69+
client.close();
6570
}
6671

6772
private H2AsyncClientBuilder clientBuilderWithHttpEndpoint() {
6873
return H2AsyncClient.builder()
69-
.endpointOverride(URI.create("http://localhost:8080"));
74+
.endpointOverride(URI.create("http://localhost:" + getUnusedPort()));
7075
}
7176

7277
private void makeRequest(H2AsyncClient client) {
@@ -90,4 +95,13 @@ private static boolean alpnSupported() {
9095
return false;
9196
}
9297
}
98+
99+
private static int getUnusedPort() {
100+
try (ServerSocket socket = new ServerSocket(0)) {
101+
socket.setReuseAddress(true);
102+
return socket.getLocalPort();
103+
} catch (IOException e) {
104+
throw new UncheckedIOException(e);
105+
}
106+
}
93107
}

0 commit comments

Comments
 (0)