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

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


The following commit(s) were added to refs/heads/main by this push:
     new dbc21418d9 test servers take ports below the ephemeral range so the 
server's pipes listener can't steal them; fail fast when the server exits 
(#3465)
dbc21418d9 is described below

commit dbc21418d995081f9916124e099978046d5dec18
Author: Tim Allison <[email protected]>
AuthorDate: Fri Oct 30 20:33:30 2026 -0400

    test servers take ports below the ephemeral range so the server's pipes 
listener can't steal them; fail fast when the server exits (#3465)
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../apache/tika/server/e2e/RunUatSmokeTest.java    |  7 +--
 .../java/org/apache/tika/server/e2e/TestPorts.java | 51 ++++++++++++++++++++++
 .../tika/server/e2e/TikaServerHttp2Test.java       |  7 +--
 .../tika/server/core/IntegrationTestBase.java      |  4 ++
 .../apache/tika/server/core/TestPortAllocator.java | 37 ++++++++++------
 5 files changed, 82 insertions(+), 24 deletions(-)

diff --git 
a/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/RunUatSmokeTest.java
 
b/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/RunUatSmokeTest.java
index dc405a5847..f635cc2777 100644
--- 
a/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/RunUatSmokeTest.java
+++ 
b/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/RunUatSmokeTest.java
@@ -24,7 +24,6 @@ import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
-import java.net.ServerSocket;
 import java.net.URI;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
@@ -416,10 +415,8 @@ public class RunUatSmokeTest {
         return f;
     }
 
-    private static int findFreePort() throws Exception {
-        try (ServerSocket s = new ServerSocket(0)) {
-            return s.getLocalPort();
-        }
+    private static int findFreePort() {
+        return TestPorts.findFreePort();
     }
 
     /** Locates the unpacked dist jar via {@code tika.server.home} (set by the 
-Pe2e profile). */
diff --git 
a/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/TestPorts.java
 
b/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/TestPorts.java
new file mode 100644
index 0000000000..806e01480f
--- /dev/null
+++ 
b/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/TestPorts.java
@@ -0,0 +1,51 @@
+/*
+ * 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.e2e;
+
+import java.io.IOException;
+import java.net.ServerSocket;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Ports for test servers, from below the ephemeral range so the server's own 
pipes
+ * listener ({@code bind(0)}) can't take one between allocation and Jetty's 
bind. Same
+ * scheme as tika-server-core's TestPortAllocator, which this module can't 
depend on.
+ */
+final class TestPorts {
+
+    private static final int LOW = 20000;
+    private static final int HIGH = 32000;
+
+    private static final AtomicInteger NEXT =
+            new AtomicInteger(ThreadLocalRandom.current().nextInt(HIGH - LOW));
+
+    private TestPorts() {
+    }
+
+    static int findFreePort() {
+        for (int i = 0; i < HIGH - LOW; i++) {
+            int port = LOW + Math.floorMod(NEXT.getAndIncrement(), HIGH - LOW);
+            try (ServerSocket socket = new ServerSocket(port)) {
+                return port;
+            } catch (IOException e) {
+                // in use; try the next
+            }
+        }
+        throw new IllegalStateException("No free port in " + LOW + "-" + HIGH);
+    }
+}
diff --git 
a/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/TikaServerHttp2Test.java
 
b/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/TikaServerHttp2Test.java
index 43e2a56974..3c813323c4 100644
--- 
a/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/TikaServerHttp2Test.java
+++ 
b/tika-e2e-tests/tika-server/src/test/java/org/apache/tika/server/e2e/TikaServerHttp2Test.java
@@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
-import java.net.ServerSocket;
 import java.net.URI;
 import java.net.http.HttpClient;
 import java.net.http.HttpRequest;
@@ -217,10 +216,8 @@ public class TikaServerHttp2Test {
         throw new IllegalStateException("tika-server did not start within " + 
SERVER_STARTUP_TIMEOUT_MILLIS + " ms");
     }
 
-    private static int findFreePort() throws Exception {
-        try (ServerSocket s = new ServerSocket(0)) {
-            return s.getLocalPort();
-        }
+    private static int findFreePort() {
+        return TestPorts.findFreePort();
     }
 
     /** Returns the tika-server-standard-&lt;version&gt;.jar in serverHome, or 
null if absent. */
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 44fb8953e6..e24b937481 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
@@ -253,6 +253,10 @@ public class IntegrationTestBase extends TikaTest {
                 .between(started, Instant.now())
                 .toMillis();
         while (elapsed < MAX_WAIT_MS) {
+            if (process != null && !process.isAlive()) {
+                throw new IllegalStateException("tika-server exited with " + 
process.exitValue()
+                        + " before it accepted connections; see its log 
above");
+            }
             try {
                 Response response = client.get();
                 if (response.getStatus() == 200) {
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
index fcade0d292..e912b5c6cb 100644
--- 
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
@@ -18,29 +18,38 @@ package org.apache.tika.server.core;
 
 import java.io.IOException;
 import java.net.ServerSocket;
+import java.util.concurrent.ThreadLocalRandom;
+import java.util.concurrent.atomic.AtomicInteger;
 
 /**
- * Allocates ephemeral ports for tests to avoid conflicts when running
- * multiple builds simultaneously.
+ * Allocates ports for test servers.
+ * <p>
+ * Ports come from below the ephemeral range (Linux 32768-60999, Windows/macOS
+ * 49152-65535), which the OS never hands out for {@code bind(0)} or outgoing
+ * connections. An ephemeral "free" port, once released, can be taken by the 
server's
+ * own pipes listener before Jetty binds it. Each JVM starts at a random 
offset so
+ * parallel test JVMs rarely meet; a port is checked free before it is 
returned.
  */
 public final class TestPortAllocator {
 
+    static final int LOW = 20000;
+    static final int HIGH = 32000;
+
+    private static final AtomicInteger NEXT =
+            new AtomicInteger(ThreadLocalRandom.current().nextInt(HIGH - LOW));
+
     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);
+        for (int i = 0; i < HIGH - LOW; i++) {
+            int port = LOW + Math.floorMod(NEXT.getAndIncrement(), HIGH - LOW);
+            try (ServerSocket socket = new ServerSocket(port)) {
+                return port;
+            } catch (IOException e) {
+                // in use; try the next
+            }
         }
+        throw new IllegalStateException("No free port in " + LOW + "-" + HIGH);
     }
 }

Reply via email to