This is an automated email from the ASF dual-hosted git repository. tallison pushed a commit to branch TIKA-4640 in repository https://gitbox.apache.org/repos/asf/tika.git
commit efbd1959e834dc49a441d4b870afe030d979e210 Author: tallison <[email protected]> AuthorDate: Fri Jan 30 16:04:59 2026 -0500 TIKA-4640 -- use ephemeral port for unit tests in tika-server --- .../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 63e0044baa..7b025fe5ad 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 @@ -95,7 +95,8 @@ public abstract class CXFTestBase { private static final String TEMPLATE_RESOURCE = "/configs/cxf-test-base-template.json"; private static final ObjectMapper MAPPER = new ObjectMapper(); - 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 TikaLoader 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 00173240ee..a4f48b89ba 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); @@ -91,7 +90,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); + } + } +}
