This is an automated email from the ASF dual-hosted git repository. tabish pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git
The following commit(s) were added to refs/heads/main by this push: new e6d8f68 PROTON-2421 Get the server port from netty unless one is configured e6d8f68 is described below commit e6d8f6836b098cd777eb8c664f579ddbd911d3d1 Author: Timothy Bish <tabish...@gmail.com> AuthorDate: Mon Aug 30 17:05:13 2021 -0400 PROTON-2421 Get the server port from netty unless one is configured Avoid race to obtain server port by using netty API to obtain the server port if none is explicitly configured on start. --- .../protonj2/client/transport/NettyServer.java | 30 ++++++++-------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/transport/NettyServer.java b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/transport/NettyServer.java index 41bb75a..d753c88 100644 --- a/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/transport/NettyServer.java +++ b/protonj2-client/src/test/java/org/apache/qpid/protonj2/client/transport/NettyServer.java @@ -19,15 +19,13 @@ package org.apache.qpid.protonj2.client.transport; import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; -import java.io.IOException; -import java.net.ServerSocket; +import java.net.InetSocketAddress; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import javax.net.ServerSocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLEngine; @@ -77,15 +75,15 @@ public abstract class NettyServer implements AutoCloseable { private static final Logger LOG = LoggerFactory.getLogger(NettyServer.class); - static final int PORT = Integer.parseInt(System.getProperty("port", "8007")); static final String WEBSOCKET_PATH = "/"; + static final int SERVER_CHOOSES_PORT = 0; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; private Channel serverChannel; private final TransportOptions options; private final SslOptions sslOptions; - private int serverPort; + private int serverPort = SERVER_CHOOSES_PORT; private final boolean needClientAuth; private int maxFrameSize = TransportOptions.DEFAULT_WEBSOCKET_MAX_FRAME_SIZE; private String webSocketPath = WEBSOCKET_PATH; @@ -184,7 +182,10 @@ public abstract class NettyServer implements AutoCloseable { } public void start() throws Exception { + start(serverPort); + } + public void start(int listenOn) throws Exception { if (started.compareAndSet(false, true)) { // Configure the server. @@ -223,6 +224,7 @@ public abstract class NettyServer implements AutoCloseable { // Start the server. serverChannel = server.bind(getServerPort()).sync().channel(); + serverPort = ((InetSocketAddress) serverChannel.localAddress()).getPort(); } } @@ -254,22 +256,10 @@ public abstract class NettyServer implements AutoCloseable { } public int getServerPort() { - if (serverPort == 0) { - ServerSocket ss = null; - try { - ss = ServerSocketFactory.getDefault().createServerSocket(0); - serverPort = ss.getLocalPort(); - } catch (IOException e) { // revert back to default - serverPort = PORT; - } finally { - try { - if (ss != null ) { - ss.close(); - } - } catch (IOException e) { // ignore - } - } + if (!started.get()) { + throw new IllegalStateException("Cannot get server port of non-started server"); } + return serverPort; } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org