Skip to content

Instantly share code, notes, and snippets.

@Jeevuz
Last active Dec 17, 2019
Embed
What would you like to do?
Test Rule to test the classes with rx shedulers
class SchedulersRule(private val useTestScheduler: Boolean = false) : ExternalResource() {
private lateinit var _testScheduler: TestScheduler
val testScheduler: TestScheduler
get() {
if (!useTestScheduler) throw IllegalStateException("TestScheduler is switched off.")
return _testScheduler
}
override fun before() {
RxJavaPlugins.setIoSchedulerHandler { _ -> Schedulers.trampoline() }
RxAndroidPlugins.setInitMainThreadSchedulerHandler { _ -> Schedulers.trampoline() }
val computationScheduler = if (useTestScheduler) {
_testScheduler = TestScheduler()
_testScheduler
} else {
Schedulers.trampoline()
}
RxJavaPlugins.setComputationSchedulerHandler { _ -> computationScheduler }
}
override fun after() {
RxJavaPlugins.reset()
RxAndroidPlugins.reset()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment