Skip to content

Instantly share code, notes, and snippets.

@fo2rist
Created November 23, 2019 03:46
Show Gist options
  • Save fo2rist/68a90651d2100087c3c13e58e13fa46e to your computer and use it in GitHub Desktop.
Save fo2rist/68a90651d2100087c3c13e58e13fa46e to your computer and use it in GitHub Desktop.
RxExceptionsHandler Handler Rule
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