private Exception createTimeoutException(Thread thread) { StackTraceElement[] stackTrace = thread.getStackTrace(); final Thread stuckThread = fLookForStuckThread ? getStuckThread(thread) : null; Exception currThreadException = new TestTimedOutException(fTimeout, fTimeUnit); 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; } }
@Override protected void failed(Throwable e, Description description) { if (e instanceof TestTimedOutException) { System.out.println(threadDump()); } super.failed(e, description); }
@Test public void notifies_failure_when_example_times_out() throws InitializationError { J8SpecRunner runner = new J8SpecRunner(SampleSpec.class); List<Example> examples = runner.getChildren(); RunNotifier runNotifier = new RunNotifier(); RunListenerHelper listener = new RunListenerHelper(); runNotifier.addListener(listener); runner.runChild(examples.get(6), runNotifier); assertThat(listener.getDescription(), is(runner.describeChild(examples.get(6)))); assertThat(listener.getException(), instanceOf(TestTimedOutException.class)); }
@Test public void throwExceptionIfTheSecondCallToEvaluateNeedsTooMuchTime() throws Throwable { thrown.expect(TestTimedOutException.class); evaluateWithWaitDuration(0); evaluateWithWaitDuration(TIMEOUT + 50); }
@Test public void throwsExceptionWithTimeoutValueAndTimeUnitSet() throws Throwable { try { evaluateWithWaitDuration(TIMEOUT + 50); fail("No exception was thrown when test timed out"); } catch (TestTimedOutException e) { assertEquals(TIMEOUT, e.getTimeout()); assertEquals(TimeUnit.MILLISECONDS, e.getTimeUnit()); } }
protected CtMethod<?> makeFailureTest(CtMethod<?> test, Failure failure) { CtMethod cloneMethodTest = AmplificationHelper.cloneMethodTest(test, ""); cloneMethodTest.setSimpleName(test.getSimpleName()); Factory factory = cloneMethodTest.getFactory(); Throwable exception = failure.getException(); if (exception instanceof TestTimedOutException || // TestTimedOutException means infinite loop 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); AmplificationHelper.getAmpTestToParent().put(cloneMethodTest, test); return cloneMethodTest; }
@Test public void throwsTestTimedOutException() throws Throwable { thrown.expect(TestTimedOutException.class); evaluateWithWaitDuration(TIMEOUT + 50); }
@Test(timeout = 1000) public void testCliAgainstSslEtcd() throws URISyntaxException, IOException, EtcdAuthenticationException, TimeoutException, EtcdException { expectedEx.expect(TestTimedOutException.class); EtcdClient etcd = new EtcdClient(new URI(SECURED_ETCD_SERVICE)); etcd.put("/test", "1234").send().get(); }