Skip to content

Instantly share code, notes, and snippets.

@seoft
Created May 22, 2021 06:16
Show Gist options
  • Save seoft/3f0eb9f096ce04840e485e6d7eaa7b13 to your computer and use it in GitHub Desktop.
Save seoft/3f0eb9f096ce04840e485e6d7eaa7b13 to your computer and use it in GitHub Desktop.
RxSchedulerRule.kt
import io.reactivex.android.plugins.RxAndroidPlugins
import io.reactivex.plugins.RxJavaPlugins
import io.reactivex.schedulers.Schedulers
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement
class RxSchedulerRule : TestRule {
override fun apply(base: Statement?, description: Description?): Statement {
return object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
RxJavaPlugins.setIoSchedulerHandler { Schedulers.trampoline() }
RxJavaPlugins.setComputationSchedulerHandler { Schedulers.trampoline() }
RxJavaPlugins.setNewThreadSchedulerHandler { Schedulers.trampoline() }
RxJavaPlugins.setSingleSchedulerHandler { Schedulers.trampoline() }
RxAndroidPlugins.setInitMainThreadSchedulerHandler { Schedulers.trampoline() }
try {
base?.evaluate()
} finally {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment