Search in sources :

Example 6 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project dspot by STAMP-project.

the class MethodsAssertGenerator method makeFailureTest.

/**
 * Adds surrounding try/catch/fail in a failing test.
 *
 * @param test Failing test method to amplify
 * @param failure Test's failure description
 * @return New amplified test
 */
protected CtMethod<?> makeFailureTest(CtMethod<?> test, Failure failure) {
    CtMethod cloneMethodTest = AmplificationHelper.cloneTestMethodForAmp(test, "");
    cloneMethodTest.setSimpleName(test.getSimpleName());
    Factory factory = cloneMethodTest.getFactory();
    Throwable exception = failure.getException();
    if (// TestTimedOutException means infinite loop
    exception instanceof TestTimedOutException || exception instanceof AssertionError) {
        // AssertionError means that some assertion remained in the test: TODO
        return null;
    }
    Class exceptionClass;
    if (exception == null) {
        exceptionClass = Exception.class;
    } else {
        exceptionClass = exception.getClass();
    }
    CtTry tryBlock = factory.Core().createTry();
    tryBlock.setBody(cloneMethodTest.getBody());
    String snippet = "org.junit.Assert.fail(\"" + test.getSimpleName() + " should have thrown " + exceptionClass.getSimpleName() + "\")";
    tryBlock.getBody().addStatement(factory.Code().createCodeSnippetStatement(snippet));
    DSpotUtils.addComment(tryBlock, "AssertGenerator generate try/catch block with fail statement", CtComment.CommentType.INLINE);
    CtCatch ctCatch = factory.Core().createCatch();
    CtTypeReference exceptionType = factory.Type().createReference(exceptionClass);
    ctCatch.setParameter(factory.Code().createCatchVariable(exceptionType, "eee"));
    ctCatch.setBody(factory.Core().createBlock());
    List<CtCatch> catchers = new ArrayList<>(1);
    catchers.add(ctCatch);
    tryBlock.setCatchers(catchers);
    CtBlock body = factory.Core().createBlock();
    body.addStatement(tryBlock);
    cloneMethodTest.setBody(body);
    cloneMethodTest.setSimpleName(cloneMethodTest.getSimpleName() + "_failAssert" + (numberOfFail++));
    Counter.updateAssertionOf(cloneMethodTest, 1);
    return cloneMethodTest;
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) Factory(spoon.reflect.factory.Factory) CtTypeReference(spoon.reflect.reference.CtTypeReference) TestTimedOutException(org.junit.runners.model.TestTimedOutException) CtMethod(spoon.reflect.declaration.CtMethod)

Example 7 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project indy by Commonjava.

the class ThreadDumper method timeoutRule.

public static TestRule timeoutRule(int timeout, TimeUnit units) {
    return (base, description) -> new Statement() {

        public void evaluate() throws Throwable {
            System.out.printf("Setting up timeout: %d %s to wrap: %s\n", timeout, units, base);
            AtomicReference<Throwable> error = new AtomicReference<>();
            CountDownLatch latch = new CountDownLatch(1);
            FutureTask<Void> task = new FutureTask<>(() -> {
                try {
                    latch.countDown();
                    base.evaluate();
                } catch (Throwable t) {
                    error.set(t);
                }
                return null;
            });
            ThreadGroup tg = new ThreadGroup("Test Timeout Group");
            Thread t = new Thread(tg, task, "Test Timeout Thread");
            t.setDaemon(true);
            t.start();
            try {
                System.out.println("Waiting for test to start.");
                latch.await();
            } catch (InterruptedException e) {
                error.set(e);
            }
            if (error.get() == null) {
                try {
                    System.out.println("Waiting for test to complete (or timeout)");
                    task.get(timeout, units);
                } catch (InterruptedException e) {
                    error.set(e);
                } catch (ExecutionException e) {
                    error.set(e.getCause());
                } catch (TimeoutException e) {
                    System.out.printf("Test timeout %d %s expired!\n", timeout, units.name());
                    dumpThreads();
                    StackTraceElement[] stackTrace = t.getStackTrace();
                    Exception currThreadException = new TestTimedOutException(timeout, units);
                    if (stackTrace != null) {
                        currThreadException.setStackTrace(stackTrace);
                        t.interrupt();
                    }
                    throw currThreadException;
                }
            }
            Throwable throwable = error.get();
            if (throwable != null) {
                throw throwable;
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) TestRule(org.junit.rules.TestRule) MonitorInfo(java.lang.management.MonitorInfo) TestTimedOutException(org.junit.runners.model.TestTimedOutException) FutureTask(java.util.concurrent.FutureTask) TimeoutException(java.util.concurrent.TimeoutException) ThreadMXBean(java.lang.management.ThreadMXBean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) StringUtils.join(org.apache.commons.lang3.StringUtils.join) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadInfo(java.lang.management.ThreadInfo) Stream(java.util.stream.Stream) ManagementFactory(java.lang.management.ManagementFactory) Statement(org.junit.runners.model.Statement) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) FutureTask(java.util.concurrent.FutureTask) ExecutionException(java.util.concurrent.ExecutionException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 8 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project junit4 by junit-team.

the class FailOnTimeout method createTimeoutException.

private Exception createTimeoutException(Thread thread) {
    StackTraceElement[] stackTrace = thread.getStackTrace();
    final Thread stuckThread = lookForStuckThread ? getStuckThread(thread) : null;
    Exception currThreadException = new TestTimedOutException(timeout, timeUnit);
    if (stackTrace != null) {
        currThreadException.setStackTrace(stackTrace);
        thread.interrupt();
    }
    if (stuckThread != null) {
        Exception stuckThreadException = new Exception("Appears to be stuck in thread " + stuckThread.getName());
        stuckThreadException.setStackTrace(getStackTrace(stuckThread));
        return new MultipleFailureException(Arrays.<Throwable>asList(currThreadException, stuckThreadException));
    } else {
        return currThreadException;
    }
}
Also used : MultipleFailureException(org.junit.runners.model.MultipleFailureException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TestTimedOutException(org.junit.runners.model.TestTimedOutException) TimeoutException(java.util.concurrent.TimeoutException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project junit4 by junit-team.

the class FailOnTimeoutTest method stackTraceContainsRealCauseOfTimeout.

@Test
public void stackTraceContainsRealCauseOfTimeout() {
    TestTimedOutException timedOutException = assertThrows(TestTimedOutException.class, run(failAfter50Ms(new StuckStatement())));
    StackTraceElement[] stackTrace = timedOutException.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);
}
Also used : TestTimedOutException(org.junit.runners.model.TestTimedOutException) Test(org.junit.Test)

Example 10 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project junit4 by junit-team.

the class FailOnTimeoutTest method throwsExceptionWithTimeoutValueAndTimeUnitSet.

@Test
public void throwsExceptionWithTimeoutValueAndTimeUnitSet() {
    TestTimedOutException e = assertThrows(TestTimedOutException.class, run(failAfter50Ms(new RunForASecond())));
    assertEquals(50, e.getTimeout());
    assertEquals(MILLISECONDS, e.getTimeUnit());
}
Also used : TestTimedOutException(org.junit.runners.model.TestTimedOutException) Test(org.junit.Test)

Aggregations

TestTimedOutException (org.junit.runners.model.TestTimedOutException)19 ExecutionException (java.util.concurrent.ExecutionException)8 TimeoutException (java.util.concurrent.TimeoutException)8 Test (org.junit.Test)5 ManagementFactory (java.lang.management.ManagementFactory)4 MonitorInfo (java.lang.management.MonitorInfo)4 ThreadInfo (java.lang.management.ThreadInfo)4 ThreadMXBean (java.lang.management.ThreadMXBean)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 FutureTask (java.util.concurrent.FutureTask)4 TimeUnit (java.util.concurrent.TimeUnit)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Stream (java.util.stream.Stream)4 TestRule (org.junit.rules.TestRule)4 Statement (org.junit.runners.model.Statement)4 Failure (org.junit.runner.notification.Failure)3 MultipleFailureException (org.junit.runners.model.MultipleFailureException)3 StringUtils.join (org.apache.commons.lang.StringUtils.join)2 StringUtils.join (org.apache.commons.lang3.StringUtils.join)2 ThreadInterruptedError (org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError)2