Search in sources :

Example 1 with UncaughtExceptionHandler

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

the class LayoutTestsExecutor method onCreate.

/** IMPLEMENTATION */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /**
         * It detects the crash by catching all the uncaught exceptions. However, we
         * still have to kill the process, because after catching the exception the
         * activity remains in a strange state, where intents don't revive it.
         * However, we send the message to the service to speed up the rebooting
         * (we don't have to wait for time-out to kick in).
         */
    Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable e) {
            Log.w(LOG_TAG, "onTestCrashed(): " + mCurrentTestRelativePath + " thread=" + thread, e);
            try {
                Message serviceMsg = Message.obtain(null, ManagerService.MSG_CURRENT_TEST_CRASHED);
                mManagerServiceMessenger.send(serviceMsg);
            } catch (RemoteException e2) {
                Log.e(LOG_TAG, "mCurrentTestRelativePath=" + mCurrentTestRelativePath, e2);
            }
            Process.killProcess(Process.myPid());
        }
    });
    requestWindowFeature(Window.FEATURE_PROGRESS);
    Intent intent = getIntent();
    mTestsList = FsUtils.loadTestListFromStorage(intent.getStringExtra(EXTRA_TESTS_FILE));
    mCurrentTestIndex = intent.getIntExtra(EXTRA_TEST_INDEX, -1);
    mTotalTestCount = mCurrentTestIndex + mTestsList.size();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mScreenDimLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "WakeLock in LayoutTester");
    mScreenDimLock.acquire();
    bindService(new Intent(this, ManagerService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
}
Also used : PowerManager(android.os.PowerManager) ConsoleMessage(android.webkit.ConsoleMessage) Message(android.os.Message) Intent(android.content.Intent) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) RemoteException(android.os.RemoteException)

Example 2 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project RxAndroid by ReactiveX.

the class HandlerSchedulerTest method throwingActionRoutedToHookAndThreadHandler.

@Test
public void throwingActionRoutedToHookAndThreadHandler() {
    // TODO Test hook as well. Requires https://github.com/ReactiveX/RxJava/pull/3820.
    Thread thread = Thread.currentThread();
    UncaughtExceptionHandler originalHandler = thread.getUncaughtExceptionHandler();
    final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
    thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            throwableRef.set(ex);
        }
    });
    Worker worker = scheduler.createWorker();
    final NullPointerException npe = new NullPointerException();
    Runnable action = new Runnable() {

        @Override
        public void run() {
            throw npe;
        }
    };
    worker.schedule(action);
    runUiThreadTasks();
    Throwable throwable = throwableRef.get();
    assertTrue(throwable instanceof IllegalStateException);
    assertEquals("Fatal Exception thrown on Scheduler.", throwable.getMessage());
    assertSame(npe, throwable.getCause());
    // Restore the original uncaught exception handler.
    thread.setUncaughtExceptionHandler(originalHandler);
}
Also used : CountingRunnable(io.reactivex.android.testutil.CountingRunnable) Worker(io.reactivex.Scheduler.Worker) AtomicReference(java.util.concurrent.atomic.AtomicReference) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) Test(org.junit.Test)

Example 3 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project RxJava by ReactiveX.

the class RxJavaPlugins method uncaught.

static void uncaught(@NonNull Throwable error) {
    Thread currentThread = Thread.currentThread();
    UncaughtExceptionHandler handler = currentThread.getUncaughtExceptionHandler();
    handler.uncaughtException(currentThread, error);
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 4 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project RxJava by ReactiveX.

the class ScheduledRunnableTest method pluginCrash.

@Test
public void pluginCrash() {
    Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

        @Override
        public void uncaughtException(Thread t, Throwable e) {
            throw new TestException("Second");
        }
    });
    CompositeDisposable set = new CompositeDisposable();
    final ScheduledRunnable run = new ScheduledRunnable(new Runnable() {

        @Override
        public void run() {
            throw new TestException("First");
        }
    }, set);
    set.add(run);
    try {
        run.run();
        fail("Should have thrown!");
    } catch (TestException ex) {
        assertEquals("Second", ex.getMessage());
    } finally {
        Thread.currentThread().setUncaughtExceptionHandler(null);
    }
    assertTrue(run.isDisposed());
    assertEquals(0, set.size());
}
Also used : TestException(io.reactivex.exceptions.TestException) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Test(org.junit.Test)

Example 5 with UncaughtExceptionHandler

use of java.lang.Thread.UncaughtExceptionHandler in project RxJava by ReactiveX.

the class RxJavaPluginsTest method onErrorNoHandler.

@Test
public void onErrorNoHandler() {
    try {
        final List<Throwable> list = new ArrayList<Throwable>();
        RxJavaPlugins.setErrorHandler(null);
        Thread.currentThread().setUncaughtExceptionHandler(new UncaughtExceptionHandler() {

            @Override
            public void uncaughtException(Thread t, Throwable e) {
                list.add(e);
            }
        });
        RxJavaPlugins.onError(new TestException("Forced failure"));
        Thread.currentThread().setUncaughtExceptionHandler(null);
        // this will be printed on the console and should not crash
        RxJavaPlugins.onError(new TestException("Forced failure 3"));
        assertEquals(1, list.size());
        assertUndeliverableTestException(list, 0, "Forced failure");
    } finally {
        RxJavaPlugins.reset();
        Thread.currentThread().setUncaughtExceptionHandler(null);
    }
}
Also used : UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Aggregations

UncaughtExceptionHandler (java.lang.Thread.UncaughtExceptionHandler)51 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 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 LowLevelHeartBeatEvent (com.carrotsearch.ant.tasks.junit4.events.LowLevelHeartBeatEvent)1