1818import static org .assertj .core .api .Assertions .assertThat ;
1919import static org .junit .jupiter .api .Assertions .assertThrows ;
2020
21+ import java .io .IOException ;
22+ import java .io .UncheckedIOException ;
2123import java .lang .invoke .MethodHandle ;
2224import java .lang .invoke .MethodHandles ;
2325import java .lang .invoke .MethodType ;
2426import java .net .ConnectException ;
27+ import java .net .ServerSocket ;
2528import java .net .URI ;
2629import java .security .AccessController ;
2730import 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