This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch 2.2.X in repository https://gitbox.apache.org/repos/asf/mina.git
commit 297f619a618eb550024769a0c86341c2417eb43e Author: Emmanuel Lécharny <[email protected]> AuthorDate: Fri Oct 2 09:33:05 2026 +0200 Added a Host parameter so that we can create an InetAddress with it --- .../transport/socket/nio/SocketAcceptorTest.java | 5 +- .../org/apache/mina/util/AcceptorBindUtil.java | 57 +++++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/mina-core/src/test/java/org/apache/mina/transport/socket/nio/SocketAcceptorTest.java b/mina-core/src/test/java/org/apache/mina/transport/socket/nio/SocketAcceptorTest.java index 6b5995435..623c95600 100644 --- a/mina-core/src/test/java/org/apache/mina/transport/socket/nio/SocketAcceptorTest.java +++ b/mina-core/src/test/java/org/apache/mina/transport/socket/nio/SocketAcceptorTest.java @@ -58,8 +58,9 @@ public class SocketAcceptorTest { acceptor.setReuseAddress(true); acceptor.setHandler(new IoHandlerAdapter()); try { - int port = AcceptorBindUtil.tryBind(acceptor); - InetSocketAddress address = new InetSocketAddress("127.0.0.1", port); + int port = AcceptorBindUtil.tryBind(acceptor, "127.0.0.1"); + System.out.println( "Listening on " + acceptor.getLocalAddress() ); + InetSocketAddress address = acceptor.getLocalAddress(); //new InetSocketAddress("127.0.0.1", port); acceptor.unbind(address); acceptor.bind(address); acceptor.unbind(address); diff --git a/mina-core/src/test/java/org/apache/mina/util/AcceptorBindUtil.java b/mina-core/src/test/java/org/apache/mina/util/AcceptorBindUtil.java index 251214171..d098ddceb 100644 --- a/mina-core/src/test/java/org/apache/mina/util/AcceptorBindUtil.java +++ b/mina-core/src/test/java/org/apache/mina/util/AcceptorBindUtil.java @@ -65,7 +65,35 @@ public class AcceptorBindUtil { } } } - + + /** + * Try to bind an IoAcceptor, using a random port, but retrying + * if the port get used between the moment it's picked and the moment + * the bind occurs. If it fails, we get a untimeException + * + * @param acceptor The IoAccceptor we try to bind + * @param host The host to use + * @return The port to which the IoAcceptor is bound to + */ + public static final int tryBind(IoAcceptor acceptor, String host) { + int nbTry = NB_TRIES; + + while (true) { + int nextAvailable = AvailablePortFinder.getNextAvailable(); + try { + acceptor.bind(new InetSocketAddress(host, nextAvailable)); + + return nextAvailable; + } catch ( IOException e ) { + nbTry--; + + if (nbTry == 0) { + throw new RuntimeException(e); + } + } + } + } + /** * Try to bind an IoAcceptor, using a random port, but retrying * if the port get used between the moment it's picked and the moment @@ -91,4 +119,31 @@ public class AcceptorBindUtil { } } } + + /** + * Try to bind an IoAcceptor, using a random port, but retrying + * if the port get used between the moment it's picked and the moment + * the bind occurs. If it fails, we get a untimeException + * + * @param acceptor The IoAccceptor we try to bind + * @param host The host to use + * @param nbTries The number of times it tries + * @return The port to which the IoAcceptor is bound to + */ + public static final int tryBind(IoAcceptor acceptor, String host, int nbTries) { + while (true) { + int nextAvailable = AvailablePortFinder.getNextAvailable(); + try { + acceptor.bind(new InetSocketAddress(host, nextAvailable)); + + return nextAvailable; + } catch ( IOException e ) { + nbTries--; + + if (nbTries == 0) { + throw new RuntimeException(e); + } + } + } + } }
