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

ravindra 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 534e6aa  ARROW-4754: [Java] Randomize port and retry binding server 
when bind fails
534e6aa is described below

commit 534e6aa16f40cf13e8688bf227391aa5727edd56
Author: Micah Kornfield <emkornfi...@gmail.com>
AuthorDate: Tue Mar 5 11:35:54 2019 +0530

    ARROW-4754: [Java] Randomize port and retry binding server when bind fails
    
    Also, random cleanups to make intellij happy.
    
    Should prevent reoccurences of  recent master build failure: 
https://travis-ci.org/apache/arrow/jobs/501310887
    
    Author: Micah Kornfield <emkornfi...@gmail.com>
    
    Closes #3801 from emkornfield/dont_bind_to_same_port and squashes the 
following commits:
    
    393338d4 <Micah Kornfield> ARROW-4755:  Randomize selectin of all ports
    9e455f71 <Micah Kornfield> ARROW-4754:  Randomize port and retry binding 
server in flight TestAuth
---
 .../org/apache/arrow/flight/FlightTestUtil.java    | 67 ++++++++++++++++++++++
 .../org/apache/arrow/flight/TestBackPressure.java  | 24 ++++----
 .../apache/arrow/flight/TestBasicOperation.java    |  9 ++-
 .../org/apache/arrow/flight/auth/TestAuth.java     | 21 +++----
 .../arrow/flight/example/TestExampleServer.java    |  2 +-
 .../org/apache/arrow/flight/perf/TestPerf.java     | 14 +++--
 6 files changed, 99 insertions(+), 38 deletions(-)

diff --git 
a/java/flight/src/test/java/org/apache/arrow/flight/FlightTestUtil.java 
b/java/flight/src/test/java/org/apache/arrow/flight/FlightTestUtil.java
new file mode 100644
index 0000000..11b476c
--- /dev/null
+++ b/java/flight/src/test/java/org/apache/arrow/flight/FlightTestUtil.java
@@ -0,0 +1,67 @@
+/*
+ * 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.arrow.flight;
+
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Random;
+import java.util.function.Function;
+
+/**
+ * Utility methods and constants for testing flight servers.
+ */
+public class FlightTestUtil {
+  private static final Random RANDOM = new Random();
+
+  public static final String LOCALHOST = "localhost";
+
+  /**
+   * Returns a a FlightServer (actually anything that is startable)
+   * that has been started bound to a random port.
+   */
+  public static <T> T getStartedServer(Function<Integer, T> newServerFromPort) 
throws IOException {
+    IOException lastThrown = null;
+    T server = null;
+    for (int x = 0; x < 3; x++) {
+      final int port = 49152 + RANDOM.nextInt(5000);
+      lastThrown = null;
+      try {
+        server = newServerFromPort.apply(port);
+        try {
+          server.getClass().getMethod("start").invoke(server);
+        } catch (NoSuchMethodException | IllegalAccessException e) {
+          throw new IllegalArgumentException("Couldn't call start method on 
object.", e);
+        }
+        break;
+      } catch (InvocationTargetException e) {
+        if (e.getTargetException() instanceof IOException) {
+          lastThrown = (IOException) e.getTargetException();
+        } else {
+          throw (RuntimeException) e.getTargetException();
+        }
+      }
+    }
+    if (lastThrown != null) {
+      throw lastThrown;
+    }
+    return server;
+  }
+
+  private FlightTestUtil() {
+  }
+}
diff --git 
a/java/flight/src/test/java/org/apache/arrow/flight/TestBackPressure.java 
b/java/flight/src/test/java/org/apache/arrow/flight/TestBackPressure.java
index 5aa47f6..505e0ba 100644
--- a/java/flight/src/test/java/org/apache/arrow/flight/TestBackPressure.java
+++ b/java/flight/src/test/java/org/apache/arrow/flight/TestBackPressure.java
@@ -44,16 +44,12 @@ public class TestBackPressure {
   @Ignore
   @Test
   public void ensureIndependentSteams() throws Exception {
-
-    final Location l = new Location("localhost", 12233);
     try (
         final BufferAllocator a = new RootAllocator(Long.MAX_VALUE);
-        final PerformanceTestServer server = new PerformanceTestServer(a, l);
-        final FlightClient client = new FlightClient(a, l);
-        ) {
-
-      server.start();
-
+        final PerformanceTestServer server = FlightTestUtil.getStartedServer(
+            (port) -> (new PerformanceTestServer(a, new 
Location(FlightTestUtil.LOCALHOST, port))));
+        final FlightClient client = new FlightClient(a, server.getLocation())
+    ) {
       FlightStream fs1 = client.getStream(client.getInfo(
           TestPerf.getPerfFlightDescriptor(110L * BATCH_SIZE, BATCH_SIZE, 1))
           .getEndpoints().get(0).getTicket());
@@ -87,7 +83,6 @@ public class TestBackPressure {
     final long wait = 3000;
     final long epsilon = 1000;
 
-    final Location l = new Location("localhost", 12233);
     AtomicLong sleepTime = new AtomicLong(0);
     try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE)) {
 
@@ -125,12 +120,13 @@ public class TestBackPressure {
 
       try (
           BufferAllocator serverAllocator = 
allocator.newChildAllocator("server", 0, Long.MAX_VALUE);
-          FlightServer server = new FlightServer(serverAllocator, l.getPort(), 
producer, ServerAuthHandler.NO_OP);
+          FlightServer server =
+              FlightTestUtil.getStartedServer(
+                  (port) -> new FlightServer(serverAllocator, port, producer, 
ServerAuthHandler.NO_OP));
           BufferAllocator clientAllocator = 
allocator.newChildAllocator("client", 0, Long.MAX_VALUE);
-          FlightClient client = new FlightClient(clientAllocator, l)
-        ) {
-
-        server.start();
+          FlightClient client =
+              new FlightClient(clientAllocator, new 
Location(FlightTestUtil.LOCALHOST, server.getPort()))
+      ) {
         FlightStream stream = client.getStream(new Ticket(new byte[1]));
         VectorSchemaRoot root = stream.getRoot();
         root.clear();
diff --git 
a/java/flight/src/test/java/org/apache/arrow/flight/TestBasicOperation.java 
b/java/flight/src/test/java/org/apache/arrow/flight/TestBasicOperation.java
index f95744a..8fe93c1 100644
--- a/java/flight/src/test/java/org/apache/arrow/flight/TestBasicOperation.java
+++ b/java/flight/src/test/java/org/apache/arrow/flight/TestBasicOperation.java
@@ -138,13 +138,12 @@ public class TestBasicOperation {
     try (
         BufferAllocator a = new RootAllocator(Long.MAX_VALUE);
         Producer producer = new Producer(a);
-        FlightServer s = new FlightServer(a, 12233, producer, 
ServerAuthHandler.NO_OP);) {
-
-      s.start();
+        FlightServer s =
+            FlightTestUtil.getStartedServer((port) -> new FlightServer(a, 
port, producer, ServerAuthHandler.NO_OP))) {
 
       try (
-          FlightClient c = new FlightClient(a, new Location("localhost", 
12233));
-        ) {
+          FlightClient c = new FlightClient(a, new 
Location(FlightTestUtil.LOCALHOST, s.getPort()));
+      ) {
         try (BufferAllocator testAllocator = a.newChildAllocator("testcase", 
0, Long.MAX_VALUE)) {
           consumer.accept(c, testAllocator);
         }
diff --git 
a/java/flight/src/test/java/org/apache/arrow/flight/auth/TestAuth.java 
b/java/flight/src/test/java/org/apache/arrow/flight/auth/TestAuth.java
index c8f3fc5..384190f 100644
--- a/java/flight/src/test/java/org/apache/arrow/flight/auth/TestAuth.java
+++ b/java/flight/src/test/java/org/apache/arrow/flight/auth/TestAuth.java
@@ -24,12 +24,14 @@ import org.apache.arrow.flight.Criteria;
 import org.apache.arrow.flight.FlightClient;
 import org.apache.arrow.flight.FlightInfo;
 import org.apache.arrow.flight.FlightServer;
+import org.apache.arrow.flight.FlightTestUtil;
 import org.apache.arrow.flight.Location;
 import org.apache.arrow.flight.NoOpFlightProducer;
 import org.apache.arrow.memory.BufferAllocator;
 import org.apache.arrow.memory.RootAllocator;
 import org.apache.arrow.util.AutoCloseables;
 import org.junit.After;
+import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -53,7 +55,8 @@ public class TestAuth {
   @Test
   public void validAuth() {
     client.authenticateBasic(USERNAME, PASSWORD);
-    ImmutableList.copyOf(client.listFlights(Criteria.ALL));
+    
Assert.assertTrue(ImmutableList.copyOf(client.listFlights(Criteria.ALL)).size() 
>= 0);
+
   }
 
   @Test
@@ -75,8 +78,6 @@ public class TestAuth {
   @Before
   public void setup() throws IOException {
     allocator = new RootAllocator(Long.MAX_VALUE);
-    final Location l = new Location("localhost", 12233);
-
     final BasicServerAuthHandler.BasicAuthValidator validator = new 
BasicServerAuthHandler.BasicAuthValidator() {
 
       @Override
@@ -85,7 +86,7 @@ public class TestAuth {
       }
 
       @Override
-      public byte[] getToken(String username, String password) throws 
Exception {
+      public byte[] getToken(String username, String password) {
         if (USERNAME.equals(username) && PASSWORD.equals(password)) {
           return VALID_TOKEN;
         } else {
@@ -94,23 +95,19 @@ public class TestAuth {
       }
     };
 
-    server = new FlightServer(
+    server = FlightTestUtil.getStartedServer((port) -> new FlightServer(
         allocator,
-        l.getPort(),
+        port,
         new NoOpFlightProducer() {
           @Override
           public void listFlights(Criteria criteria, 
StreamListener<FlightInfo> listener) {
             listener.onCompleted();
           }
         },
-        new BasicServerAuthHandler(validator));
-
-    server.start();
-    client = new FlightClient(allocator, l);
+        new BasicServerAuthHandler(validator)));
+    client = new FlightClient(allocator, new 
Location(FlightTestUtil.LOCALHOST, server.getPort()));
   }
 
-
-
   @After
   public void shutdown() throws Exception {
     AutoCloseables.close(client, server, allocator);
diff --git 
a/java/flight/src/test/java/org/apache/arrow/flight/example/TestExampleServer.java
 
b/java/flight/src/test/java/org/apache/arrow/flight/example/TestExampleServer.java
index 597c5a9..3730a09 100644
--- 
a/java/flight/src/test/java/org/apache/arrow/flight/example/TestExampleServer.java
+++ 
b/java/flight/src/test/java/org/apache/arrow/flight/example/TestExampleServer.java
@@ -67,7 +67,7 @@ public class TestExampleServer {
   }
 
   @Test
-  public void putStream() throws Exception {
+  public void putStream() {
     BufferAllocator a = caseAllocator;
     final int size = 10;
 
diff --git 
a/java/flight/src/test/java/org/apache/arrow/flight/perf/TestPerf.java 
b/java/flight/src/test/java/org/apache/arrow/flight/perf/TestPerf.java
index 220f4bb..1047c97 100644
--- a/java/flight/src/test/java/org/apache/arrow/flight/perf/TestPerf.java
+++ b/java/flight/src/test/java/org/apache/arrow/flight/perf/TestPerf.java
@@ -27,6 +27,7 @@ import org.apache.arrow.flight.FlightClient;
 import org.apache.arrow.flight.FlightDescriptor;
 import org.apache.arrow.flight.FlightInfo;
 import org.apache.arrow.flight.FlightStream;
+import org.apache.arrow.flight.FlightTestUtil;
 import org.apache.arrow.flight.Location;
 import org.apache.arrow.flight.Ticket;
 import org.apache.arrow.flight.perf.impl.PerfOuterClass.Perf;
@@ -60,7 +61,7 @@ public class TestPerf {
         Field.nullable("b", MinorType.BIGINT.getType()),
         Field.nullable("c", MinorType.BIGINT.getType()),
         Field.nullable("d", MinorType.BIGINT.getType())
-        ));
+    ));
 
     FlatBufferBuilder builder = new FlatBufferBuilder();
     pojoSchema.getSchema(builder);
@@ -77,12 +78,13 @@ public class TestPerf {
   @Test
   public void throughput() throws Exception {
     for (int i = 0; i < 10; i++) {
-      final Location l = new Location("localhost", 12233);
       try (
           final BufferAllocator a = new RootAllocator(Long.MAX_VALUE);
-          final PerformanceTestServer server = new PerformanceTestServer(a, l);
-          final FlightClient client = new FlightClient(a, l);
-          ) {
+          final PerformanceTestServer server =
+              FlightTestUtil.getStartedServer((port) -> new 
PerformanceTestServer(a,
+                  new Location(FlightTestUtil.LOCALHOST, port)));
+          final FlightClient client = new FlightClient(a, 
server.getLocation());
+      ) {
 
         server.start();
 
@@ -108,7 +110,7 @@ public class TestPerf {
             (r.bytes * 1.0d / 1024 / 1024) / seconds,
             (r.rows * 1.0d) / seconds,
             (r.batches * 1.0d) / seconds
-            ));
+        ));
       }
     }
   }

Reply via email to