< prev index next >

test/testlibrary/com/oracle/java/testlibrary/Utils.java

Print this page
rev 7554 : imported patch 8066440.diff


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package com.oracle.java.testlibrary;
  25 
  26 import static com.oracle.java.testlibrary.Asserts.assertTrue;
  27 import java.io.BufferedReader;
  28 import java.io.File;
  29 import java.io.FileReader;
  30 import java.io.IOException;
  31 import java.lang.reflect.Field;
  32 import java.net.InetAddress;
  33 import java.net.ServerSocket;
  34 import java.net.UnknownHostException;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.Collections;
  38 import java.util.List;
  39 import java.util.Random;

  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 import sun.misc.Unsafe;
  43 
  44 /**
  45  * Common library for various test helper functions.
  46  */
  47 public final class Utils {
  48 
  49     /**
  50      * Returns the sequence used by operating system to separate lines.
  51      */
  52     public static final String NEW_LINE = System.getProperty("line.separator");
  53 
  54     /**
  55      * Returns the value of 'test.vm.opts'system property.
  56      */
  57     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  58 
  59     /**


 378     }
 379 
 380     /**
 381      * Returns {@link java.util.Random} generator initialized with particular seed.
 382      * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
 383      * In case no seed is provided, the method uses a random number.
 384      * The used seed printed to stdout.
 385      * @return {@link java.util.Random} generator with particular seed.
 386      */
 387     public static Random getRandomInstance() {
 388         if (RANDOM_GENERATOR == null) {
 389             synchronized (Utils.class) {
 390                 if (RANDOM_GENERATOR == null) {
 391                     RANDOM_GENERATOR = new Random(SEED);
 392                     System.out.printf("For random generator using seed: %d%n", SEED);
 393                     System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
 394                 }
 395             }
 396         }
 397         return RANDOM_GENERATOR;














































 398     }
 399 }


  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package com.oracle.java.testlibrary;
  25 
  26 import static com.oracle.java.testlibrary.Asserts.assertTrue;
  27 import java.io.BufferedReader;
  28 import java.io.File;
  29 import java.io.FileReader;
  30 import java.io.IOException;
  31 import java.lang.reflect.Field;
  32 import java.net.InetAddress;
  33 import java.net.ServerSocket;
  34 import java.net.UnknownHostException;
  35 import java.util.ArrayList;
  36 import java.util.Arrays;
  37 import java.util.Collections;
  38 import java.util.List;
  39 import java.util.Random;
  40 import java.util.function.BooleanSupplier;
  41 import java.util.regex.Matcher;
  42 import java.util.regex.Pattern;
  43 import sun.misc.Unsafe;
  44 
  45 /**
  46  * Common library for various test helper functions.
  47  */
  48 public final class Utils {
  49 
  50     /**
  51      * Returns the sequence used by operating system to separate lines.
  52      */
  53     public static final String NEW_LINE = System.getProperty("line.separator");
  54 
  55     /**
  56      * Returns the value of 'test.vm.opts'system property.
  57      */
  58     public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim();
  59 
  60     /**


 379     }
 380 
 381     /**
 382      * Returns {@link java.util.Random} generator initialized with particular seed.
 383      * The seed could be provided via system property {@link Utils#SEED_PROPERTY_NAME}
 384      * In case no seed is provided, the method uses a random number.
 385      * The used seed printed to stdout.
 386      * @return {@link java.util.Random} generator with particular seed.
 387      */
 388     public static Random getRandomInstance() {
 389         if (RANDOM_GENERATOR == null) {
 390             synchronized (Utils.class) {
 391                 if (RANDOM_GENERATOR == null) {
 392                     RANDOM_GENERATOR = new Random(SEED);
 393                     System.out.printf("For random generator using seed: %d%n", SEED);
 394                     System.out.printf("To re-run test with same seed value please add \"-D%s=%d\" to command line.%n", SEED_PROPERTY_NAME, SEED);
 395                 }
 396             }
 397         }
 398         return RANDOM_GENERATOR;
 399     }
 400     
 401     /**
 402      * Wait for condition to be true
 403      *
 404      * @param condition, a condition to wait for
 405      */
 406     public static final void waitForCondition(BooleanSupplier condition) {
 407         waitForCondition(condition, -1L, 100L);
 408     }
 409 
 410     /**
 411      * Wait until timeout for condition to be true
 412      *
 413      * @param condition, a condition to wait for
 414      * @param timeout a time in milliseconds to wait for condition to be true
 415      * specifying -1 will wait forever
 416      * @return condition value, to determine if wait was successfull
 417      */
 418     public static final boolean waitForCondition(BooleanSupplier condition,
 419             long timeout) {
 420         return waitForCondition(condition, timeout, 100L);
 421     }
 422 
 423     /**
 424      * Wait until timeout for condition to be true for specified time
 425      *
 426      * @param condition, a condition to wait for
 427      * @param timeout a time in milliseconds to wait for condition to be true,
 428      * specifying -1 will wait forever
 429      * @param sleepTime a time to sleep value in milliseconds
 430      * @return condition value, to determine if wait was successfull
 431      */
 432     public static final boolean waitForCondition(BooleanSupplier condition,
 433             long timeout, long sleepTime) {
 434         long startTime = System.currentTimeMillis();
 435         while (!(condition.getAsBoolean() || (timeout != -1L
 436                 && ((System.currentTimeMillis() - startTime) > timeout)))) {
 437             try {
 438                 Thread.sleep(sleepTime);
 439             } catch (InterruptedException e) {
 440                 Thread.currentThread().interrupt();
 441                 throw new Error(e);
 442             }
 443         } 
 444         return condition.getAsBoolean();
 445     }
 446 }
< prev index next >