Skip to content

Instantly share code, notes, and snippets.

@carloseduardosx
Last active December 31, 2016 03:52
Show Gist options
  • Save carloseduardosx/95a1a5841b744acea5954c8fbf544571 to your computer and use it in GitHub Desktop.
Save carloseduardosx/95a1a5841b744acea5954c8fbf544571 to your computer and use it in GitHub Desktop.
Simple JUnit TestRule for do something when Observable schedulers change
package com.carloseduardo.rules
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
import rx.Scheduler
import rx.android.plugins.RxAndroidPlugins
import rx.android.plugins.RxAndroidSchedulersHook
import rx.plugins.RxJavaHooks
import rx.schedulers.Schedulers
class RxJavaSchedulersChangedRule : TestRule {
override fun apply(base: Statement?, description: Description?): Statement {
return object: Statement() {
override fun evaluate() {
RxJavaHooks.setOnIOScheduler { Schedulers.immediate() }
RxJavaHooks.setOnComputationScheduler { Schedulers.immediate() }
RxJavaHooks.setOnNewThreadScheduler { Schedulers.immediate() }
RxAndroidPlugins.getInstance().reset()
RxAndroidPlugins.getInstance().registerSchedulersHook(object: RxAndroidSchedulersHook() {
override fun getMainThreadScheduler(): Scheduler {
return Schedulers.immediate()
}
})
try {
base?.evaluate()
} finally {
RxJavaHooks.reset()
RxAndroidPlugins.getInstance().reset()
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment