org.junit.internal.runners.statements.FailOnTimeout Java Examples

The following examples show how to use org.junit.internal.runners.statements.FailOnTimeout. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidJUnit4ClassRunner.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * Default to <a href="http://junit.org/javadoc/latest/org/junit/Test.html#timeout()"><code>
 * org.junit.Test#timeout()</code></a> level timeout if set. Otherwise, set the timeout that was
 * passed to the instrumentation via argument.
 */
@Override
protected Statement withPotentialTimeout(FrameworkMethod method, Object test, Statement next) {
  // test level timeout i.e @Test(timeout = 123)
  long timeout = getTimeout(method.getAnnotation(Test.class));

  // use runner arg timeout if test level timeout is not present
  if (timeout <= 0 && androidRunnerParams.getPerTestTimeout() > 0) {
    timeout = androidRunnerParams.getPerTestTimeout();
  }

  if (timeout <= 0) {
    // no timeout was set
    return next;
  }

  // Cannot switch to use builder as that is not supported in JUnit 4.10 which is what is
  // available in AOSP.
  return new FailOnTimeout(next, timeout);
}
 
Example #2
Source File: CdiTestRunner.java    From deltaspike with Apache License 2.0 6 votes vote down vote up
@Override
protected Statement withPotentialTimeout(FrameworkMethod method, Object test, Statement next)
{
    Statement result = super.withPotentialTimeout(method, test, next);

    if (result instanceof FailOnTimeout)
    {
        return new Statement()
        {
            @Override
            public void evaluate() throws Throwable
            {
                throw new RuntimeException("@" + Test.class.getName() + "#timeout isn't supported");
            }
        };
    }

    return result;
}
 
Example #3
Source File: BCryptHighCostTest.java    From bcrypt with Apache License 2.0 5 votes vote down vote up
public Statement apply(Statement base, Description description) {
    return new FailOnTimeout(base, MIN_TIMEOUT) {
        @Override
        public void evaluate() throws Throwable {
            try {
                super.evaluate();
                throw new TimeoutException();
            } catch (Exception e) {
            }
        }
    };
}
 
Example #4
Source File: JUnitRunListener.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private static Throwable getTimeoutException() {
  final FailOnTimeout f = FailOnTimeout.builder().withTimeout(1, TimeUnit.NANOSECONDS).build(new Statement() {
    @Override
    public void evaluate() throws InterruptedException {
      Thread.sleep(1000);
    }
  });
  try {
    f.evaluate();
  } catch(Throwable throwable) {
    return throwable;
  }
  throw new IllegalStateException("Failed to getTimeoutException");
}
 
Example #5
Source File: JUnitRunListener.java    From ratis with Apache License 2.0 5 votes vote down vote up
private static Throwable getTimeoutException() {
  final FailOnTimeout f = new FailOnTimeout(new Statement() {
    @Override
    public void evaluate() throws InterruptedException {
      Thread.sleep(1000);
    }
  }, 1);
  try {
    f.evaluate();
  } catch(Throwable throwable) {
    return throwable;
  }
  return null;
}
 
Example #6
Source File: StringBenchIncubation.java    From stringbench with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(Statement base, Description description) {
       return FailOnTimeout.builder()
           .withTimeout(40, SECONDS)
           .withLookingForStuckThread(true)
           .build(base);
}
 
Example #7
Source File: UiThreadTestRule.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(final Statement base, Description description) {
  if (base instanceof FailOnTimeout
      || (base instanceof UiThreadStatement && !((UiThreadStatement) base).isRunOnUiThread())) {
    // In upstream junit code Rules Statements are handled last. Since we now handle
    // @UiThreadTest as part of the core Android runner, there is a chance that
    // UiThreadStatement was already applied on the current statement.
    // This is mainly for compatibility reasons to deprecated this rule.
    return base;
  }
  return new UiThreadStatement(base, shouldRunOnUiThread(description));
}
 
Example #8
Source File: TimeoutWrapper.java    From spectrum with MIT License 5 votes vote down vote up
/**
 * Convert the timeout into a {@link NonReportingHook} which executes
 * the inner inside a daemon thread, failing if it takes too long.
 * @param timeout duration of the timeout
 * @return hook which implements the timeout
 */
static NonReportingHook timeoutHook(Duration timeout) {
  return nonReportingHookFrom(
      (description, reporting, block) -> withAppliedTimeout(FailOnTimeout.builder(), timeout)
          .build(statementOf(block))
          .evaluate());
}
 
Example #9
Source File: QpidJMSTestRunner.java    From qpid-jms with Apache License 2.0 5 votes vote down vote up
/**
 * Perform the same logic as
 * {@link BlockJUnit4ClassRunner#withPotentialTimeout(FrameworkMethod, Object, Statement)}
 * but with additional support for changing the coded timeout with an extended value.
 *
 * @return either a {@link FailOnTimeout}, or the supplied {@link Statement} as appropriate.
 */
@SuppressWarnings("deprecation")
@Override
protected Statement withPotentialTimeout(FrameworkMethod frameworkMethod, Object testInstance, Statement next) {
    long testTimeout = getOriginalTimeout(frameworkMethod);

    if (testTimeout > 0) {
        String multiplierString = System.getProperty("org.apache.qpid.jms.testTimeoutMultiplier");
        double multiplier = 0.0;

        try {
            multiplier = Double.parseDouble(multiplierString);
        } catch (NullPointerException npe) {
        } catch (NumberFormatException nfe) {
            LOG.warn("Ignoring testTimeoutMultiplier not set to a valid value: " + multiplierString);
        }

        if (multiplier > 0.0) {
            LOG.info("Test timeout multiple {} applied to test timeout {}ms: new timeout = {}",
                multiplier, testTimeout, (long) (testTimeout * multiplier));
            testTimeout = (long) (testTimeout * multiplier);
        }

        next = FailOnTimeout.builder().
            withTimeout(testTimeout, TimeUnit.MILLISECONDS).build(next);
    } else {
        next = super.withPotentialTimeout(frameworkMethod, testInstance, next);
    }

    return next;
}
 
Example #10
Source File: LoadTimeWeavableTestRunner.java    From rice with Educational Community License v2.0 3 votes vote down vote up
/**
 * Returns a {@link org.junit.runners.model.Statement}: if {@code method}'s {@code @Test} annotation
 * has the {@code timeout} attribute, throw an exception if {@code next}
 * takes more than the specified number of milliseconds.
 *
 * @deprecated Will be private soon: use Rules instead
 */
@Deprecated
protected Statement withPotentialTimeout(FrameworkMethod method,
        Object test, Statement next) {
    long timeout = getTimeout(method.getAnnotation(Test.class));
    return timeout > 0 ? new FailOnTimeout(next, timeout) : next;
}
 
Example #11
Source File: TimeoutWrapper.java    From spectrum with MIT License 2 votes vote down vote up
/**
 * Apply a timeout expressed as a duration to a builder of a {@link FailOnTimeout} object.
 * @param builder to modify
 * @param timeout duration of the timeout
 * @return the builder input - for fluent use.
 */
static FailOnTimeout.Builder withAppliedTimeout(FailOnTimeout.Builder builder, Duration timeout) {
  builder.withTimeout(timeout.toNanos(), TimeUnit.NANOSECONDS);

  return builder;
}