/**
   * Runs Runnable r with a security policy that permits precisely the specified permissions. If
   * there is no current security manager, the runnable is run twice, both with and without a
   * security manager. We require that any security manager permit getPolicy/setPolicy.
   */
  public void runWithPermissions(Runnable r, Permission... permissions) {
    SecurityManager sm = System.getSecurityManager();
    if (sm == null) {
      r.run();
      Policy savedPolicy = Policy.getPolicy();
      try {
        Policy.setPolicy(permissivePolicy());
        System.setSecurityManager(new SecurityManager());
        runWithPermissions(r, permissions);
      } finally {
        System.setSecurityManager(null);
        Policy.setPolicy(savedPolicy);
      }
    } else {
      Policy savedPolicy = Policy.getPolicy();
      AdjustablePolicy policy = new AdjustablePolicy(permissions);
      Policy.setPolicy(policy);

      try {
        r.run();
      } finally {
        policy.addPermission(new SecurityPermission("setPolicy"));
        Policy.setPolicy(savedPolicy);
      }
    }
  }
 /**
  * Delays, via Thread.sleep, for the given millisecond delay, but if the sleep is shorter than
  * specified, may re-sleep or yield until time elapses.
  */
 static void delay(long millis) throws InterruptedException {
   long startTime = System.nanoTime();
   long ns = millis * 1000 * 1000;
   for (; ; ) {
     if (millis > 0L) Thread.sleep(millis);
     else // too short to sleep
     Thread.yield();
     long d = ns - (System.nanoTime() - startTime);
     if (d > 0L) millis = d / (1000 * 1000);
     else break;
   }
 }
  /** Runs all JSR166 unit tests using junit.textui.TestRunner */
  public static void main(String[] args) {
    if (useSecurityManager) {
      System.err.println("Setting a permissive security manager");
      Policy.setPolicy(permissivePolicy());
      System.setSecurityManager(new SecurityManager());
    }
    int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);

    Test s = suite();
    for (int i = 0; i < iters; ++i) {
      junit.textui.TestRunner.run(s);
      System.gc();
      System.runFinalization();
    }
    System.exit(0);
  }
 protected void runTestProfiled() throws Throwable {
   // Warmup run, notably to trigger all needed classloading.
   super.runTest();
   long t0 = System.nanoTime();
   try {
     super.runTest();
   } finally {
     long elapsedMillis = millisElapsedSince(t0);
     if (elapsedMillis >= profileThreshold)
       System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
   }
 }
 /** Checks that future.get times out, with the given millisecond timeout. */
 void assertFutureTimesOut(Future future, long timeoutMillis) {
   long startTime = System.nanoTime();
   try {
     future.get(timeoutMillis, MILLISECONDS);
     shouldThrow();
   } catch (TimeoutException success) {
   } catch (Exception e) {
     threadUnexpectedException(e);
   } finally {
     future.cancel(true);
   }
   assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
 }
 /**
  * Spin-waits up to the specified number of milliseconds for the given thread to enter a wait
  * state: BLOCKED, WAITING, or TIMED_WAITING.
  */
 void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
   long startTime = System.nanoTime();
   for (; ; ) {
     Thread.State s = thread.getState();
     if (s == Thread.State.BLOCKED || s == Thread.State.WAITING || s == Thread.State.TIMED_WAITING)
       return;
     else if (s == Thread.State.TERMINATED) fail("Unexpected thread termination");
     else if (millisElapsedSince(startTime) > timeoutMillis) {
       threadAssertTrue(thread.isAlive());
       return;
     }
     Thread.yield();
   }
 }
 /**
  * Returns the number of milliseconds since time given by startNanoTime, which must have been
  * previously returned from a call to {@link System.nanoTime()}.
  */
 static long millisElapsedSince(long startNanoTime) {
   return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
 }
 /** Returns a new Date instance representing a time delayMillis milliseconds in the future. */
 Date delayedDate(long delayMillis) {
   return new Date(System.currentTimeMillis() + delayMillis);
 }