Java Code Examples for org.junit.Test#timeout()

The following examples show how to use org.junit.Test#timeout() . 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: BuckBlockJUnit4ClassRunner.java    From buck with Apache License 2.0 6 votes vote down vote up
private long getTimeout(FrameworkMethod method) {
  // Check to see if the method has declared a timeout on the @Test annotation. If that's present
  // and set, then let JUnit handle the timeout for us, which (counter-intuitively) means letting
  // the test run indefinitely.
  Test annotation = method.getMethod().getAnnotation(Test.class);
  if (annotation != null) {
    long timeout = annotation.timeout();
    if (timeout != 0) { // 0 represents the default timeout
      return Long.MAX_VALUE;
    }
  }

  if (!isNeedingCustomTimeout()) {
    return defaultTestTimeoutMillis;
  }
  return Long.MAX_VALUE;
}
 
Example 2
Source File: DelegateRunNotifier.java    From buck with Apache License 2.0 6 votes vote down vote up
boolean hasJunitTimeout(Description description) {
  // Do not do apply the default timeout if the test has its own @Test(timeout).
  Test testAnnotation = description.getAnnotation(Test.class);
  if (testAnnotation != null && testAnnotation.timeout() > 0) {
    return true;
  }

  // Do not do apply the default timeout if the test has its own @Rule Timeout.
  if (runner instanceof ParentRunner) {
    return BuckBlockJUnit4ClassRunner.hasTimeoutRule(((ParentRunner) runner).getTestClass());
  }

  Class<?> clazz = description.getTestClass();
  while (clazz != null) {
    for (Field field : clazz.getFields()) {
      if (field.getAnnotationsByType(Rule.class).length > 0
          && field.getType().equals(Timeout.class)) {
        return true;
      }
    }

    clazz = clazz.getSuperclass();
  }
  return false;
}
 
Example 3
Source File: ServiceTalkTestTimeout.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Override
public Statement apply(Statement base, Description description) {
    // Check if multiple Timeout are present and annotated with @Rule.
    Class<?> clazz = description.getTestClass();
    List<Class<?>> timeoutRuleClasses = new ArrayList<>(2);
    do {
        for (Field field : clazz.getDeclaredFields()) {
            if (field.isAnnotationPresent(Rule.class) && Timeout.class.isAssignableFrom(field.getType())) {
                timeoutRuleClasses.add(clazz);
            }
        }
    } while ((clazz = clazz.getSuperclass()) != Object.class);
    if (timeoutRuleClasses.size() > 1) {
        StringBuilder sb = new StringBuilder(256)
                .append("Only one @Rule for a Timeout is allowed, but ")
                .append(timeoutRuleClasses.size())
                .append(" were detected in types: ");
        for (Class<?> clazz2 : timeoutRuleClasses) {
            sb.append(clazz2.getName()).append(", ");
        }
        sb.setLength(sb.length() - 2);
        throw new IllegalStateException(sb.toString());
    }
    // If timeout is specified in @Test, let that have precedence over the global timeout.
    Test testAnnotation = description.getAnnotation(Test.class);
    if (testAnnotation != null) {
        long timeout = testAnnotation.timeout();
        if (timeout > 0) {
            return new TimeoutStatement(base, timeout, TimeUnit.MILLISECONDS, onTimeout);
        }
    }
    return new TimeoutStatement(base, getTimeout(TimeUnit.MILLISECONDS), TimeUnit.MILLISECONDS, onTimeout);
}
 
Example 4
Source File: JUnit4Validator.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private boolean validateAsync(ExecutableElement executableElement) {
  Test testAnnotation = executableElement.getAnnotation(Test.class);
  Timeout timeoutAnnotation = executableElement.getAnnotation(Timeout.class);
  long timeout =
      testAnnotation != null
          ? testAnnotation.timeout()
          : timeoutAnnotation != null ? timeoutAnnotation.value() : 0;

  boolean isValid = true;

  if (testAnnotation != null && timeoutAnnotation != null) {
    errorReporter.report(ErrorMessage.TEST_HAS_TIMEOUT_ANNOTATION, executableElement);
    isValid = false;
  }

  if (TestingPredicates.IS_RETURNTYPE_A_THENABLE.test(executableElement)) {
    // if we are an async test, we need the timeout attribute
    if (timeout <= 0L) {
      errorReporter.report(ErrorMessage.ASYNC_HAS_NO_TIMEOUT, executableElement);
      isValid = false;
    }
    // block usage of expected exception with async tests
    if (MoreApt.getClassNameFromAnnotation(executableElement, Test.class, "expected")
        .isPresent()) {
      errorReporter.report(ErrorMessage.ASYNC_HAS_EXPECTED_EXCEPTION, executableElement);
      isValid = false;
    }
  } else {
    // block usage of timeout with non async tests
    if (timeout != 0L) {
      errorReporter.report(ErrorMessage.NON_ASYNC_HAS_TIMEOUT, executableElement);
      isValid = false;
    }
    if (!TestingPredicates.RETURN_TYPE_VOID_PREDICATE.test(executableElement)) {
      errorReporter.report(ErrorMessage.NON_PROMISE_RETURN, executableElement);
      isValid = false;
    }
  }
  return isValid;
}
 
Example 5
Source File: JUnit4TestDataExtractor.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static long getTimeout(Element method) {
  Test testAnnotation = method.getAnnotation(Test.class);
  Timeout timeoutAnnotation = method.getAnnotation(Timeout.class);
  return testAnnotation != null
      ? testAnnotation.timeout()
      : timeoutAnnotation != null ? timeoutAnnotation.value() : 0;
}
 
Example 6
Source File: AsyncTestRunner.java    From j2cl with Apache License 2.0 5 votes vote down vote up
private static long getTimeout(FrameworkMethod testMethod) {
  Test testAnnotation = testMethod.getAnnotation(Test.class);
  Timeout timeoutAnnotation = testMethod.getAnnotation(Timeout.class);
  return testAnnotation != null
      ? testAnnotation.timeout()
      : timeoutAnnotation != null ? timeoutAnnotation.value() : 0;
}
 
Example 7
Source File: VertxUnitRunner.java    From vertx-unit with Apache License 2.0 5 votes vote down vote up
private long getTimeout(FrameworkMethod fMethod) {
  long timeout = 2 * 60 * 1000L;
  if (timeoutStack.size() > 0) {
    timeout = timeoutStack.peekLast();
  }
  Test annotation = fMethod.getAnnotation(Test.class);
  if (annotation != null && annotation.timeout() > 0) {
    timeout = annotation.timeout();
  }
  return timeout;
}
 
Example 8
Source File: AndroidJUnit4ClassRunner.java    From android-test with Apache License 2.0 4 votes vote down vote up
private long getTimeout(Test annotation) {
  if (annotation == null) {
    return 0;
  }
  return annotation.timeout();
}
 
Example 9
Source File: LoadTimeWeavableTestRunner.java    From rice with Educational Community License v2.0 4 votes vote down vote up
private long getTimeout(Test annotation) {
    if (annotation == null) {
        return 0;
    }
    return annotation.timeout();
}
 
Example 10
Source File: SpringJUnit4ParameterizedClassRunner.java    From tds with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * Retrieves the configured JUnit <code>timeout</code> from the {@link Test
 * &#064;Test} annotation on the supplied {@link FrameworkMethod test
 * method}.
 *
 * @return the timeout, or <code>0</code> if none was specified.
 */
protected long getJUnitTimeout(FrameworkMethod frameworkMethod) {
  Test testAnnotation = frameworkMethod.getAnnotation(Test.class);
  return (testAnnotation != null && testAnnotation.timeout() > 0 ? testAnnotation.timeout() : 0);
}
 
Example 11
Source File: SpringJUnit4ClassRunner.java    From java-technology-stack with MIT License 2 votes vote down vote up
/**
 * Retrieve the configured JUnit {@code timeout} from the {@link Test @Test}
 * annotation on the supplied {@linkplain FrameworkMethod test method}.
 * @return the timeout, or {@code 0} if none was specified
 */
protected long getJUnitTimeout(FrameworkMethod frameworkMethod) {
	Test test = frameworkMethod.getAnnotation(Test.class);
	return (test != null && test.timeout() > 0 ? test.timeout() : 0);
}
 
Example 12
Source File: SpringJUnit4ClassRunner.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieve the configured JUnit {@code timeout} from the {@link Test @Test}
 * annotation on the supplied {@linkplain FrameworkMethod test method}.
 * @return the timeout, or {@code 0} if none was specified
 */
protected long getJUnitTimeout(FrameworkMethod frameworkMethod) {
	Test test = frameworkMethod.getAnnotation(Test.class);
	return (test != null && test.timeout() > 0 ? test.timeout() : 0);
}
 
Example 13
Source File: QpidJMSTestRunner.java    From qpid-jms with Apache License 2.0 2 votes vote down vote up
/**
 * Retrieve the original JUnit {@code timeout} from the {@link Test @Test}
 * annotation on the incoming {@linkplain FrameworkMethod test method}.
 *
 * @return the timeout, or {@code 0} if none was specified
 */
protected long getOriginalTimeout(FrameworkMethod frameworkMethod) {
    Test test = frameworkMethod.getAnnotation(Test.class);
    return (test != null && test.timeout() > 0 ? test.timeout() : 0);
}