下面列出了org.springframework.test.context.junit4.statements.SpringFailOnTimeout#org.junit.internal.runners.statements.FailOnTimeout 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
@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;
}
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) {
}
}
};
}
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");
}
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;
}
@Override
public Statement apply(Statement base, Description description) {
return FailOnTimeout.builder()
.withTimeout(40, SECONDS)
.withLookingForStuckThread(true)
.build(base);
}
@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));
}
/**
* 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());
}
/**
* 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;
}
/**
* 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;
}
/**
* 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;
}