44
44
import java .util .HashMap ;
45
45
import java .util .Locale ;
46
46
import java .util .Map ;
47
+ import java .util .concurrent .Callable ;
47
48
import java .util .concurrent .atomic .AtomicBoolean ;
48
49
import java .util .concurrent .atomic .AtomicReference ;
49
50
import java .util .zip .GZIPInputStream ;
@@ -246,10 +247,13 @@ void loadOnStartAfterContextIsInitialized() {
246
247
@ Test
247
248
void specificPort () throws Exception {
248
249
AbstractServletWebServerFactory factory = getFactory ();
249
- int specificPort = SocketUtils .findAvailableTcpPort (41000 );
250
- factory .setPort (specificPort );
251
- this .webServer = factory .getWebServer (exampleServletRegistration ());
252
- this .webServer .start ();
250
+ int specificPort = doWithRetry (() -> {
251
+ int port = SocketUtils .findAvailableTcpPort (41000 );
252
+ factory .setPort (port );
253
+ this .webServer = factory .getWebServer (exampleServletRegistration ());
254
+ this .webServer .start ();
255
+ return port ;
256
+ });
253
257
assertThat (getResponse ("http://localhost:" + specificPort + "/hello" )).isEqualTo ("Hello World" );
254
258
assertThat (this .webServer .getPort ()).isEqualTo (specificPort );
255
259
}
@@ -823,7 +827,7 @@ void serverHeaderIsDisabledByDefault() throws Exception {
823
827
}
824
828
825
829
@ Test
826
- protected void portClashOfPrimaryConnectorResultsInPortInUseException () throws IOException {
830
+ protected void portClashOfPrimaryConnectorResultsInPortInUseException () throws Exception {
827
831
doWithBlockedPort ((port ) -> {
828
832
assertThatExceptionOfType (RuntimeException .class ).isThrownBy (() -> {
829
833
AbstractServletWebServerFactory factory = getFactory ();
@@ -835,7 +839,7 @@ protected void portClashOfPrimaryConnectorResultsInPortInUseException() throws I
835
839
}
836
840
837
841
@ Test
838
- void portClashOfSecondaryConnectorResultsInPortInUseException () throws IOException {
842
+ void portClashOfSecondaryConnectorResultsInPortInUseException () throws Exception {
839
843
doWithBlockedPort ((port ) -> {
840
844
assertThatExceptionOfType (RuntimeException .class ).isThrownBy (() -> {
841
845
AbstractServletWebServerFactory factory = getFactory ();
@@ -1131,19 +1135,28 @@ public void service(ServletRequest request, ServletResponse response) throws IOE
1131
1135
return bean ;
1132
1136
}
1133
1137
1134
- protected final void doWithBlockedPort (BlockedPortAction action ) throws IOException {
1135
- int port = SocketUtils .findAvailableTcpPort (40000 );
1136
- ServerSocket serverSocket = new ServerSocket ();
1138
+ private <T > T doWithRetry (Callable <T > action ) throws Exception {
1139
+ Exception lastFailure = null ;
1137
1140
for (int i = 0 ; i < 10 ; i ++) {
1138
1141
try {
1139
- serverSocket .bind (new InetSocketAddress (port ));
1140
- break ;
1142
+ return action .call ();
1141
1143
}
1142
1144
catch (Exception ex ) {
1145
+ lastFailure = ex ;
1143
1146
}
1144
1147
}
1148
+ throw new IllegalStateException ("Action was not successful in 10 attempts" , lastFailure );
1149
+ }
1150
+
1151
+ protected final void doWithBlockedPort (BlockedPortAction action ) throws Exception {
1152
+ ServerSocket serverSocket = new ServerSocket ();
1153
+ int blockedPort = doWithRetry (() -> {
1154
+ int port = SocketUtils .findAvailableTcpPort (40000 );
1155
+ serverSocket .bind (new InetSocketAddress (port ));
1156
+ return port ;
1157
+ });
1145
1158
try {
1146
- action .run (port );
1159
+ action .run (blockedPort );
1147
1160
}
1148
1161
finally {
1149
1162
serverSocket .close ();
0 commit comments