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

lidavidm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow.git


The following commit(s) were added to refs/heads/master by this push:
     new a0d0ecee71 GH-33993: [Java] Let OS assign port in tests while creating 
Flight server (#33992)
a0d0ecee71 is described below

commit a0d0ecee71b99b45f2c1b269c70033d39b982d00
Author: rtadepalli <[email protected]>
AuthorDate: Thu Feb 2 07:58:27 2023 -0500

    GH-33993: [Java] Let OS assign port in tests while creating Flight server 
(#33992)
    
    
    
    ### Rationale for this change
    
    Sometimes tests are flaky because the current port selection algorithm 
selects a random port between the ranges 49152-54152. This change makes it such 
that the OS will assign the next free port without us searching for a port in 
this range.
    
    ### What changes are included in this PR?
    
    This PR makes use of  `java.net.ServerSocket`'s ability to assign the next 
free port if 0 is passed as the port number.
    
    ### Are these changes tested?
    
    If existing tests pass, there's confirmation that the Flight server is able 
to come up successfully, thereby no new tests should be needed.
    
    ### Are there any user-facing changes?
    
    No, there are no user facing changes.
    
    * Closes: #32954
    * Closes: #33993
    
    Authored-by: Ramasai <[email protected]>
    Signed-off-by: David Li <[email protected]>
---
 .../src/test/java/org/apache/arrow/flight/FlightTestUtil.java    | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git 
a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/FlightTestUtil.java
 
b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/FlightTestUtil.java
index a0eb80daca..b50613311b 100644
--- 
a/java/flight/flight-core/src/test/java/org/apache/arrow/flight/FlightTestUtil.java
+++ 
b/java/flight/flight-core/src/test/java/org/apache/arrow/flight/FlightTestUtil.java
@@ -20,6 +20,7 @@ package org.apache.arrow.flight;
 import java.io.File;
 import java.io.IOException;
 import java.lang.reflect.InvocationTargetException;
+import java.net.ServerSocket;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.Arrays;
@@ -51,7 +52,13 @@ public class FlightTestUtil {
     IOException lastThrown = null;
     T server = null;
     for (int x = 0; x < 3; x++) {
-      final int port = 49152 + RANDOM.nextInt(5000);
+      int port;
+      try (final ServerSocket dynamicPortSocket = new ServerSocket(0)) {
+        port = dynamicPortSocket.getLocalPort();
+      } catch (SecurityException | IOException e) {
+        throw new RuntimeException("Unable to create a ServerSocket instance. 
", e);
+      }
+
       final Location location = Location.forGrpcInsecure(LOCALHOST, port);
       lastThrown = null;
       try {

Reply via email to