Search in sources :

Example 1 with StoppedByUserException

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);
}
Also used : TimerTask(java.util.TimerTask) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) Failure(org.junit.runner.notification.Failure)

Example 2 with StoppedByUserException

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();
}
Also used : JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) CountDownLatch(java.util.concurrent.CountDownLatch) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) ExecutionException(java.util.concurrent.ExecutionException) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) Result(org.junit.runner.Result) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ExecutorService(java.util.concurrent.ExecutorService) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.Test)

Example 3 with StoppedByUserException

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);
    }
}
Also used : Description(org.junit.runner.Description) InOrder(org.mockito.InOrder) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) CancellableRequestFactory(com.google.testing.junit.runner.internal.junit4.CancellableRequestFactory) RunListener(org.junit.runner.notification.RunListener) SettableCurrentRunningTest(com.google.testing.junit.runner.internal.junit4.SettableCurrentRunningTest) CurrentRunningTest(com.google.testing.junit.runner.util.CurrentRunningTest) Test(org.junit.Test)

Example 4 with StoppedByUserException

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());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) JUnitCore(org.junit.runner.JUnitCore) Request(org.junit.runner.Request) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) Test(org.junit.Test)

Example 5 with StoppedByUserException

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();
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) EachTestNotifier(org.junit.internal.runners.model.EachTestNotifier) StoppedByUserException(org.junit.runner.notification.StoppedByUserException)

Aggregations

StoppedByUserException (org.junit.runner.notification.StoppedByUserException)5 Test (org.junit.Test)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)2 JUnitCore (org.junit.runner.JUnitCore)2 Request (org.junit.runner.Request)2 CancellableRequestFactory (com.google.testing.junit.runner.internal.junit4.CancellableRequestFactory)1 SettableCurrentRunningTest (com.google.testing.junit.runner.internal.junit4.SettableCurrentRunningTest)1 CurrentRunningTest (com.google.testing.junit.runner.util.CurrentRunningTest)1 TimerTask (java.util.TimerTask)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1 EachTestNotifier (org.junit.internal.runners.model.EachTestNotifier)1 Description (org.junit.runner.Description)1 Result (org.junit.runner.Result)1 Failure (org.junit.runner.notification.Failure)1 RunListener (org.junit.runner.notification.RunListener)1 Statement (org.junit.runners.model.Statement)1 InOrder (org.mockito.InOrder)1