test/java/rmi/testlibrary/TestLibrary.java

Print this page

        

@@ -133,26 +133,27 @@
      * @param port The port number to check
      * @param msTimeout The amount of milliseconds to spend checking
      */
 
     public static boolean checkIfRegistryRunning(int port, int msTimeout) {
-        long stopTime = System.currentTimeMillis() + msTimeout;
+        final long POLLTIME_MS = 100L;
+        long stopTime = computeDeadline(System.currentTimeMillis(), msTimeout);
         do {
             try {
                 Registry r = LocateRegistry.getRegistry(port);
                 String[] s = r.list();
                 // no exception. We're now happy that registry is running
                 return true;
             } catch (RemoteException e) {
                 // problem - not ready ? Try again
                 try {
-                    Thread.sleep(500);
+                    Thread.sleep(POLLTIME_MS);
                 } catch (InterruptedException ie) {
                     // not expected
                 }
             }
-        } while (stopTime > System.currentTimeMillis());
+        } while (System.currentTimeMillis() < stopTime);
         return false;
     }
 
     public static String getProperty(final String property,
                                      final String defaultVal) {

@@ -167,10 +168,35 @@
             bomb("Exception getting property " + property, ex);
             throw new AssertionError("this should be unreachable");
         }
     }
 
+    public static double getTimeoutFactor() {
+        String prop = getProperty("test.timeout.factor", "1.0");
+        double timeoutFactor = 1.0;
+
+        try {
+            timeoutFactor = Double.parseDouble(prop);
+        } catch (NumberFormatException ignore) { }
+
+        return timeoutFactor;
+    }
+
+    /**
+     * Computes a deadline from a timestamp and a timeout value.
+     * Maximum timeout (before multipliers are applied) is one hour.
+     */
+    public static long computeDeadline(long timestamp, long timeout) {
+        final long MAX_TIMEOUT_MS = 3_600_000L;
+
+        if (timeout < 0L || timeout > MAX_TIMEOUT_MS) {
+            throw new IllegalArgumentException("timeout " + timeout + "ms out of range");
+        }
+
+        return timestamp + (long)(timeout * getTimeoutFactor());
+    }
+
     /**
      * Property mutators
      */
     public static void setBoolean(String property, boolean value) {
         setProperty(property, (new Boolean(value)).toString());