Search in sources :

Example 1 with ThreadLeakGroup

use of com.carrotsearch.randomizedtesting.annotations.ThreadLeakGroup in project randomizedtesting by randomizedtesting.

the class ThreadLeakControl method forSuite.

/**
   * A {@link Statement} for wrapping suite-level execution. 
   */
Statement forSuite(final Statement s, final Description suiteDescription) {
    final Class<?> suiteClass = RandomizedContext.current().getTargetClass();
    final int timeout = determineTimeout(suiteClass);
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            checkZombies();
            threadLeakGroup = firstAnnotated(ThreadLeakGroup.class, suiteClass, DefaultAnnotationValues.class);
            final List<Throwable> errors = new ArrayList<Throwable>();
            suiteFilters = instantiateFilters(errors, suiteClass);
            MultipleFailureException.assertEmpty(errors);
            final StatementRunner sr = new StatementRunner(s);
            final boolean timedOut = forkTimeoutingTask(sr, timeout, errors);
            synchronized (notifierLock) {
                if (timedOut) {
                    // Mark as timed out so that we don't do any checks in any currently running test
                    suiteTimedOut.set(true);
                    // Flush streams so that we don't get warning outputs before sysout buffers.
                    flushStreams();
                    // Emit a warning.
                    LOGGER.warning("Suite execution timed out: " + suiteDescription + formatThreadStacksFull());
                    // mark subNotifier as dead (no longer passing events).
                    subNotifier.pleaseStop();
                }
            }
            if (timedOut) {
                // complete subNotifier state in case in the middle of a test.
                if (subNotifier.testInProgress != null) {
                    targetNotifier.fireTestFailure(new Failure(subNotifier.testInProgress, RandomizedRunner.augmentStackTrace(emptyStack(new Exception("Test abandoned because suite timeout was reached.")))));
                    targetNotifier.fireTestFinished(subNotifier.testInProgress);
                }
                // throw suite failure (timeout).
                errors.add(RandomizedRunner.augmentStackTrace(emptyStack(new Exception("Suite timeout exceeded (>= " + timeout + " msec)."))));
            }
            final AnnotatedElement[] chain = { suiteClass, DefaultAnnotationValues.class };
            List<Throwable> threadLeakErrors = timedOut ? new ArrayList<Throwable>() : errors;
            checkThreadLeaks(refilter(expectedSuiteState, suiteFilters), threadLeakErrors, LifecycleScope.SUITE, suiteDescription, chain);
            processUncaught(errors, runner.handler.getUncaughtAndClear());
            MultipleFailureException.assertEmpty(errors);
        }

        @SuppressForbidden("Legitimate use of syserr.")
        private void flushStreams() {
            System.out.flush();
            System.err.flush();
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList) AnnotatedElement(java.lang.reflect.AnnotatedElement) ThreadLeakGroup(com.carrotsearch.randomizedtesting.annotations.ThreadLeakGroup) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) UncaughtException(com.carrotsearch.randomizedtesting.RandomizedRunner.UncaughtException) Failure(org.junit.runner.notification.Failure)

Aggregations

UncaughtException (com.carrotsearch.randomizedtesting.RandomizedRunner.UncaughtException)1 ThreadLeakGroup (com.carrotsearch.randomizedtesting.annotations.ThreadLeakGroup)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 ArrayList (java.util.ArrayList)1 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)1 Failure (org.junit.runner.notification.Failure)1 StoppedByUserException (org.junit.runner.notification.StoppedByUserException)1 MultipleFailureException (org.junit.runners.model.MultipleFailureException)1 Statement (org.junit.runners.model.Statement)1