public Statement apply(Statement base, Description description) { return new FailOnTimeout(base, TEST_TIMEOUT) { @Override public void evaluate() throws Throwable { try { super.evaluate(); throw new TimeoutException(); } catch(Exception ignored) { } } }; }
@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) { if (disabled()) { return base; } else { return new FailOnTimeout(base, (long) this.fMillis); } }
public Statement apply(Statement base, Description description) { if (disabled()) { return base; } else { return new FailOnTimeout(base, fMillis); } }
public Statement apply(Statement base, Description description) { if (fMillis > 0) { return new FailOnTimeout(base, fMillis); } else { return base; } }
@Override public Statement apply(Statement base, Description description) { return FailOnTimeout.builder() .withTimeout(40, SECONDS) .withLookingForStuckThread(true) .build(base); }
/** * Returns a {@link 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 protected Statement withPotentialTimeout(FrameworkMethod method, Object test, Statement next) { long timeout = getTimeout(method.getAnnotation(Test.class)); return timeout > 0 ? new FailOnTimeout(next, timeout) : next; }
@Test public void stopEndlessStatement() throws Throwable { InfiniteLoopStatement infiniteLoop = new InfiniteLoopStatement(); FailOnTimeout infiniteLoopTimeout = new FailOnTimeout(infiniteLoop, TIMEOUT); try { infiniteLoopTimeout.evaluate(); } catch (Exception timeoutException) { sleep(20); // time to interrupt the thread int firstCount = InfiniteLoopStatement.COUNT; sleep(20); // time to increment the count assertTrue("Thread has not been stopped.", firstCount == InfiniteLoopStatement.COUNT); } }
@Test public void stackTraceContainsRealCauseOfTimeout() throws Throwable { StuckStatement stuck = new StuckStatement(); FailOnTimeout stuckTimeout = new FailOnTimeout(stuck, TIMEOUT); try { stuckTimeout.evaluate(); // We must not get here, we expect a timeout exception fail("Expected timeout exception"); } catch (Exception timeoutException) { StackTraceElement[] stackTrace = timeoutException.getStackTrace(); boolean stackTraceContainsTheRealCauseOfTheTimeout = false; boolean stackTraceContainsOtherThanTheRealCauseOfTheTimeout = false; for (StackTraceElement element : stackTrace) { String methodName = element.getMethodName(); if ("theRealCauseOfTheTimeout".equals(methodName)) { stackTraceContainsTheRealCauseOfTheTimeout = true; } if ("notTheRealCauseOfTheTimeout".equals(methodName)) { stackTraceContainsOtherThanTheRealCauseOfTheTimeout = true; } } assertTrue( "Stack trace does not contain the real cause of the timeout", stackTraceContainsTheRealCauseOfTheTimeout); assertFalse( "Stack trace contains other than the real cause of the timeout, which can be very misleading", stackTraceContainsOtherThanTheRealCauseOfTheTimeout); } }
/** * 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; }
@Test public void whenTestAnnotationDoesSpecifyATimeout_returnFailOnTimeoutStatement() throws Exception { TestClass testClass = TestClassPool.forClass(SingleTestWithTimeoutAnnotation.class); FrameworkMethod method = testClass.getAnnotatedMethods(Test.class).get(0); Statement actual = builder.createStatement(testClass, method, target, next, description, notifier); assertThat(actual, is(instanceOf(FailOnTimeout.class))); }
public Statement apply(Statement base, Description description) { return new FailOnTimeout(base, fTimeout, fTimeUnit, fLookForStuckThread); }
public Statement apply(Statement base, Description description) { return new FailOnTimeout(base, fMillis); }
public Statement createStatement(final TestClass testClass, final FrameworkMethod method, final Object target, final Statement next, final Description description, final RunNotifier notifier) { final Test annotation = method.getAnnotation(Test.class); return annotation.timeout() <= 0 ? next : new FailOnTimeout(next, annotation.timeout()); }
/** * 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; }
/** * Returns a {@link 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; }