use of org.junit.runner.notification.StoppedByUserException in project buck by facebook.
the class DelegateRunNotifier method fireTestStarted.
@Override
public void fireTestStarted(final Description description) throws StoppedByUserException {
delegate.fireTestStarted(description);
// Do not do apply the default timeout if the test has its own @Test(timeout).
if (hasJunitTimeout(description)) {
return;
}
// Schedule a timer that verifies that the test completed within the specified timeout.
TimerTask task = new TimerTask() {
@Override
public void run() {
synchronized (finishedTests) {
// If the test already finished, then do nothing.
if (finishedTests.contains(description)) {
return;
}
hasTestThatExceededTimeout.set(true);
// Should report the failure. The Exception is modeled after the one created by
// org.junit.internal.runners.statements.FailOnTimeout#createTimeoutException(Thread).
Exception exception = new Exception(String.format("test timed out after %d milliseconds", defaultTestTimeoutMillis));
Failure failure = new Failure(description, exception);
fireTestFailure(failure);
fireTestFinished(description);
if (!finishedTests.contains(description)) {
throw new IllegalStateException("fireTestFinished() should update finishedTests.");
}
onTestRunFinished();
}
}
};
timer.schedule(task, defaultTestTimeoutMillis);
}
use of org.junit.runner.notification.StoppedByUserException in project bazel by bazelbuild.
the class CancellableRequestFactoryTest method testCancelRunAfterStarting.
@Test
public void testCancelRunAfterStarting() throws Exception {
final CountDownLatch testStartLatch = new CountDownLatch(1);
final CountDownLatch testContinueLatch = new CountDownLatch(1);
final AtomicBoolean secondTestRan = new AtomicBoolean(false);
// Simulates a test that hangs
FakeRunner blockingRunner = new FakeRunner("blocks", new Runnable() {
@Override
public void run() {
testStartLatch.countDown();
try {
testContinueLatch.await(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Timed out waiting for signal to continue test", e);
}
}
});
// A runner that should never run its test
FakeRunner secondRunner = new FakeRunner("shouldNotRun", new Runnable() {
@Override
public void run() {
secondTestRan.set(true);
}
});
RunnerSuite fakeSuite = new RunnerSuite(blockingRunner, secondRunner);
final Request request = cancellableRequestFactory.createRequest(Request.runner(fakeSuite));
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<Result> future = executor.submit(new Callable<Result>() {
@Override
public Result call() throws Exception {
JUnitCore core = new JUnitCore();
return core.run(request);
}
});
// Simulate cancel being called in the middle of the test
testStartLatch.await(1, TimeUnit.SECONDS);
cancellableRequestFactory.cancelRun();
testContinueLatch.countDown();
try {
future.get(10, TimeUnit.SECONDS);
fail("exception expected");
} catch (ExecutionException e) {
Throwable runnerException = e.getCause();
assertTrue(runnerException instanceof RuntimeException);
assertEquals("Test run interrupted", runnerException.getMessage());
assertTrue(runnerException.getCause() instanceof StoppedByUserException);
}
executor.shutdownNow();
}
use of org.junit.runner.notification.StoppedByUserException in project bazel by bazelbuild.
the class JUnit4RunnerTest method testInterruptedTest.
@Test
public void testInterruptedTest() throws Exception {
config = createConfig();
mockRunListener = mock(RunListener.class);
JUnit4BazelMock component = createComponent(SampleSuite.class);
JUnit4Runner runner = component.runner();
final CancellableRequestFactory requestFactory = component.cancellableRequestFactory();
Description testDescription = Description.createTestDescription(SamplePassingTest.class, "testThatAlwaysPasses");
doAnswer(cancelTestRun(requestFactory)).when(mockRunListener).testStarted(testDescription);
try {
runner.run();
fail("exception expected");
} catch (RuntimeException e) {
assertEquals("Test run interrupted", e.getMessage());
assertTrue("Expected cause to be a StoppedByUserException", e.getCause() instanceof StoppedByUserException);
InOrder inOrder = inOrder(mockRunListener);
inOrder.verify(mockRunListener).testRunStarted(any(Description.class));
inOrder.verify(mockRunListener).testStarted(testDescription);
inOrder.verify(mockRunListener).testFinished(testDescription);
}
}
use of org.junit.runner.notification.StoppedByUserException in project bazel by bazelbuild.
the class CancellableRequestFactoryTest method testCancelRunBeforeStarting.
@Test
public void testCancelRunBeforeStarting() throws Exception {
final AtomicBoolean testRan = new AtomicBoolean(false);
// A runner that should never run its test
FakeRunner runner = new FakeRunner("shouldNotRun", new Runnable() {
@Override
public void run() {
testRan.set(true);
}
});
Request request = cancellableRequestFactory.createRequest(Request.runner(runner));
cancellableRequestFactory.cancelRun();
JUnitCore core = new JUnitCore();
try {
core.run(request);
fail("exception expected");
} catch (RuntimeException e) {
assertEquals("Test run interrupted", e.getMessage());
assertTrue(e.getCause() instanceof StoppedByUserException);
}
assertFalse(testRan.get());
}
use of org.junit.runner.notification.StoppedByUserException in project junit4 by junit-team.
the class ParentRunner method run.
@Override
public void run(final RunNotifier notifier) {
EachTestNotifier testNotifier = new EachTestNotifier(notifier, getDescription());
testNotifier.fireTestSuiteStarted();
try {
Statement statement = classBlock(notifier);
statement.evaluate();
} catch (AssumptionViolatedException e) {
testNotifier.addFailedAssumption(e);
} catch (StoppedByUserException e) {
throw e;
} catch (Throwable e) {
testNotifier.addFailure(e);
} finally {
testNotifier.fireTestSuiteFinished();
}
}
Aggregations