Пример #1
0
  /**
   * 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);
      }
    }
  }
Пример #2
0
 /**
  * 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;
   }
 }
Пример #3
0
  /** 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);
  }
Пример #4
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);
   }
 }
Пример #5
0
 /** 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);
 }
Пример #6
0
 /**
  * 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();
   }
 }
Пример #7
0
 /**
  * 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);
 }
Пример #8
0
 /** Returns a new Date instance representing a time delayMillis milliseconds in the future. */
 Date delayedDate(long delayMillis) {
   return new Date(System.currentTimeMillis() + delayMillis);
 }