Skip to content

Commit

Permalink
Merge pull request #391 from artem-zinnatullin/az/sync-scheduler-erro…
Browse files Browse the repository at this point in the history
…r-handler-with-rxjava

Sync HandlerScheduler error handling with RxJava impl.
  • Loading branch information
JakeWharton authored Aug 22, 2017
2 parents 4156143 + 51301fe commit c2dc1e8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ public void run() {
try {
delegate.run();
} catch (Throwable t) {
IllegalStateException ie =
new IllegalStateException("Fatal Exception thrown on Scheduler.", t);
RxJavaPlugins.onError(ie);
Thread thread = Thread.currentThread();
thread.getUncaughtExceptionHandler().uncaughtException(thread, ie);
RxJavaPlugins.onError(t);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import io.reactivex.Scheduler.Worker;
import io.reactivex.android.testutil.CountingRunnable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.plugins.RxJavaPlugins;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.junit.After;
Expand Down Expand Up @@ -638,37 +638,34 @@ public void disposedWorkerReturnsDisposedDisposables() {
assertTrue(disposable.isDisposed());
}

@Test public void throwingActionRoutedToHookAndThreadHandler() {
// TODO Test hook as well. Requires https://github.com/ReactiveX/RxJava/pull/3820.
@Test public void throwingActionRoutedToRxJavaPlugins() {
Consumer<Throwable> originalErrorHandler = RxJavaPlugins.getErrorHandler();

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);
}
});
try {
final AtomicReference<Throwable> throwableRef = new AtomicReference<>();
RxJavaPlugins.setErrorHandler(new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
throwableRef.set(throwable);
}
});

Worker worker = scheduler.createWorker();
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());
final NullPointerException npe = new NullPointerException();
Runnable action = new Runnable() {
@Override
public void run() {
throw npe;
}
};
worker.schedule(action);

// Restore the original uncaught exception handler.
thread.setUncaughtExceptionHandler(originalHandler);
runUiThreadTasks();
assertSame(npe, throwableRef.get());
} finally {
RxJavaPlugins.setErrorHandler(originalErrorHandler);
}
}

@Test
Expand Down

0 comments on commit c2dc1e8

Please sign in to comment.