This is an automated email from the ASF dual-hosted git repository.

tibordigana pushed a commit to branch gh-ci
in repository https://gitbox.apache.org/repos/asf/maven-surefire.git

commit 2da8630e595ce44acfaebbeea5468714fda96e1a
Author: tibordigana <tibor.dig...@gmail.com>
AuthorDate: Thu Apr 15 22:11:50 2021 +0200

    java.net.BindException: Cannot assign requested address
---
 .../surefire/extensions/SurefireForkChannel.java   | 35 ++++++++++++++++++++--
 .../api/util/internal/AsyncSocketTest.java         |  9 +++---
 2 files changed, 37 insertions(+), 7 deletions(-)

diff --git 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java
 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java
index 640d562..1b5304a 100644
--- 
a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java
+++ 
b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/extensions/SurefireForkChannel.java
@@ -32,14 +32,17 @@ import 
org.apache.maven.surefire.extensions.util.LineConsumerThread;
 import javax.annotation.Nonnull;
 import java.io.Closeable;
 import java.io.IOException;
+import java.net.BindException;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.SocketOption;
 import java.nio.Buffer;
 import java.nio.ByteBuffer;
+import java.nio.channels.AlreadyBoundException;
 import java.nio.channels.AsynchronousServerSocketChannel;
 import java.nio.channels.AsynchronousSocketChannel;
 import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.UnsupportedAddressTypeException;
 import java.nio.channels.WritableByteChannel;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
@@ -87,14 +90,40 @@ final class SurefireForkChannel extends ForkChannel
         super( arguments );
         server = open( withThreadPool( THREAD_POOL ) );
         setTrueOptions( SO_REUSEADDR, TCP_NODELAY, SO_KEEPALIVE );
-        InetAddress ip = InetAddress.getLoopbackAddress();
-        server.bind( new InetSocketAddress( ip, 0 ), 1 );
-        InetSocketAddress localAddress = (InetSocketAddress) 
server.getLocalAddress();
+        InetSocketAddress localAddress = bindSocketAddress();
         localHost = localAddress.getHostString();
         localPort = localAddress.getPort();
         sessionId = arguments.getSessionId();
     }
 
+    private InetSocketAddress bindSocketAddress() throws IOException
+    {
+        InetSocketAddress localLoopback = findLocalAddress( 
InetAddress.getLoopbackAddress() );
+        if ( localLoopback != null )
+        {
+            return localLoopback;
+        }
+        InetSocketAddress localhost = findLocalAddress( 
InetAddress.getLocalHost() );
+        if ( localhost != null )
+        {
+            return localhost;
+        }
+        throw new IOException( "Could not find local IP address." );
+    }
+
+    private InetSocketAddress findLocalAddress( InetAddress ip ) throws 
IOException
+    {
+        try
+        {
+            server.bind( new InetSocketAddress( ip, 0 ), 1 );
+            return (InetSocketAddress) server.getLocalAddress();
+        }
+        catch ( BindException | AlreadyBoundException | 
UnsupportedAddressTypeException e )
+        {
+            return null;
+        }
+    }
+
     @Override
     public void connectToClient() throws IOException
     {
diff --git 
a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
 
b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
index f4cb773..0ce0d5e 100644
--- 
a/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
+++ 
b/surefire-api/src/test/java/org/apache/maven/surefire/api/util/internal/AsyncSocketTest.java
@@ -52,8 +52,9 @@ import static 
org.apache.maven.surefire.api.util.internal.Channels.newOutputStre
 import static org.fest.assertions.Assertions.assertThat;
 
 /**
- *
+ * Low level Java benchmark test.
  */
+@SuppressWarnings( "checkstyle:magicnumber" )
 public class AsyncSocketTest
 {
     private static final String LONG_STRING =
@@ -65,7 +66,7 @@ public class AsyncSocketTest
 
     private volatile InetSocketAddress address;
 
-    @Test
+    @Test( timeout = 10_000L )
     public void test() throws Exception
     {
         int forks = 2;
@@ -80,7 +81,7 @@ public class AsyncSocketTest
         AsynchronousChannelGroup group = 
AsynchronousChannelGroup.withThreadPool( executorService );
         AsynchronousServerSocketChannel server = 
AsynchronousServerSocketChannel.open( group );
         setTrueOptions( server, SO_REUSEADDR, TCP_NODELAY, SO_KEEPALIVE );
-        InetAddress ip = InetAddress.getLocalHost();
+        InetAddress ip = InetAddress.getLoopbackAddress();
         server.bind( new InetSocketAddress( ip, 0 ), 1 );
         address = (InetSocketAddress) server.getLocalAddress();
 
@@ -192,7 +193,7 @@ public class AsyncSocketTest
     @SuppressWarnings( "checkstyle:magicnumber" )
     private void client() throws Exception
     {
-        InetSocketAddress hostAddress = new InetSocketAddress( 
InetAddress.getLocalHost(), address.getPort() );
+        InetSocketAddress hostAddress = new InetSocketAddress( 
InetAddress.getLoopbackAddress(), address.getPort() );
         AsynchronousSocketChannel clientSocketChannel = 
AsynchronousSocketChannel.open();
         clientSocketChannel.connect( hostAddress ).get(); // Wait until 
connection is done.
         InputStream is = new BufferedInputStream( newInputStream( 
clientSocketChannel ), 64 * 1024 );

Reply via email to