Skip to content

Commit c2dc1e8

Browse files
authored
Merge pull request #391 from artem-zinnatullin/az/sync-scheduler-error-handler-with-rxjava
Sync HandlerScheduler error handling with RxJava impl.
2 parents 4156143 + 51301fe commit c2dc1e8

File tree

2 files changed

+26
-33
lines changed

2 files changed

+26
-33
lines changed

rxandroid/src/main/java/io/reactivex/android/schedulers/HandlerScheduler.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,7 @@ public void run() {
108108
try {
109109
delegate.run();
110110
} catch (Throwable t) {
111-
IllegalStateException ie =
112-
new IllegalStateException("Fatal Exception thrown on Scheduler.", t);
113-
RxJavaPlugins.onError(ie);
114-
Thread thread = Thread.currentThread();
115-
thread.getUncaughtExceptionHandler().uncaughtException(thread, ie);
111+
RxJavaPlugins.onError(t);
116112
}
117113
}
118114

rxandroid/src/test/java/io/reactivex/android/schedulers/HandlerSchedulerTest.java

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import io.reactivex.Scheduler.Worker;
2020
import io.reactivex.android.testutil.CountingRunnable;
2121
import io.reactivex.disposables.Disposable;
22+
import io.reactivex.functions.Consumer;
2223
import io.reactivex.functions.Function;
2324
import io.reactivex.plugins.RxJavaPlugins;
24-
import java.lang.Thread.UncaughtExceptionHandler;
2525
import java.util.concurrent.TimeUnit;
2626
import java.util.concurrent.atomic.AtomicReference;
2727
import org.junit.After;
@@ -638,37 +638,34 @@ public void disposedWorkerReturnsDisposedDisposables() {
638638
assertTrue(disposable.isDisposed());
639639
}
640640

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();
643643

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+
});
653652

654-
Worker worker = scheduler.createWorker();
653+
Worker worker = scheduler.createWorker();
655654

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);
669663

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+
}
672669
}
673670

674671
@Test

0 commit comments

Comments
 (0)