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

tallison pushed a commit to branch branch_3x
in repository https://gitbox.apache.org/repos/asf/tika.git


The following commit(s) were added to refs/heads/branch_3x by this push:
     new ca29e3d32f TIKA-4640 -- use ephemeral port for unit tests in 
tika-server (#2560)
ca29e3d32f is described below

commit ca29e3d32f7cca02d479c27f2c69b0b2e5183883
Author: Tim Allison <[email protected]>
AuthorDate: Fri Jan 30 16:38:50 2026 -0500

    TIKA-4640 -- use ephemeral port for unit tests in tika-server (#2560)
---
 .../org/apache/tika/server/core/CXFTestBase.java   |  3 +-
 .../tika/server/core/IntegrationTestBase.java      |  8 ++--
 .../apache/tika/server/core/TestPortAllocator.java | 46 ++++++++++++++++++++++
 3 files changed, 52 insertions(+), 5 deletions(-)

diff --git 
a/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java
 
b/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java
index d6a455eb0c..375412326f 100644
--- 
a/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java
+++ 
b/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/CXFTestBase.java
@@ -56,7 +56,8 @@ import org.apache.tika.server.core.resource.TikaResource;
 import org.apache.tika.server.core.resource.UnpackerResource;
 
 public abstract class CXFTestBase {
-    protected static final String endPoint = "http://localhost:"; + 
TikaServerConfig.DEFAULT_PORT;
+    protected static final int testPort = TestPortAllocator.findFreePort();
+    protected static final String endPoint = "http://localhost:"; + testPort;
     protected final static int DIGESTER_READ_LIMIT = 20 * 1024 * 1024;
     protected Server server;
     protected TikaConfig tika;
diff --git 
a/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/IntegrationTestBase.java
 
b/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/IntegrationTestBase.java
index b5b066207d..7b0b5f9343 100644
--- 
a/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/IntegrationTestBase.java
+++ 
b/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/IntegrationTestBase.java
@@ -52,9 +52,8 @@ public class IntegrationTestBase extends TikaTest {
     static final String STATUS_PATH = "/status";
 
     static final long MAX_WAIT_MS = 60000;
-    //running into conflicts on 9998 with the CXFTestBase tests
-    //TODO: figure out why?!
-    static final String INTEGRATION_TEST_PORT = "9999";
+    static final int integrationTestPort = TestPortAllocator.findFreePort();
+    static final String INTEGRATION_TEST_PORT = 
String.valueOf(integrationTestPort);
     protected static final String endPoint = "http://localhost:"; + 
INTEGRATION_TEST_PORT;
     private static final Logger LOG = 
LoggerFactory.getLogger(IntegrationTestBase.class);
 
@@ -85,7 +84,8 @@ public class IntegrationTestBase extends TikaTest {
     }
 
     public void startProcess(String[] extraArgs) throws IOException {
-        String[] base = new String[]{"java", "-cp", 
System.getProperty("java.class.path"), 
"org.apache.tika.server.core.TikaServerCli",};
+        String[] base = new String[]{"java", "-cp", 
System.getProperty("java.class.path"), 
"org.apache.tika.server.core.TikaServerCli",
+                "-p", INTEGRATION_TEST_PORT};
         List<String> args = new ArrayList<>(Arrays.asList(base));
         args.addAll(Arrays.asList(extraArgs));
         ProcessBuilder pb = new ProcessBuilder(args);
diff --git 
a/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/TestPortAllocator.java
 
b/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/TestPortAllocator.java
new file mode 100644
index 0000000000..fcade0d292
--- /dev/null
+++ 
b/tika-server/tika-server-core/src/test/java/org/apache/tika/server/core/TestPortAllocator.java
@@ -0,0 +1,46 @@
+/*
+ * 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.tika.server.core;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+
+/**
+ * Allocates ephemeral ports for tests to avoid conflicts when running
+ * multiple builds simultaneously.
+ */
+public final class TestPortAllocator {
+
+    private TestPortAllocator() {
+    }
+
+    /**
+     * Finds and returns an available port.
+     * <p>
+     * Note: There is a small race window between closing the ServerSocket
+     * and the test actually binding to the port. In practice, this is
+     * rarely an issue since ephemeral ports are drawn from a large pool.
+     */
+    public static int findFreePort() {
+        try (ServerSocket socket = new ServerSocket(0)) {
+            socket.setReuseAddress(true);
+            return socket.getLocalPort();
+        } catch (IOException e) {
+            throw new RuntimeException("Could not find free port", e);
+        }
+    }
+}

Reply via email to