/** Sleeps until the given time has elapsed. Throws AssertionFailedError if interrupted. */
 void sleep(long millis) {
   try {
     delay(millis);
   } catch (InterruptedException ie) {
     AssertionFailedError afe = new AssertionFailedError("Unexpected InterruptedException");
     afe.initCause(ie);
     throw afe;
   }
 }
 /**
  * Records the given exception using {@link #threadRecordFailure}, then rethrows the exception,
  * wrapping it in an AssertionFailedError if necessary.
  */
 public void threadUnexpectedException(Throwable t) {
   threadRecordFailure(t);
   t.printStackTrace();
   if (t instanceof RuntimeException) throw (RuntimeException) t;
   else if (t instanceof Error) throw (Error) t;
   else {
     AssertionFailedError afe = new AssertionFailedError("unexpected exception: " + t);
     afe.initCause(t);
     throw afe;
   }
 }
 public int await() {
   try {
     return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
   } catch (TimeoutException e) {
     throw new AssertionFailedError("timed out");
   } catch (Exception e) {
     AssertionFailedError afe = new AssertionFailedError("Unexpected exception: " + e);
     afe.initCause(e);
     throw afe;
   }
 }
  /**
   * Extra checks that get done for all test cases.
   *
   * <p>Triggers test case failure if any thread assertions have failed, by rethrowing, in the test
   * harness thread, any exception recorded earlier by threadRecordFailure.
   *
   * <p>Triggers test case failure if interrupt status is set in the main thread.
   */
  public void tearDown() throws Exception {
    Throwable t = threadFailure.getAndSet(null);
    if (t != null) {
      if (t instanceof Error) throw (Error) t;
      else if (t instanceof RuntimeException) throw (RuntimeException) t;
      else if (t instanceof Exception) throw (Exception) t;
      else {
        AssertionFailedError afe = new AssertionFailedError(t.toString());
        afe.initCause(t);
        throw afe;
      }
    }

    if (Thread.interrupted()) throw new AssertionFailedError("interrupt status set in main thread");
  }