Created
November 23, 2019 03:46
-
-
Save fo2rist/68a90651d2100087c3c13e58e13fa46e to your computer and use it in GitHub Desktop.
RxExceptionsHandler Handler Rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RxSchedulersTrampolineRule : ExternalResource() { | |
private var defaultExceptionHandler: Thread.UncaughtExceptionHandler? = null | |
override fun before() { | |
// Remember current default exception handler and replace it with one that rethrows the exception | |
// Rethrowing allows unit test fail in the same place the application would fail. | |
defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() | |
Thread.setDefaultUncaughtExceptionHandler { _, exc -> throw exc } | |
// Turn on Trampoline rule to execute RX call synchronously | |
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() } | |
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() } | |
} | |
override fun after() { | |
RxJavaPlugins.reset() | |
RxAndroidPlugins.reset() | |
Thread.setDefaultUncaughtExceptionHandler(defaultExceptionHandler) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment