Search in sources :

Example 41 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project robovm by robovm.

the class SSLSocketTest method test_SSLSocket_HandshakeCompletedListener_RuntimeException.

public void test_SSLSocket_HandshakeCompletedListener_RuntimeException() throws Exception {
    final Thread self = Thread.currentThread();
    final UncaughtExceptionHandler original = self.getUncaughtExceptionHandler();
    final RuntimeException expectedException = new RuntimeException("expected");
    final TestUncaughtExceptionHandler test = new TestUncaughtExceptionHandler();
    self.setUncaughtExceptionHandler(test);
    final TestSSLContext c = TestSSLContext.create();
    final SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            server.startHandshake();
            return null;
        }
    });
    executor.shutdown();
    client.addHandshakeCompletedListener(new HandshakeCompletedListener() {

        public void handshakeCompleted(HandshakeCompletedEvent event) {
            throw expectedException;
        }
    });
    client.startHandshake();
    future.get();
    client.close();
    server.close();
    c.close();
    assertSame(expectedException, test.actualException);
    self.setUncaughtExceptionHandler(original);
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) ExecutorService(java.util.concurrent.ExecutorService) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 42 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project randomizedtesting by randomizedtesting.

the class RandomizedRunner method runSuite.

/**
   * Test execution logic for the entire suite. 
   */
private void runSuite(final RunNotifier notifier) {
    // NOTE: this effectively means we can't run concurrent randomized runners.
    final UncaughtExceptionHandler previous = Thread.getDefaultUncaughtExceptionHandler();
    handler = new QueueUncaughtExceptionsHandler();
    AccessController.doPrivileged(new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            Thread.setDefaultUncaughtExceptionHandler(handler);
            return null;
        }
    });
    this.runnerThreadGroup = new RunnerThreadGroup("TGRP-" + Classes.simpleName(suiteClass));
    final Thread runner = new Thread(runnerThreadGroup, "SUITE-" + Classes.simpleName(suiteClass) + "-seed#" + SeedUtils.formatSeedChain(runnerRandomness)) {

        public void run() {
            try {
                // on the class NOT being initialized before.
                try {
                    Class.forName(suiteClass.getName(), true, suiteClass.getClassLoader());
                } catch (ExceptionInInitializerError e) {
                    throw e.getCause();
                }
                RandomizedContext context = createContext(runnerThreadGroup);
                runSuite(context, notifier);
                context.dispose();
            } catch (Throwable t) {
                notifier.fireTestFailure(new Failure(suiteDescription, t));
            }
        }
    };
    runner.start();
    try {
        runner.join();
    } catch (InterruptedException e) {
        notifier.fireTestFailure(new Failure(suiteDescription, new RuntimeException("Interrupted while waiting for the suite runner? Weird.", e)));
    }
    UncaughtExceptionHandler current = Thread.getDefaultUncaughtExceptionHandler();
    if (current != handler) {
        notifier.fireTestFailure(new Failure(suiteDescription, new RuntimeException("Suite replaced Thread.defaultUncaughtExceptionHandler. " + "It's better not to touch it. Or at least revert it to what it was before. Current: " + (current == null ? "(null)" : current.getClass()))));
    }
    AccessController.doPrivileged(new PrivilegedAction<Void>() {

        @Override
        public Void run() {
            Thread.setDefaultUncaughtExceptionHandler(previous);
            return null;
        }
    });
    runnerThreadGroup = null;
    handler = null;
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Failure(org.junit.runner.notification.Failure)

Example 43 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project voltdb by VoltDB.

the class ThreadFactoryBuilder method build.

private static ThreadFactory build(ThreadFactoryBuilder builder) {
    final String nameFormat = builder.nameFormat;
    final Boolean daemon = builder.daemon;
    final Integer priority = builder.priority;
    final UncaughtExceptionHandler uncaughtExceptionHandler = builder.uncaughtExceptionHandler;
    final ThreadFactory backingThreadFactory = (builder.backingThreadFactory != null) ? builder.backingThreadFactory : Executors.defaultThreadFactory();
    final AtomicLong count = (nameFormat != null) ? new AtomicLong(0) : null;
    return new ThreadFactory() {

        @Override
        public Thread newThread(Runnable runnable) {
            Thread thread = backingThreadFactory.newThread(runnable);
            if (nameFormat != null) {
                thread.setName(format(nameFormat, count.getAndIncrement()));
            }
            if (daemon != null) {
                thread.setDaemon(daemon);
            }
            if (priority != null) {
                thread.setPriority(priority);
            }
            if (uncaughtExceptionHandler != null) {
                thread.setUncaughtExceptionHandler(uncaughtExceptionHandler);
            }
            return thread;
        }
    };
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) AtomicLong(java.util.concurrent.atomic.AtomicLong) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 44 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project geode by apache.

the class NamedThreadFactory method newThread.

/**
   * {@inheritDoc}
   * <p/>
   * This implementation sets the name of the thread, sets the thread to be a daemon thread, and
   * adds an uncaught exception handler.
   */
@Override
public Thread newThread(Runnable r) {
    Thread thr = new Thread(r);
    thr.setDaemon(true);
    thr.setName(id + " - " + serial.incrementAndGet());
    thr.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            LOG.log(Level.WARNING, "Uncaught Exception in thread: " + t.getName(), e);
        }
    });
    return thr;
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 45 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project android_frameworks_base by ResurrectionRemix.

the class UiAutomatorTestRunner method run.

public void run(List<String> testClasses, Bundle params, boolean debug, boolean monkey) {
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            Log.e(LOGTAG, "uncaught exception", ex);
            Bundle results = new Bundle();
            results.putString("shortMsg", ex.getClass().getName());
            results.putString("longMsg", ex.getMessage());
            mWatcher.instrumentationFinished(null, 0, results);
            // bailing on uncaught exception
            System.exit(EXIT_EXCEPTION);
        }
    });
    mTestClasses = testClasses;
    mParams = params;
    mDebug = debug;
    mMonkey = monkey;
    start();
    System.exit(EXIT_OK);
}
Also used : Bundle(android.os.Bundle) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) HandlerThread(android.os.HandlerThread)

Aggregations

UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)53 Bundle (android.os.Bundle)5 HandlerThread (android.os.HandlerThread)5 IOException (java.io.IOException)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 ThreadFactory (java.util.concurrent.ThreadFactory)4 Test (org.junit.Test)4 LocalLoadingCache (com.google.common.cache.LocalCache.LocalLoadingCache)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Intent (android.content.Intent)1 Message (android.os.Message)1 PowerManager (android.os.PowerManager)1 RemoteException (android.os.RemoteException)1 ConsoleMessage (android.webkit.ConsoleMessage)1 SmartThread (com.alibaba.jstorm.utils.SmartThread)1 JCommander (com.beust.jcommander.JCommander)1 Console (com.beust.jcommander.internal.Console)1