Search in sources :

Example 1 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) StringUtils.join(org.apache.commons.lang.StringUtils.join) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) 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 2 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project scout.rt by eclipse.

the class TimeoutClientRunContextStatement method evaluate.

@Override
public void evaluate() throws Throwable {
    final SafeStatementInvoker invoker = new SafeStatementInvoker(m_next);
    final IFuture<Void> future = ModelJobs.schedule(invoker, ModelJobs.newInput(ClientRunContexts.copyCurrent()).withName("Running test with support for JUnit timeout"));
    try {
        if (m_timeoutMillis <= 0) {
            future.awaitDone();
        } else {
            future.awaitDone(m_timeoutMillis, TimeUnit.MILLISECONDS);
        }
    } catch (ThreadInterruptedError | TimedOutError e) {
        // NOSONAR
        future.cancel(true);
        // JUnit timeout exception
        throw new TestTimedOutException(m_timeoutMillis, TimeUnit.MILLISECONDS);
    }
    invoker.throwOnError();
}
Also used : SafeStatementInvoker(org.eclipse.scout.rt.testing.platform.runner.SafeStatementInvoker) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) TestTimedOutException(org.junit.runners.model.TestTimedOutException)

Example 3 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project scout.rt by eclipse.

the class ClientTestRunnerTimeoutTest method testTimeoutExceeded.

@Test
public void testTimeoutExceeded() throws Exception {
    Result result = runClientTestRunner(ClientTestRunnerTimeoutTestFixture.class, m_name.getMethodName(), 1);
    Failure f = result.getFailures().get(0);
    assertNotNull(f);
    assertTrue(f.getException() instanceof TestTimedOutException);
}
Also used : TestTimedOutException(org.junit.runners.model.TestTimedOutException) Failure(org.junit.runner.notification.Failure) Result(org.junit.runner.Result) Test(org.junit.Test)

Example 4 with TestTimedOutException

use of org.junit.runners.model.TestTimedOutException in project scout.rt by eclipse.

the class TimeoutRunContextStatement method evaluate.

@Override
public void evaluate() throws Throwable {
    final SafeStatementInvoker invoker = new SafeStatementInvoker(m_next);
    final IFuture<Void> future = Jobs.schedule(invoker, Jobs.newInput().withRunContext(// Run in new TX, because the same TX is not allowed to be used by multiple threads.
    RunContext.CURRENT.get().copy().withTransactionScope(TransactionScope.REQUIRES_NEW)).withName("Running test with support for JUnit timeout"));
    try {
        future.awaitDone(m_timeoutMillis, TimeUnit.MILLISECONDS);
    } catch (ThreadInterruptedError | TimedOutError e) {
        // NOSONAR
        future.cancel(true);
        // JUnit timeout exception
        throw new TestTimedOutException(m_timeoutMillis, TimeUnit.MILLISECONDS);
    }
    invoker.throwOnError();
}
Also used : SafeStatementInvoker(org.eclipse.scout.rt.testing.platform.runner.SafeStatementInvoker) ThreadInterruptedError(org.eclipse.scout.rt.platform.util.concurrent.ThreadInterruptedError) TimedOutError(org.eclipse.scout.rt.platform.util.concurrent.TimedOutError) TestTimedOutException(org.junit.runners.model.TestTimedOutException)

Example 5 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)

Aggregations

TestTimedOutException (org.junit.runners.model.TestTimedOutException)17 ExecutionException (java.util.concurrent.ExecutionException)7 TimeoutException (java.util.concurrent.TimeoutException)7 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 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 TimedOutError (org.eclipse.scout.rt.platform.util.concurrent.TimedOutError)2