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;
}
}
};
}
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();
}
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);
}
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();
}
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;
}
Aggregations