You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2019/12/05 08:31:57 UTC

[GitHub] [camel-quarkus] lburgazzoli commented on a change in pull request #509: Do not use hard coded ports in integration-tests

lburgazzoli commented on a change in pull request #509: Do not use hard coded ports in integration-tests
URL: https://github.com/apache/camel-quarkus/pull/509#discussion_r354165931
 
 

 ##########
 File path: integration-tests/support/test-support/src/main/java/org/apache/camel/quarkus/test/AvailablePortFinder.java
 ##########
 @@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.quarkus.test;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.function.Function;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Finds currently available server ports.
+ */
+public final class AvailablePortFinder {
+    private static final Logger LOGGER = LoggerFactory.getLogger(AvailablePortFinder.class);
+
+    /**
+     * Creates a new instance.
+     */
+    private AvailablePortFinder() {
+        // Do nothing
+    }
+
+    /**
+     * Gets the next available port.
+     *
+     * @throws IllegalStateException if there are no ports available
+     * @return the available port
+     */
+    public static int getNextAvailable() {
+        try (ServerSocket ss = new ServerSocket()) {
+            ss.setReuseAddress(true);
+            ss.bind(new InetSocketAddress((InetAddress) null, 0), 1);
 
 Review comment:
   This is a copy and paste of an utility class from camel that is in place since quite a few years (but you can find the same implementation on other projects such as mina, spring and what not). 
   
   Under the hood binding is done via syscall and in the kernel will assign a pseudo random free port so yes there is the theoretical possibility that two process get the same port but that would happen only in case you have a **lot** of ephemeral tcp connections.
   
   A better solution would be to use unix socket but that's not very common for the majority of the components we have.
   
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services