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