|
19 | 19 | import io.reactivex.Scheduler.Worker;
|
20 | 20 | import io.reactivex.android.testutil.CountingRunnable;
|
21 | 21 | import io.reactivex.disposables.Disposable;
|
| 22 | +import io.reactivex.functions.Consumer; |
22 | 23 | import io.reactivex.functions.Function;
|
23 | 24 | import io.reactivex.plugins.RxJavaPlugins;
|
24 |
| -import java.lang.Thread.UncaughtExceptionHandler; |
25 | 25 | import java.util.concurrent.TimeUnit;
|
26 | 26 | import java.util.concurrent.atomic.AtomicReference;
|
27 | 27 | import org.junit.After;
|
@@ -638,37 +638,34 @@ public void disposedWorkerReturnsDisposedDisposables() {
|
638 | 638 | assertTrue(disposable.isDisposed());
|
639 | 639 | }
|
640 | 640 |
|
641 |
| - @Test public void throwingActionRoutedToHookAndThreadHandler() { |
642 |
| - // TODO Test hook as well. Requires https://github.com/ReactiveX/RxJava/pull/3820. |
| 641 | + @Test public void throwingActionRoutedToRxJavaPlugins() { |
| 642 | + Consumer<Throwable> originalErrorHandler = RxJavaPlugins.getErrorHandler(); |
643 | 643 |
|
644 |
| - Thread thread = Thread.currentThread(); |
645 |
| - UncaughtExceptionHandler originalHandler = thread.getUncaughtExceptionHandler(); |
646 |
| - |
647 |
| - final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); |
648 |
| - thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
649 |
| - @Override public void uncaughtException(Thread thread, Throwable ex) { |
650 |
| - throwableRef.set(ex); |
651 |
| - } |
652 |
| - }); |
| 644 | + try { |
| 645 | + final AtomicReference<Throwable> throwableRef = new AtomicReference<>(); |
| 646 | + RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() { |
| 647 | + @Override |
| 648 | + public void accept(Throwable throwable) throws Exception { |
| 649 | + throwableRef.set(throwable); |
| 650 | + } |
| 651 | + }); |
653 | 652 |
|
654 |
| - Worker worker = scheduler.createWorker(); |
| 653 | + Worker worker = scheduler.createWorker(); |
655 | 654 |
|
656 |
| - final NullPointerException npe = new NullPointerException(); |
657 |
| - Runnable action = new Runnable() { |
658 |
| - @Override public void run() { |
659 |
| - throw npe; |
660 |
| - } |
661 |
| - }; |
662 |
| - worker.schedule(action); |
663 |
| - |
664 |
| - runUiThreadTasks(); |
665 |
| - Throwable throwable = throwableRef.get(); |
666 |
| - assertTrue(throwable instanceof IllegalStateException); |
667 |
| - assertEquals("Fatal Exception thrown on Scheduler.", throwable.getMessage()); |
668 |
| - assertSame(npe, throwable.getCause()); |
| 655 | + final NullPointerException npe = new NullPointerException(); |
| 656 | + Runnable action = new Runnable() { |
| 657 | + @Override |
| 658 | + public void run() { |
| 659 | + throw npe; |
| 660 | + } |
| 661 | + }; |
| 662 | + worker.schedule(action); |
669 | 663 |
|
670 |
| - // Restore the original uncaught exception handler. |
671 |
| - thread.setUncaughtExceptionHandler(originalHandler); |
| 664 | + runUiThreadTasks(); |
| 665 | + assertSame(npe, throwableRef.get()); |
| 666 | + } finally { |
| 667 | + RxJavaPlugins.setErrorHandler(originalErrorHandler); |
| 668 | + } |
672 | 669 | }
|
673 | 670 |
|
674 | 671 | @Test
|
|
0 commit comments